[2.0.64] LuaGuiElement.add woes with sparse arrays?
Posted: Tue Aug 19, 2025 6:11 am
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:
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:
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.
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:
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:
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.