diff options
Diffstat (limited to 'src/Bindings/LuaChunkStay.cpp')
-rw-r--r-- | src/Bindings/LuaChunkStay.cpp | 45 |
1 files changed, 36 insertions, 9 deletions
diff --git a/src/Bindings/LuaChunkStay.cpp b/src/Bindings/LuaChunkStay.cpp index 606f7694f..f84964f56 100644 --- a/src/Bindings/LuaChunkStay.cpp +++ b/src/Bindings/LuaChunkStay.cpp @@ -11,20 +11,29 @@ +cLuaChunkStay::cLuaChunkStay(void) : + m_LuaState((lua_State *)NULL) // Want a detached Lua state by default +{ +} + + + + + void cLuaChunkStay::Enable( - cWorld & a_World, cLuaState & a_LuaState, - const cLuaState::cRef & a_OnChunkAvailable, const cLuaState::cRef & a_OnAllChunksAvailable + cWorld & a_World, lua_State * a_LuaState, + int a_OnChunkAvailableStackPos, int a_OnAllChunksAvailableStackPos ) { - if (m_LuaState != NULL) + if (m_LuaState.IsValid()) { LOGWARNING("LuaChunkStay: Already enabled. Ignoring this call."); - a_LuaState.LogStackTrace(); + cLuaState::LogStackTrace(a_LuaState); return; } - m_LuaState = &a_LuaState; - m_OnChunkAvailable = a_OnAllChunksAvailable; - m_OnAllChunksAvailable = a_OnAllChunksAvailable; + m_LuaState.Attach(a_LuaState); + m_OnChunkAvailable.RefStack(m_LuaState, a_OnChunkAvailableStackPos); + m_OnAllChunksAvailable.RefStack(m_LuaState, a_OnAllChunksAvailableStackPos); super::Enable(*a_World.GetChunkMap()); } @@ -32,9 +41,27 @@ void cLuaChunkStay::Enable( +void cLuaChunkStay::Disable(void) +{ + super::Disable(); + + // If the state was valid (bound to Lua functions), unbind those functions: + if (!m_LuaState.IsValid()) + { + return; + } + m_OnAllChunksAvailable.UnRef(); + m_OnChunkAvailable.UnRef(); + m_LuaState.Detach(); +} + + + + + void cLuaChunkStay::OnChunkAvailable(int a_ChunkX, int a_ChunkZ) { - m_LuaState->Call(m_OnChunkAvailable, a_ChunkX, a_ChunkZ); + m_LuaState.Call(m_OnChunkAvailable, a_ChunkX, a_ChunkZ); } @@ -43,7 +70,7 @@ void cLuaChunkStay::OnChunkAvailable(int a_ChunkX, int a_ChunkZ) void cLuaChunkStay::OnAllChunksAvailable(void) { - m_LuaState->Call(m_OnAllChunksAvailable); + m_LuaState.Call(m_OnAllChunksAvailable); } |