Rendering to accept players visibility with [index] = true table
Posted: Mon Oct 18, 2021 2:23 am
I'm currently writing a mod where I would like visibility of a rendered object to be enabled/disabled as a player option. There's no difficulty with this, except there is a slightly bothersome interface: the player visibility in rendering functions.
I can store player visibility as a table local to the file, and the most obvious / performant / convenient expression is keying by player_index, and the value being true or the key being absent. i,e, {[1] = true, [2] = true, (player 3 was removed), (player 4 has setting = false so key is not present), [5] = true... }
However, according to LuaRendering, all the player tables should be arrays:
https://lua-api.factorio.com/1.1.42/LuaRendering.html
Effectively I need to poorly represent the visibility settings, adding or removing players by looping over the whole table every time a player changes the setting (so it's prepared for draw_* functions), or I have to build the table every time I want to draw. The former is cumbersome, the latter would scale very poorly.
In my case, the frequency of visibility is an entity selected with on_selected_entity_changed, and changing settings would be infrequent.
The table pattern is similar to the existing pattern of collision mask:
https://lua-api.factorio.com/1.1.42/Con ... lisionMask
A speculative difference is that since players can be uints, strings, luaPlayers, the first value would need to be tested as true to be loaded "the other way". If collisions masks were dependent on the key type being string vs uint... then that logic doesn't work. Edit: (actually I guess it does)
I can store player visibility as a table local to the file, and the most obvious / performant / convenient expression is keying by player_index, and the value being true or the key being absent. i,e, {[1] = true, [2] = true, (player 3 was removed), (player 4 has setting = false so key is not present), [5] = true... }
However, according to LuaRendering, all the player tables should be arrays:
https://lua-api.factorio.com/1.1.42/LuaRendering.html
Effectively I need to poorly represent the visibility settings, adding or removing players by looping over the whole table every time a player changes the setting (so it's prepared for draw_* functions), or I have to build the table every time I want to draw. The former is cumbersome, the latter would scale very poorly.
In my case, the frequency of visibility is an entity selected with on_selected_entity_changed, and changing settings would be infrequent.
The table pattern is similar to the existing pattern of collision mask:
https://lua-api.factorio.com/1.1.42/Con ... lisionMask
A speculative difference is that since players can be uints, strings, luaPlayers, the first value would need to be tested as true to be loaded "the other way". If collisions masks were dependent on the key type being string vs uint... then that logic doesn't work. Edit: (actually I guess it does)