lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 29 Nov 2012, at 23:20, Philipp Kraus wrote:

I would like to create for each of my individuals an own script, so I need for each script
one Lua state, do I? I would like to use one Lua state on one thread, so I would like
to use the Lua state with different Lua scripts. Can I do this?

If you write each script as a module

ie

local function run(args)
--do the calculation
--don't affect globals
end

return run

and then compile your scripts like this:

void addscript(lua_State *l, string name, string script) {
luaL_loadstring(l, script.c_str());
lua_pcall(l, 0, 1, 0);
lua_setglobal(l, name.c_str());
}

Then you have a global function for each script's run and you can do a lua_getglobal followed by a lua_call/lua_pcall to run it.

Thanks,
Kevin