Lua Script
algdata={lua_obj1=-1,lua_obj2=-1}
return algdata
c++ code
Init
L = lua_open();
luaL_openlibs(L);
luaL_loadfile(L, "alg_script.lua");
update loop
int result;
printstackDump(L);
lua_newtable(L);
int tableindex = 1;
lua_pushnumber(L, tableindex++);
lua_pushnumber(L, fdcur->var1);
lua_rawset(L, -3);
lua_pushnumber(L, tableindex++);
lua_pushnumber(L, fdcur->var2);
lua_rawset(L, -3);
lua_pushnumber(L, tableindex++);
lua_pushnumber(L, fdcur->var3);
lua_rawset(L, -3);
lua_setglobal(L, "data");
result = lua_pcall(L, 0, 1, 0);
if (result) {
fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1));
}
ar->lua_obj1 = GetNumberFromLua(L, "lua_obj1");
ar->lua_obj2 = GetNumberFromLua(L, "lua_obj2");
ar->lua_obj3 = GetNumberFromLua(L, "lua_obj3");
printstackDump(L);
lua_pop(L, 1);
first update: 1st stackdump (first line)= 'function' 2nd stackdump (last line- before pop)= 'table'
second update: 1st stackdump (first line)= '' (empty) fail on lua_pcall = attempt to call a nil value
lua_pop(L, 1);from update loop - Egor Skriptunoff