i have implemented a lua wrapper to access a C++ class using userdata in lua following this article : http://lua-users.org/wiki/BindingWithMembersAndMethods
My C++ class looks like this :
class GameObject
{
public:
GameObject();
~GameObject();
int m_id;
float m_scale;
const char* m_path;
};
So far i can do this in lua:
gameObject = GameObject.new()
gameObject.path = "test"
gameObject.scale = 2
local path = gameObject.path
local scale = gameObject.scale
print(scale)
print(path)
Everything works fine except when printing the path variable :
The "set string" line comes from a debug i put in the setter function :
int LuaGameObjectManager::set_string (lua_State *L, void *v)
{
v = (void*)luaL_checkstring(L, 3);
std::cout << "set string : " << (char*) v << std::endl;
return 0;
}
So i guess i'm setting the right value and the issue must occur when getting it with this getter function :
int LuaGameObjectManager::get_string (lua_State *L, void *v)
{
char * tmp = (char*)v;
lua_pushstring(L, tmp );
return 1;
}
And this is how i define my methods and metatable :
static const luaL_Reg methodsArray[] = {
{"new", New},
{"load", load},
{0,0}
};
static const luaL_Reg metaArray[] = {
{"__gc", gc},
{"__tostring", toString},
{0, 0}
};
static LuaManager::Xet_reg_pre gettersArray[] = {
{"scale", get_int, offsetof(GameObject, m_scale) },
{"path", get_string, offsetof(GameObject, m_path) },
{0,0}
};
static LuaManager::Xet_reg_pre settersArray[] = {
{"scale", set_int, offsetof(GameObject, m_scale) },
{"path", set_string, offsetof(GameObject, m_path) },
{0,0}
};
So does anyone have an idea of why i'm printing some weird values ? is this an encoding issue ? I know you guys will need more code to understand my problem and to help me but the whole part to register the lua methods and metatable is pretty long so just tell my what code you would want me to post.
