I'm developing a server for a multiplayer game, where I want to give each player his own Lua thread, that I'd like to fill with some unique globals, e.g. the player's name and id. Basically, I want to be able to set thread local variables from the host application, without any additional code in the actual scripts I'm writing, which I can then use over the duration of the play session, whenever I call a function. Is that possible with Lua?
// Example of how it naturally doesn't work
var L = Lua.LuaLNewState();
Lua.LuaRegister(L, "print", WriteLine);
dostring(L, "print('hello, world')", null);
var x = Lua.LuaNewThread(L);
Lua.LuaPushLiteral(x, "charId");
Lua.LuaPushNumber(x, 123);
Lua.LuaRawSet(x, Lua.LUA_GLOBALSINDEX);
dostring(x, "print('x1 '..charId)", null); // call to a function that uses charId
var y = Lua.LuaNewThread(L);
Lua.LuaPushLiteral(y, "charId");
Lua.LuaPushNumber(y, 456);
Lua.LuaRawSet(y, Lua.LUA_GLOBALSINDEX);
dostring(y, "print('y1 '..charId)", null); // call to a function that uses charId
dostring(x, "print('x2 '..charId)", null); // I want this to still be 123
Update: My application is multi-threaded, multiple players can run scripts at the same time. My understanding of setting the environment of a function in Lua is that it changes it globally, that won't work in my case.