Читать таблицу Lua из C

Я пытаюсь передать таблицу Lua в мою C-программу, но я не знаю, как это сделать.

Мой код Lua:

local stages = {}
stages[1] = stage1
stages[2] = stage2
stages[3] = stage3

lstage.buildpollingtable(stages)

Мой код C:

static int lstage_build_polling_table (lua_State * L) {    
    luaL_checktype(L, 1, LUA_TTABLE);

    lua_getfield(L, 1, "stage1");
    lua_getfield(L, 1, "stage2");
    lua_getfield(L, 1, "stage3");

    stage_t s1 = lstage_tostage(L, -3);
    stage_t s2 = lstage_tostage(L, -2);
    stage_t s3 = lstage_tostage(L, -1);

    printf("%d\n",s1->priority);
    printf("%d\n",s2->priority);
    printf("%d\n",s3->priority);

    return 1;
}

Что мне нужно сделать, чтобы обойти все элементы? Этот код генерирует ошибку как это:

неверный аргумент #-3 для 'buildpollingtable' (ожидается lstage-Stage *, получена таблица)

Может кто-нибудь объяснить, что я делаю не так?

1 ответ

Решение

Ваша таблица не имеет названных полей stage1и т. д., только поля 1, 2, 3, Так что постарайтесь

lua_rawgeti(L,1,1);
lua_rawgeti(L,1,2);
lua_rawgeti(L,1,3);

вместо

lua_getfield(L, 1, "stage1");
lua_getfield(L, 1, "stage2");
lua_getfield(L, 1, "stage3");
Другие вопросы по тегам