0
votes

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

1
Remove lua_pop(L, 1); from update loop - Egor Skriptunoff

1 Answers

0
votes

lua_pcall has been replaced with

int luaL_dofile (lua_State *L, const char *filename);
Loads and runs the given file. It is defined as the following macro:

     (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))
It returns 0 if there are no errors or 1 in case of errors.

This will reload the file in memory if needed, otherwise call pcall