Ir para conteúdo
Entre para seguir isso  
Eventide

[instável] Lua State compartilhado

Recommended Posts

Eventide    7
Eventide

Autor: Eventide

Servidor: OTserv 0.6.3_SVN

 

 

Eu não testei ainda, quando testar eu posto aqui.

qualquer erro encontrado, é só dizer neste tópico que eu arrumo! =D

 

 

luascript.cpp

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 na parte public

bool initSharedState();

 

luascript.h na parte private ou protected

void newSharerState(lua_State* L);
static lua_State* getSharerState();

 

Nos eventos que você deseja compartilhar o state, vá para o código cpp dele e procure o constructor da classe, então reponha isto:

m_scriptInterface.initState();

 

por isso:

 

#ifndef __SHARED_LUASTATES__
m_scriptInterface.initState();
#else
   m_scriptInterface.initSharedState();
#endif

 

para usar os states compartilhados, defina __SHARED_LUASTATES__ em luascript.h(compilará mais rápido) ou em definitions.h

Compartilhar este post


Link para o post
Fereu    0
Fereu

Wizo~ não sabia que você era moderador.

 

even~, pelo que entendi, com isso poderá usar alguma coisa de um script em outro?

 

me corrija se eu estiver errado ._.

Compartilhar este post


Link para o post
Eventide    7
Eventide

Sim, eu fiz isso principalmente pelo global.lua e bibliotecas. =o

Compartilhar este post


Link para o post
Lord' Raynex    0
Lord' Raynex

Gostei, Fico bem legal vo fazer aqui pra ver como fica...

Abraços

Compartilhar este post


Link para o post
LG    1
LG

olha eventide postando codigos??

O.o

como você falou a cho que vai ajudar principalmente no global.lua e bibliotecas!!!

Compartilhar este post


Link para o post
Visitante
Este tópico está impedido de receber novos posts.
Entre para seguir isso  
  • Quem Está Navegando   0 membros estão online

    Nenhum usuário registrado visualizando esta página.

×