Lua 卢阿至';C';呼吁公约问题

Lua 卢阿至';C';呼吁公约问题,lua,Lua,我真的不想走元表等路线,因为它看起来相当复杂。 要在Lua中粗略地访问“C”结构,我需要: void execute_lua_script(char *name) { lua_pushstring (L,name); lua_gettable (L, LUA_GLOBALSINDEX); lua_pushstring(L,"junk"); lua_pushinteger(L,7); lua_pushlightuserdata(L, avatar_obj)

我真的不想走元表等路线,因为它看起来相当复杂。 要在Lua中粗略地访问“C”结构,我需要:

void execute_lua_script(char *name)
{
    lua_pushstring (L,name);
    lua_gettable (L, LUA_GLOBALSINDEX); 
    lua_pushstring(L,"junk");
    lua_pushinteger(L,7);
    lua_pushlightuserdata(L, avatar_obj);
    lua_pcall (L, 3, 2, 0);
}
注册的C函数为:

int get_obj_struct(lua_State *L)
{
    const char *str;
    OBJECT_DEF *obj;
    int stack;

    obj=(OBJECT_DEF *)lua_touserdata(L,1);

    str=lua_tostring(L,2);

    //printf("\nIN OBJ:%d %s",obj,str);

    if (!strcmp(str,"body->p.x"))
        lua_pushnumber(L,obj->body->p.x);

    if (!strcmp(str,"collided_with"))
        lua_pushlightuserdata(L, obj->collided_with);

    if (!strcmp(str,"type"))
        lua_pushnumber(L,obj->type);

    stack=lua_gettop(L);
    //printf("\n%d",stack);

    if (stack<3)
        report_error("Unknown structure request ",(char *)str);

    return 1;
}
我希望“collided_with”是一个指针,然后我可以将它传递回get_obj_struct并查找类型。 我知道这与我使用pushlightuserdata的mis以及obj的读取有关。 所以一个解释就好了!。另外,如果有人希望给出一个使用“表”的版本(我认为这样会更有效率),那么我将非常感谢您的帮助。

在线“书籍”很好地描述了如何在C中实现Lua类型。在我看来,您最好按照中提供的示例“正确地执行”并为对象创建一个完整的Lua包装器。除了易于维护之外,它几乎肯定比基于strcmp的
实现更快

function test(a,b,obj)
    --print("\nLUA! test:",a,b);

    b=b+1;

    c=get_obj_struct(obj,"body->p.x");

    --print("x:",c);

    collided_with=get_obj_struct(obj,"collided_with");
    type=get_obj_struct(collided_with,"type");

    print("type:",type);

    return a,b;
end