1

I want to make a C function that takes a lua table with strings as parameter, and the lua table does not have any keys, just values. How can I do this? I cannot figure it out. I did not find anything when I searched in google.

4

1 回答 1

1

表中的“默认”键是从 1 开始的连续整数。这:

{"hello", "world"}

是相同的:

{[1] = "hello", [2] = "world"}

您不能使用 访问这些条目lua_getfield,因为它需要一个字符串键。您可以使用“手动”方式进行操作,使用lua_pushnumberand lua_gettable。如果L是你的lua_State*,t是堆栈上表的索引并且k是键,那么:

lua_pushnumber(L, k);
lua_gettable(L, t);

应该做同样的事情:

lua_getfield(L, t, k);

用于字符串键。请注意,如果t是一个相对索引(负数),那么因为您将另一个项目推入堆栈,您需要将其调整为 1。

于 2015-03-13T21:11:13.500 回答