diff --git a/src/lua/LegacyLuaAPI.cpp b/src/lua/LegacyLuaAPI.cpp index 12312615b2..b756ff18c6 100644 --- a/src/lua/LegacyLuaAPI.cpp +++ b/src/lua/LegacyLuaAPI.cpp @@ -699,10 +699,12 @@ int luatpt_element_func(lua_State *l) if(element > 0 && element < PT_NUM) { lua_el_func[element] = function; - if(replace) - lua_el_mode[element] = 2; + if (replace == 2) + lua_el_mode[element] = 3; //update before + else if (replace) + lua_el_mode[element] = 2; //replace else - lua_el_mode[element] = 1; + lua_el_mode[element] = 1; //update after return 0; } else diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 24952e01e9..88a9b74614 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -2381,19 +2381,21 @@ int LuaScriptInterface::elements_property(lua_State * l) { if(lua_type(l, 3) == LUA_TFUNCTION) { - lua_pushvalue(l, 3); - lua_el_func[id] = luaL_ref(l, LUA_REGISTRYINDEX); if (args > 3) { luaL_checktype(l, 4, LUA_TNUMBER); int replace = lua_tointeger(l, 4); - if (replace == 1) - lua_el_mode[id] = 2; + if (replace == 2) + lua_el_mode[id] = 3; //update before + else if (replace == 1) + lua_el_mode[id] = 2; //replace else - lua_el_mode[id] = 1; + lua_el_mode[id] = 1; //update after } else lua_el_mode[id] = 1; + lua_pushvalue(l, 3); + lua_el_func[id] = luaL_ref(l, LUA_REGISTRYINDEX); } else if(lua_type(l, 3) == LUA_TBOOLEAN && !lua_toboolean(l, 3)) { diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index d88e54ec2b..a3981cac7c 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -3817,6 +3817,15 @@ void Simulation::UpdateParticles(int start, int end) //call the particle update function, if there is one #if !defined(RENDERER) && defined(LUACONSOLE) + if (lua_el_mode[parts[i].type] == 3) + { + if (luacon_elementReplacement(this, i, x, y, surround_space, nt, parts, pmap) || t != parts[i].type) + continue; + // Need to update variables, in case they've been changed by Lua + x = (int)(parts[i].x+0.5f); + y = (int)(parts[i].y+0.5f); + } + if (elements[t].Update && lua_el_mode[t] != 2) #else if (elements[t].Update) @@ -3832,9 +3841,9 @@ void Simulation::UpdateParticles(int start, int end) } } #if !defined(RENDERER) && defined(LUACONSOLE) - if(lua_el_mode[t]) + if (lua_el_mode[parts[i].type] && lua_el_mode[parts[i].type] != 3) { - if(luacon_elementReplacement(this, i, x, y, surround_space, nt, parts, pmap)) + if (luacon_elementReplacement(this, i, x, y, surround_space, nt, parts, pmap) || t != parts[i].type) continue; // Need to update variables, in case they've been changed by Lua x = (int)(parts[i].x+0.5f); diff --git a/src/simulation/elements/TUNG.cpp b/src/simulation/elements/TUNG.cpp index 9b5f61da99..370e29f36b 100644 --- a/src/simulation/elements/TUNG.cpp +++ b/src/simulation/elements/TUNG.cpp @@ -102,6 +102,7 @@ int Element_TUNG::update(UPDATE_FUNC_ARGS) { sim->part_change_type(i,x,y,PT_BRMT); parts[i].ctype = PT_TUNG; + return 1; } return 0; }