From 3a600334a76ed9bc04e3f5f829318e310e1cb591 Mon Sep 17 00:00:00 2001 From: jakubg1 <24206305+jakubg1@users.noreply.github.com> Date: Wed, 1 Apr 2026 02:43:25 +0200 Subject: [PATCH] Add an index parameter to `lua::get_memcell_values()` We could have done away with it just because the memcell values are always the last parameter. However, this might not always be the case in the future. Also, it's better for consistency reasons. --- scripting/lua.cpp | 17 ++++++++++------- scripting/lua.h | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/scripting/lua.cpp b/scripting/lua.cpp index 1c79b1c1..78705a08 100644 --- a/scripting/lua.cpp +++ b/scripting/lua.cpp @@ -98,21 +98,24 @@ void lua::push_memcell_values(lua_State *L, const TMemCell *mc) lua_settable(L, -3); } -memcell_values lua::get_memcell_values(lua_State *L) +memcell_values lua::get_memcell_values(lua_State *L, int idx) { + // For negative indices, we need to account for the extra item that is the key name/value extracted from the table. + if (idx < 0) + idx--; memcell_values mc{nullptr, 0.0, 0.0}; lua_pushstring(L, "str"); - lua_gettable(L, -2); + lua_gettable(L, idx); if (lua_isstring(L, -1)) mc.str = lua_tostring(L, -1); lua_pop(L, 1); lua_pushstring(L, "num1"); - lua_gettable(L, -2); + lua_gettable(L, idx); if (lua_isnumber(L, -1)) mc.num1 = lua_tonumber(L, -1); lua_pop(L, 1); lua_pushstring(L, "num2"); - lua_gettable(L, -2); + lua_gettable(L, idx); if (lua_isnumber(L, -1)) mc.num2 = lua_tonumber(L, -1); lua_pop(L, 1); @@ -282,7 +285,7 @@ int lua::scriptapi_train_getname(lua_State *L) int lua::scriptapi_dynobj_putvalues(lua_State *L) { auto *dyn = static_cast(lua_touserdata(L, 1)); - auto [str, num1, num2] = get_memcell_values(L); + auto [str, num1, num2] = get_memcell_values(L, 2); if (!dyn) return 0; TLocation loc{}; @@ -326,7 +329,7 @@ int lua::scriptapi_memcell_read_n(lua_State *L) int lua::scriptapi_memcell_update(lua_State *L) { auto *mc = static_cast(lua_touserdata(L, 1)); - auto [str, num1, num2] = get_memcell_values(L); + auto [str, num1, num2] = get_memcell_values(L, 2); if (mc) mc->UpdateValues(str, num1, num2, basic_event::flags::text | basic_event::flags::value1 | basic_event::flags::value2); @@ -336,7 +339,7 @@ int lua::scriptapi_memcell_update(lua_State *L) int lua::scriptapi_memcell_update_n(lua_State *L) { std::string mstr = lua_tostring(L, 1); - auto [str, num1, num2] = get_memcell_values(L); + auto [str, num1, num2] = get_memcell_values(L, 2); TMemCell *mc = simulation::Memory.find(mstr); if (mc) mc->UpdateValues(str, num1, num2, diff --git a/scripting/lua.h b/scripting/lua.h index 1578ffd8..07d72926 100644 --- a/scripting/lua.h +++ b/scripting/lua.h @@ -45,5 +45,5 @@ public: static void unref(lua_State *L, int ref); static void dispatch_event(lua_State *L, int handler, basic_event *event, const TDynamicObject *activator); static void push_memcell_values(lua_State *L, const TMemCell *mc); - static memcell_values get_memcell_values(lua_State *L); + static memcell_values get_memcell_values(lua_State *L, int idx); };