如何使用c#代码读取复杂的lua表

xzlaal3s  于 2022-12-03  发布在  C#
关注(0)|答案(1)|浏览(226)

我从lua的世界开始,目前我正在使用C代码创建表读数
我有一个表,看起来像这样:

  1. Consume =
  2. {
  3. Consume_first = {
  4. Value = 100
  5. Request = 200
  6. },
  7. Require_var = {
  8. Enable= true
  9. Name = "Am"
  10. }
  11. }

我想知道一些情况

冲减定义为lua_getglobal(L, "Consume");
请求启用名称是否使用lua_getfield函数?

示例:
lua_getfield(L, 1, "Value")
Consume_firstRequire_var中,我应该使用什么来定义它们?
我的代码:

  1. void read_table(void) {
  2. lua_State *L;
  3. L = luaL_newstate();
  4. luaL_openlibs(L);
  5. if (luaL_loadfile(L, "table.lua") || lua_pcall(L, 0, 0, 0))
  6. {
  7. ShowError("Error reading 'table.lua'\n");
  8. return;
  9. }
  10. lua_getglobal(L, "Consume");
  11. ....
  12. lua_getfield(L, -1, "Value");
  13. lua_getfield(L, -1, "Request");
  14. ....
  15. lua_getfield(L, -1, "Enable");
  16. lua_getfield(L, -1, "Name");
  17. lua_close(L);
  18. printf("Read Table complete.\n");
  19. }

我使用的是lua 5.4

ygya80vv

ygya80vv1#

大概是这样的:

  1. lua_getglobal(L, "Consume");
  2. // Get Consume.Consume_first
  3. lua_getfield(L, -1, "Consume_first");
  4. // Get Consume.Consume_first.Value
  5. lua_getfield(L, -1, "Value");
  6. // Get Consume.Consume_first.Request
  7. // Note that currently Consume.Consume_first.Value is on the stack
  8. // So Consume.Consume_first is at index -2
  9. lua_getfield(L, -2, "Request");

如果您对索引感到困惑,也可以使用lua_gettop来帮助您。

  1. lua_getglobal(L, "Consume");
  2. // Get Consume.Consume_first
  3. lua_getfield(L, -1, "Consume_first");
  4. int Consume_first_index = lua_gettop(L);
  5. // Get Consume.Consume_first.Value
  6. lua_getfield(L, Consume_first_index, "Value");
  7. // Get Consume.Consume_first.Request
  8. lua_getfield(L, Consume_first_index, "Request");
展开查看全部

相关问题