1

Lua テーブルは、キーとしてテーブルを持つことができます。たとえば、次のようになります。

a = {[{}]=true}

lua C++ api からこれをどのようにインデックス化できるのか疑問に思っています。たとえば、次のことができます。

lua_getfield(L, -1, variablename);

スタック上のテーブルの文字列キーの値を取得します。テーブル値キーをスタックに置くにはどうすればよいですか?

4

2 に答える 2

4

lua_getfield is nothing more than syntactic sugar around a series of commands you could do on your own:

lua_pushstring(L, variablename);
lua_gettable(L -1 - 1);  //The second minus one represents the fact that your table is actually one index below the top now.

You push the key onto the stack, then use lua_gettable to access it. This is true regardless of what kind of key it is.

The only question you have to answer is how to actually get that key in the first place. For that... you're on your own. Every Lua table has a different value from every other Lua table. And if your Lua script just jammed a freshly-created Lua table in the key like that, without handing a reference to the table to you or storing a reference globally, you're hosed.

Your only recourse then is to just iterate through the table with lua_next and hope that a key who's type is "table" is the key you're looking for.

于 2013-06-17T04:25:20.470 に答える
0

でテーブルを反復する必要があると思いますlua_next。このリンクはプロセスを説明しています: http://pgl.yoyo.org/luai/i/lua_next .

反復テーブルを調査し、それが探しているものかどうかを判断します。

于 2013-06-17T05:46:37.670 に答える