[英]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.