i didn't tested yet, when i test i'll post the result here, and if you find any kind of error, post it. =)~
luascript.cpp
luascript.h on public slot
luascript.h on private or protected slot:
on the Events you want to share, go to the cpp code and find the constructor, then re-write this:
to this:
to use a shared state, define __SHARED_LUASTATES__ on luascript.h(will compile faster) or on definitions.h
luascript.cpp
Code:
bool LuaScriptInterface::initSharedState()
{
lua_State* shared_lua_state = getSharerState();
if(!shared_lua_state)
return false;
lua_newtable(shared_lua_state);
lua_setfield(shared_lua_state, LUA_REGISTRYINDEX, "EVENTS");
m_runningEventId = EVENT_ID_USER;
newSharerState(shared_lua_state);
registerFunctions();
return true;
}
void LuaScriptInterface::newSharerState(lua_State* L)
{
m_luaState = L;
}
lua_State* LuaScriptInterface::getSharerState()
{
static lua_State* sharer;
if(!sharer)
{
sharer = luaL_newstate();
#ifndef __USE_LUALIBRARIES__
//Here you load only the "safe" libraries
luaopen_base(sharer);
luaopen_table(sharer);
luaopen_os(sharer);
luaopen_string(sharer);
luaopen_math(sharer);
// Unsafe, but immensely useful
luaopen_debug(sharer);
#else
//And here you load both "safe" and "unsafe" libraries
luaL_openlibs(sharer);
#endif
std::string datadir = g_config.getString(ConfigManager::DATA_DIRECTORY);
if(loadFile(std::string(datadir + "global.lua")) == -1)
{
std::cout << "Warning: [LuaScriptInterface::initSharedState] Can not load " << datadir << "global.lua." << std::endl;
}
}
return sharer;
}
luascript.h on public slot
Code:
bool initSharedState();
luascript.h on private or protected slot:
Code:
void newSharerState(lua_State* L);
static lua_State* getSharerState();
on the Events you want to share, go to the cpp code and find the constructor, then re-write this:
Code:
m_scriptInterface.initState();
to this:
Code:
#ifndef __SHARED_LUASTATES__
m_scriptInterface.initState();
#else
m_scriptInterface.initSharedState();
#endif
to use a shared state, define __SHARED_LUASTATES__ on luascript.h(will compile faster) or on definitions.h