Page 1 of 1

[2.0.64] LuaGuiElement.add woes with sparse arrays?

Posted: Tue Aug 19, 2025 6:11 am
by hgschmie
consider this code:

local handler = {
[defines.events.on_gui_checked_state_changed] = 'onSort',
}

local child = element.add {
type = 'checkbox',
...
tags = {
value = sort_value,
entity_type = entity_type,
handler = handler,
},
}


Looking at this in the debugger shows this:
Screenshot 2025-08-18 at 23.06.16.png
Screenshot 2025-08-18 at 23.06.16.png (18.27 KiB) Viewed 41 times
so the numeric index 3 (which is on_gui_checked_state_changed) in the handler array has been converted to a string.

However, using on_click, which is 1 shows this:
Screenshot 2025-08-18 at 23.08.37.png
Screenshot 2025-08-18 at 23.08.37.png (20.32 KiB) Viewed 41 times
This seems to be a problem with an array that does not start at 1. It gets converted to a table (and all keys are now strings). If the array starts at 1, it is treated as an array.

This came as a surprise as lua clearly supports sparse arrays. But it seems the conversion from/to the C++ code when calling LuaGuiElement.add) gets confused.

Re: [2.0.64] LuaGuiElement.add woes with sparse arrays?

Posted: Tue Aug 19, 2025 6:17 am
by boskid
In lua, there is no special type for array table or for dictionary table, this is all user space interpretation of a generic lua table based on the table keys and lack of gaps within numeric indexes. I will just throw this to Not a bug.