on_configuration_changed and on_init clashing
Posted: Mon Oct 29, 2018 2:26 am
I need to add some entities to the map based on mod settings. So when the mod is added to an existing save or the runtime mod settings are changed during the game I have code to remove old mod setting based entities and add new ones.
Concept Code:
This works fine for new games and changing the mod setting once in a game. However, if you add the mod to an existing save both the on_init and on_configuration_changed events fire. This should be fine as on each loop of ReplaceEntities I remove what's not needed and only add when its missing.
What does happen is that I end up with 2 new entities added of type "bob"...modSettingValue. It feels like the game state isn't quite syncing between the 2 events occuring at map load, but I suspect I have got a coding approach error somewhere?
Full code attached. I end up with 2 "hiddenlight-" entities for both my existing turret and power pole entities on loading a map that didn't have the mod before. Any newly placed turrets or power poles only get 1 "hiddenlight" added.
Concept Code:
Code: Select all
ReplaceEntities = function()
modSettingValue = OBTAINED
newLampEntityName = "bob" .. modSettingValue
find and remove all entities at specific locations not called "bob-2" of type "lamp"
if "bob-2" lamp entity not found at location add it
end
script.on_init -> ReplaceEntities
script.on_configuration_changed -> ReplaceEntities
What does happen is that I end up with 2 new entities added of type "bob"...modSettingValue. It feels like the game state isn't quite syncing between the 2 events occuring at map load, but I suspect I have got a coding approach error somewhere?
Full code attached. I end up with 2 "hiddenlight-" entities for both my existing turret and power pole entities on loading a map that didn't have the mod before. Any newly placed turrets or power poles only get 1 "hiddenlight" added.