This is the event that is currently fired when using blueprint shotgun to upgrade a fast miniloader to an express miniloader:
Received Creation Event: {
created_entity = "[LuaEntity: hps__ml-express-miniloader at [gps=19.5,-1.5]]",
define_name = "script_raised_built",
entity = "SERPENT PLACEHOLDER",
name = 92,
options = {},
tick = 48550
}
It is a single creation event for a new miniloader (express miniloader). The fast miniloader is still there and the creation fails.
When changing the code to use apply_upgrade, it does this:
Received Deletion Event: {
define_name = "script_raised_destroy",
entity = "[LuaEntity: hps__ml-fast-miniloader at [gps=18.5,-1.5]]",
name = 93,
options = {},
tick = 43223
}
Received Creation Event: {
created_entity = "[LuaEntity: hps__ml-express-miniloader at [gps=18.5,-1.5]]",
define_name = "script_raised_built",
entity = "SERPENT PLACEHOLDER",
name = 92,
options = {},
tick = 43223
}
It fires a deletion event for the fast miniloader first and then a creation event for the express miniloader. This replaces it correctly.
I implemented your suggestion and added an on_object_destroyed handler for the main object. What happens now is that I see these events:
Received Creation Event: {
created_entity = "[LuaEntity: hps__ml-express-miniloader at [gps=19.5,-2.5]]",
define_name = "script_raised_built",
entity = "SERPENT PLACEHOLDER",
name = 92,
options = {},
tick = 82553
}
Received Object Destroyed: {
define_name = "on_object_destroyed",
name = 178,
options = {},
registration_number = 24,
tick = 82553,
type = 1,
useful_id = 248
}
So the fast miniloader is created first and then the game fires a object_destroyed event for the old loader (id 248). So they arrive in the wrong order. by the time the old loader is destroyed, the new one was already constructed.
I did bang my head against this for a while (see https://forums.factorio.com/viewtopic.php?t=133504) but in the end, the only way to make upgrades work reliably is to use LuaEntity#apply_upgrade(). I guess that is what it exists for.
Do with that information what you want; I explored your advice (using the on_object_destroyed handler) and that does not work. I don't use your mod and I try to help people that use your mod and the miniloader.