[2.0.64] LuaGuiElement.add woes with sparse arrays?

Bugs that are actually features.
User avatar
hgschmie
Fast Inserter
Fast Inserter
Posts: 125
Joined: Tue Feb 06, 2024 5:18 am
Contact:

[2.0.64] LuaGuiElement.add woes with sparse arrays?

Post 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 40 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 40 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.
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 4074
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

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

Post 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.
Post Reply

Return to “Not a bug”