在Lua代碼
山口純LUA對象的C函數,並獲得價值
Test = {}
function Test:new()
local obj = {}
setmetatable(obj, self)
self.__index = self
return obj
end
local a = Test:new()
a.ID = "abc123"
callCfunc(a)
C代碼
int callCfunc(lua_State * l)
{
void* obj = lua_topointer(l, 1); //I hope get lua's a variable
lua_pushlightuserdata(l, obj);
lua_getfield(l, 1, "ID");
std::string id = lua_tostring(l, 1); //I hoe get the value "abc123"
...
return 0;
}
但我的C測試結果爲
id = null
爲什麼?如何修改代碼工作正常?
PS:我不希望創建C測試類映射到Lua
==== ==== UPDATE1
此外,我已經添加了測試代碼,確認無誤傳入的參數。
int callCfunc(lua_State * l)
{
std::string typeName = lua_typename(l, lua_type(l, 1)); // the typeName=="table"
void* obj = lua_topointer(l, 1); //I hope get lua's a variable
lua_pushlightuserdata(l, obj);
lua_getfield(l, 1, "ID");
std::string id = lua_tostring(l, 1); //I hoe get the value "abc123"
...
return 0;
}
結果
typeName == "table"
所以傳入的參數類型是正確的
不完全的C代碼。 – chris
不是從0索引的對象嗎?不是所有的1都是零? – 2013-01-08 05:55:11
您可能想接受一些答案 - 您會得到更多回復。 0%接受小於可接受:-) – daven11