Page 1 of 1

[Rseding91] [0.14.2] on_built_entity error if others destroy the entity

Posted: Fri Sep 02, 2016 1:40 pm
by Mooncat
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.
onbuiltentityerror1_0.0.1.zip
(992 Bytes) Downloaded 187 times
2) Also the following one. When a wooden chest is built, this mod replaces it with a steel chest. The wooden chest is destroyed.
onbuiltentityerror2_0.0.1.zip
(1022 Bytes) Downloaded 204 times
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.
on_built_entity_error.png
on_built_entity_error.png (72.09 KiB) Viewed 5483 times
... game complains for mod 1 raising on_built_entity for an invalid entity.

Possible solution:
Have no idea at this moment. :?

Re: [Rseding91] [0.14.2] on_built_entity error if others destroy the entity

Posted: Fri Sep 02, 2016 6:37 pm
by Rseding91
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.

Re: [Rseding91] [0.14.2] on_built_entity error if others destroy the entity

Posted: Fri Sep 02, 2016 7:01 pm
by Mooncat
Yes, it makes sense. Thanks a lot! :D

Re: [Rseding91] [0.14.2] on_built_entity error if others destroy the entity

Posted: Fri Sep 02, 2016 7:43 pm
by credomane
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

Posted: Fri Sep 02, 2016 7:55 pm
by Rseding91
credomane 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.
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

Posted: Fri Sep 02, 2016 9:38 pm
by Rseding91
Fixed for 0.14.4.

Re: [Rseding91] [0.14.2] on_built_entity error if others destroy the entity

Posted: Sat Sep 03, 2016 4:39 am
by credomane
Rseding91 wrote:Yeah all events and data in events are guaranteed valid unless you specifically do something to invalidate them in the event handler :)
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. :/

Re: [Rseding91] [0.14.2] on_built_entity error if others destroy the entity

Posted: Sat Sep 03, 2016 7:25 am
by Mooncat
credomane wrote:
Rseding91 wrote:Yeah all events and data in events are guaranteed valid unless you specifically do something to invalidate them in the event handler :)
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. :/
It is a good habit to check entity.valid before accessing other properties. And I believe it is a must to avoid error.
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. :)
Rseding91 wrote:Fixed for 0.14.4.
Thanks~ :D