推送 Lua 表
我已经在C中创建了一个Lua表,但我不确定如何将该表推到堆栈顶部,以便我可以将其传递给Lua函数。
有谁知道该怎么做?
这是我当前的代码:
lua_createtable(state, libraries.size(), 0);
int table_index = lua_gettop(state);
for (int i = 0; i < libraries.size(); i++)
{
lua_pushstring(state, libraries[i].c_str());
lua_rawseti(state, table_index, i + 1);
}
lua_settable(state, -3);
[ Push other things ]
[ Call function ]
I have created a Lua table in C, but I'm not sure how to push that table onto the top of a stack so I can pass it to a Lua function.
Does anyone know how to do this?
This is my current code:
lua_createtable(state, libraries.size(), 0);
int table_index = lua_gettop(state);
for (int i = 0; i < libraries.size(); i++)
{
lua_pushstring(state, libraries[i].c_str());
lua_rawseti(state, table_index, i + 1);
}
lua_settable(state, -3);
[ Push other things ]
[ Call function ]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个将字符串推送到表的快速辅助函数
这里我使用辅助函数来创建表并将其传递给函数
Here's a quick helper function to push strings to the table
Here I use the helper function to create the table and pass it to a function
该表已经在堆栈中了,就是
lua_newtable
离开它的地方,不是吗?The table is already in the stack, where
lua_newtable
left it, isn't it?我开源了一个小片段,解决了将简单的 Lua 字典表从 C 推送到 Lua 的问题。
您可以在此处查看,应该可以正常工作。
I made a small snippet open source that solves pushing simple Lua dictionary tables from C to Lua.
You can check it out here, should work well.