当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Assign value to Chaiscript variable through user-type object gives unexpect behaviour

问题描述:

I've got a peculiar problem with C++ and Chaiscript, hope somebody can help me with it, and

I'll try to give it as much information as it is needed.

Basically, calling a c++ function defined in c++ through chaiscript, which returns a Vector2 object (user-type object), will return crazy values when I try to get a value directly from a member variable of Vector2 (x or y).

If I assign the whole Vector2 to a variable, it will work as expected, and accessing the member variables will give me the expected value.

I managed to reproduce the problem with as minimum code as I could, getting rid of the physics engine and everything else I was using.

Here is the c++ code:

//test.cpp

#include <chaiscript/chaiscript.hpp>

#include <chaiscript/chaiscript_stdlib.hpp>

template<typename T>

struct Vector2

{

Vector2() : x(0), y(0) {};

Vector2(T px, T py) : x(px), y(py) {};

Vector2(const Vector2& cp) : x(cp.x), y(cp.y) {};

Vector2& operator+=(const Vector2& vec_r)

{

x += vec_r.x;

y += vec_r.y;

return *this;

}

Vector2 operator+(const Vector2& vec_r)

{

return Vector2(*this += vec_r);

}

void operator=(const Vector2& ver_r)

{

x = ver_r.x;

y = ver_r.y;

}

T x;

T y;

};

Vector2<float> GetValue()

{

return Vector2<float>(10,15);

}

int main()

{

chaiscript::ChaiScript _script(chaiscript::Std_Lib::library());

//Registering stuff

_script.add(chaiscript::user_type<Vector2<float>>(), "Vector2f");

_script.add(chaiscript::constructor<Vector2<float> ()>(), "Vector2f");

_script.add(chaiscript::constructor<Vector2<float> (float, float)>(), "Vector2f");

_script.add(chaiscript::constructor<Vector2<float> (const Vector2<float>&)>(), "Vector2f");

_script.add(chaiscript::fun(&Vector2<float>::x), "x");

_script.add(chaiscript::fun(&Vector2<float>::y), "y");

_script.add(chaiscript::fun(&Vector2<float>::operator +), "+");

_script.add(chaiscript::fun(&Vector2<float>::operator +=), "+=");

_script.add(chaiscript::fun(&Vector2<float>::operator =), "=");

_script.add(chaiscript::fun(&GetValue), "getValue");

_script.eval_file("test.chai");

return 0;

}

And here is the script:

//test.chai

var test = 0.0

var test2 = Vector2f(10,10)

test = getValue().x

print(test)

print(test2.x)

This code will reproduce the following:

6.52556e-38

10

This value from getValue().x (6.52556e-38) is actually different each time I run the code, but it will usually be some crazy value between 1.f~8.f that goes to exponential -36~-38.

I can get around this problem by using "var something = getValue()", and then accessing "something.x", but it would be helpful to understand why that happens and how I could get it fixed.

网友答案:

This is a bug in ChaiScript which has just been fixed on the develop branch and will be in the next release.

https://github.com/ChaiScript/ChaiScript/commit/4f972bcf67826591423149c006f80d43a026bd15

分享给朋友:
您可能感兴趣的文章:
随机阅读: