From d71ef89dbbe8b234bf398fc203b3733ef0f796d6 Mon Sep 17 00:00:00 2001 From: Isaac0-dev <62234577+Isaac0-dev@users.noreply.github.com> Date: Fri, 21 Feb 2025 09:37:31 +1000 Subject: [PATCH] add help in cobject arrays for 0 indexed accesses in case people try to access the array starting at 0 --- src/pc/lua/smlua_cobject.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/pc/lua/smlua_cobject.c b/src/pc/lua/smlua_cobject.c index ee325baf9..f82b9b646 100644 --- a/src/pc/lua/smlua_cobject.c +++ b/src/pc/lua/smlua_cobject.c @@ -438,8 +438,9 @@ static int smlua__get_field(lua_State* L) { return 0; } - u32 key = lua_tointeger(L, 2); - if (!key) { + int isNum; + u32 key = lua_tointegerx(L, 2, &isNum); + if (!isNum) { const char *key = lua_tostring(L, 2); if (key && key[0] == '_') { if (strcmp(key, "_lot") == 0) { @@ -455,6 +456,11 @@ static int smlua__get_field(lua_State* L) { return 0; } + if (key == 0) { + LOG_LUA_LINE("Key is out of bounds for array: key '%u' (help: array starts at 1)", key); + return 0; + } + key--; // Lua is +1 indexed if (key >= data->count) { LOG_LUA_LINE("Key is out of bounds for array: key '%u'", key);