I know the basics of interacting with lua and C, and I'm currently trying to execute the following lua line in C ++
Func1():Func2().Table1.value1
I am trying to get the value "value2" and use it in my C program. Below is the code I wrote to try to get this value in C.
int GetNumber() { int retn = 0; g_clientlua.lua_getfield(LUA_REGISTRYINDEX, "Player"); g_clientlua.lua_getfield(-1, "Func2"); g_clientlua.lua_getfield(LUA_GLOBALSINDEX, "Func1"); g_clientlua.lua_call(0, 1); g_clientlua.lua_call(1, 1); if (g_clientlua.lua_isnil(-1)) return retn; g_clientlua.lua_getfield(-1, "Table1"); if (g_clientlua.lua_isnil(-1)) return retn; g_clientlua.lua_getfield(-1, "value1"); if (g_clientlua.lua_isnil(-1)) return retn; retn = (int)g_clientlua.lua_tointeger(-1); }
The clientlua object is an object that basically allows me to call a method that calls it the equivalent lua_ * function, and populates the lua_state pointer parameter with a member variable, which is a pointer to the lua state.
Every time I call it, it complains about me, causing the lua stack to leak. To solve this problem, I tried adding lua_pop(3) to the end, but then it just crashes my program without an error message, so I assume that I am doing something wrong.
Does anyone have any words of wisdom for me? Kinda lost here. I doubt the above code is even written correctly, how would I write the above lua call in C?
source share