你好,我真的被这个看似简单的任务难住了。我可以访问传递给 C 函数的表的属性,但不能访问我在其中创建的任何子表的成员。
基本上我希望能够简单地从属性表中提取字符串,这样我就可以根据用户的期望创建一个“轮子”。
这是我到目前为止所拥有的(尝试了这么多我的大脑被炸了)
路亚端:
--Function
createSomething( "wheel", { canInflate = true, properties = { "large", "full" } } )
C面:
//I can retrieve any value easily within that table, but cannot seem to extract the table
//Within it named "properties", i can access the table, but cannot extract the strings inside
if( lua_istable(L, 2) ) {
lua_getfield(L, 2, "canInflate"); // Let's extract the value for the key 'someKey'. Pushes the value on the top of the stack
static int canInflate = lua_toboolean(L, -1); // get the value of bool now at the top of stack (index: -1)
//printf("can inflate is %d\n", canInflate);
//lua_pop(L, 1); // pop the value now that we are done with it
}
//try to get the properties table
if ( lua_istable(L, 2) ) {
lua_getfield(L, 2, "properties");
const char *str = lua_tostring(L, -1);
printf( "properties 1 = %s\n", str); // NULL
lua_pop(L, 2);
}
对此的任何帮助将不胜感激