on_configuration_changed and on_init clashing

Place to get help with not working mods / modding interface.
User avatar
Muppet9010
Filter Inserter
Filter Inserter
Posts: 283
Joined: Sat Dec 09, 2017 6:01 pm
Contact:

on_configuration_changed and on_init clashing

Post 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.
Attachments
settings.lua
(542 Bytes) Downloaded 58 times
data-updates.lua
(2.29 KiB) Downloaded 58 times
control.lua
(6.08 KiB) Downloaded 61 times
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: on_configuration_changed and on_init clashing

Post 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?
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
User avatar
Muppet9010
Filter Inserter
Filter Inserter
Posts: 283
Joined: Sat Dec 09, 2017 6:01 pm
Contact:

Re: on_configuration_changed and on_init clashing

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

Return to “Modding help”