Scenario:
1) My Creative Mode has the functionality of instant blueprint: instantly revive ghost entities. For compatibility to other mods, on_built_entity is raised for the revived entity so that other mods know and do things accordingly, e.g. register the entity to update it in on_tick.
2) Some mods have dummy entities. When any of these entities is built, it is destroyed immediately or replaced by another entity. E.g. Test Mode - the mjollnir, Natural Evolution Enemies - the Artifact Collector.
3) When I shift-place any of these dummy entities, it is revived. But when on_built_entity is raised, error message is shown and the game is quit.
These may be a little bit complicated, so I created 2 little mods to simulate instant blueprint and also entity destruction. You can easily reproduce the error with them.
Steps to reproduce:
1) Download the following mod. It has the functionality of instant blueprint. on_built_entity is raised.
2) Also the following one. When a wooden chest is built, this mod replaces it with a steel chest. The wooden chest is destroyed.
They don't depend on each other. No problem will occur if only one of them is used or only one of them is triggered.
3) Use both of these 2 little mods. Start a game.
4) Shift-place a wooden chest.
The ghost of wooden chest is placed -> on_built_entity for the ghost -> mod 1 is triggered -> revive the ghost -> on_built_entity for the revived wooden chest -> mod 2 is triggered -> destroy the chest -> step 5...
5) Boom.
... game complains for mod 1 raising on_built_entity for an invalid entity.
Possible solution:
Have no idea at this moment.
[Rseding91] [0.14.2] on_built_entity error if others destroy the entity
Re: [Rseding91] [0.14.2] on_built_entity error if others destroy the entity
I'll change it to work how the base game does event calls: if any of the data it's going to pass is invalid it simply doesn't fire the event.
That will mean that after the first mod does entity.destroy() the event stops firing but at that point there's no reason to need the event because the entity is gone anyway.
That will mean that after the first mod does entity.destroy() the event stops firing but at that point there's no reason to need the event because the entity is gone anyway.
If you want to get ahold of me I'm almost always on Discord.
Re: [Rseding91] [0.14.2] on_built_entity error if others destroy the entity
Yes, it makes sense. Thanks a lot!
Re: [Rseding91] [0.14.2] on_built_entity error if others destroy the entity
Here I was assuming that any entity passed in an event was assumed invalid until it passed through a entity.valid == true if check in my code. In the case of raising my own game events another if check is performed after that event too.
Re: [Rseding91] [0.14.2] on_built_entity error if others destroy the entity
Yeah all events and data in events are guaranteed valid unless you specifically do something to invalidate them in the event handlercredomane wrote:Here I was assuming that any entity passed in an event was assumed invalid until it passed through a entity.valid == true if check in my code. In the case of raising my own game events another if check is performed after that event too.
If you want to get ahold of me I'm almost always on Discord.
Re: [Rseding91] [0.14.2] on_built_entity error if others destroy the entity
Fixed for 0.14.4.
If you want to get ahold of me I'm almost always on Discord.
Re: [Rseding91] [0.14.2] on_built_entity error if others destroy the entity
Well crap. I assumed that valid was there so I could make sure the entity/GuiElement was still valid just in case another mod got the event first and did something nefarious to the entity/GuiElement. That would mean this "bug" has been around since I started Factorio with 0.12 stable and got into modding. It was the reason I started checking the "valid" parameter and assumed that was why it was there. :/Rseding91 wrote:Yeah all events and data in events are guaranteed valid unless you specifically do something to invalidate them in the event handler
Re: [Rseding91] [0.14.2] on_built_entity error if others destroy the entity
It is a good habit to check entity.valid before accessing other properties. And I believe it is a must to avoid error.credomane wrote:Well crap. I assumed that valid was there so I could make sure the entity/GuiElement was still valid just in case another mod got the event first and did something nefarious to the entity/GuiElement. That would mean this "bug" has been around since I started Factorio with 0.12 stable and got into modding. It was the reason I started checking the "valid" parameter and assumed that was why it was there. :/Rseding91 wrote:Yeah all events and data in events are guaranteed valid unless you specifically do something to invalidate them in the event handler
Though I think it is not necessary for GUI element, unless there is any naughty modder that mess with the GUI in your mod.
But this bug has nothing to do with the valid check. I have already validated the revived entity, before raising the event. But the error is shown anyway. That's why I submitted this report. There is no way we modders can fix it.
Thanks~Rseding91 wrote:Fixed for 0.14.4.