Page 1 of 1

on_configuration_changed and on_init clashing

Posted: Mon Oct 29, 2018 2:26 am
by Muppet9010
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:

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

Re: on_configuration_changed and on_init clashing

Posted: Tue Oct 30, 2018 12:05 pm
by eradicator
Are you sure you attached the right files? In that control.lua you only call OnStartup() in on_init/config and that doesn't really do anything like your "concept code" describes. And there's no function called ReplaceEntities either?

Re: on_configuration_changed and on_init clashing

Posted: Mon Nov 05, 2018 3:36 pm
by Muppet9010
yes it is the right code, however it turns out to be an issue with the surface.find_entities_filtered api call giving different results for area and position with my custom prototype. have logged as a bug: viewtopic.php?f=7&t=63270