繁体   English   中英

如何从C中的lua访问多维表?

[英]How to access multidimensional table from lua in C?

你好,我真的很难过这个看似简单的任务。 我可以访问传递给C中的函数的表的属性,但不能访问我在其中创建的任何子表的成员。

基本上我想简单地从属性表中提取字符串,这样我就可以根据用户的期望创建一个“轮子”。

这是我到目前为止所做的(尝试了我的大脑炒)

Lua Side:

--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);
}

任何有关这方面的帮助将不胜感激

您遇到的问题是如何在Lua中指定表:以下3个语句具有完全相同的结果:

t = { 'full','large'}
t = { [1] = 'full', [2] = 'large'}
t={};t[1]='full';t[2]='large'

你想要的是使用字符串作为键而不是值(如代码和上面的示例中所做的那样):

t={full=true,large=true}
-- or 
t={}; t.full=true; t.large=true

如果你使用字符串作为键,你的C代码应该工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM