【发布时间】:2016-06-07 14:14:17
【问题描述】:
在这篇文章之后,我已经实现了一个 lua 包装器来使用 lua 中的用户数据访问 C++ 类:http://lua-users.org/wiki/BindingWithMembersAndMethods
我的 C++ 类如下所示:
class GameObject
{
public:
GameObject();
~GameObject();
int m_id;
float m_scale;
const char* m_path;
};
到目前为止,我可以在 lua 中做到这一点:
gameObject = GameObject.new()
gameObject.path = "test"
gameObject.scale = 2
local path = gameObject.path
local scale = gameObject.scale
print(scale)
print(path)
除了打印路径变量外,一切正常:
“设置字符串”行来自我在 setter 函数中进行的调试:
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;
}
所以我想我设置了正确的值,并且在使用这个 getter 函数获取它时一定会出现问题:
int LuaGameObjectManager::get_string (lua_State *L, void *v)
{
char * tmp = (char*)v;
lua_pushstring(L, tmp );
return 1;
}
这就是我定义我的方法和元表的方式:
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}
};
那么有人知道我为什么要打印一些奇怪的值吗?这是编码问题吗? 我知道你们需要更多代码来理解我的问题并帮助我,但是注册 lua 方法和元表的整个部分很长,所以请告诉我你希望我发布什么代码。
【问题讨论】: