How to create nested Lua tables using the C API(如何使用 C API 创建嵌套的 Lua 表)
问题描述
我想创建一个表格
使用 C API?
我目前的做法是
在循环中创建内部表.之前,这个循环,我用
to create the inner tables in a loop. Before, this loop, I use
为 2 个数字槽创建外部表.
to create the outer table for 2 numeric slots.
但是如何将内表保存到外表?
But how can I save the inner tables to the outer table?
推荐答案
这是一个完整的最小程序,演示了如何嵌套表.基本上你缺少的是 lua_setfield
函数.>
Here's a full and minimal program demonstrating how to nest tables. Basically what you are missing is the lua_setfield
function.
程序将简单地打印true
.
如果你需要数字索引,那么你继续使用lua_settable
:
If you need numeric indices, then you continue using lua_settable
:
您可能想使用 lua_objlen,而不是像我那样使用 0 的绝对索引
生成索引.
Rather than using absolute indices of 0 like I did, you might want to use lua_objlen
to generate the index.
这篇关于如何使用 C API 创建嵌套的 Lua 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!