1

こんにちは私はこの一見単純なタスクに本当に困惑しています。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);
}

これに関する助けをいただければ幸いです

4

1 に答える 1

3

あなたが抱えている問題は、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コードが機能するはずです。

于 2012-07-13T14:23:39.530 に答える