Page 1 of 1

Robot reconstructs a custom entity : how to recover data ?

Posted: Sat Sep 10, 2016 1:36 pm
by binbinhfr
Hi,

I often define entities based on an existing factorio entity (like a chest), but then adding more data on it (stored in global) , custom menu, etc...
The problem is that when such an entity is destroyed, it can be reconstructed by robots but then it losts all its private data and it is reconstructed as a new entity with intial data. I noticed that it is not the case for pure factorio objects ; example : a destroyed requester chest is reconstructed with the same request slots and not empty slots like a pure-new chest.
So my question is: is it possible that the automatic blueprint generated by the destruction of an entity could contain a reference to the destroyed entity ? Because in this case, I could copy the custom settings of the old entity into the new one...

Nothing here http://lua-api.factorio.com/0.14.5/LuaItemStack.html
but maybe I did not search at the right place.

Re: Robot reconstructs a custom entity : how to recover data ?

Posted: Sat Sep 10, 2016 1:51 pm
by aubergine18
The remains object has a ghost object IIRC.

* http://lua-api.factorio.com/latest/LuaE ... ghost_name -- ghost name
* http://lua-api.factorio.com/latest/LuaE ... ghost_type -- ghost type
* http://lua-api.factorio.com/latest/LuaE ... _prototype -- proto for ghost entity
* http://lua-api.factorio.com/latest/LuaE ... me_to_live -- remaining time until actual death
* http://lua-api.factorio.com/latest/LuaE ... m_requests -- items needed for revival

It's shame the entire ghost object isn't available on a .ghost property of the remains entity.

Re: Robot reconstructs a custom entity : how to recover data ?

Posted: Sat Sep 10, 2016 2:00 pm
by binbinhfr
I know these fucntions, but using this on the ghost, I can tell what is the ghost type, name, etc... but I do not see how it points to the "ancestor" of this ghost : a pointer to the entity (not the prototype) when it was still alive. Anyway, if the entity is dead, I suppose that it has no more ID, but even if the ID is not valid anymore, I wonder if it is still stored somewhere, so I can find the dead entity in my global data and transfer the data on the revived entity. Or maybe there is another trick to duplicate this custom data between ancestor and revived entity ? If someone has an idea...

Re: Robot reconstructs a custom entity : how to recover data ?

Posted: Sat Sep 10, 2016 2:22 pm
by aubergine18
I have several ideas how to handle the scenario, but they are all so awful I don't want to break the internet with them. (Like, revive the remains, grab what data you need, then unrevive it lol; or store compressed x,y of entities in addition to their unit_id so that you can map data either by entity unit_id or remains position)

Re: Robot reconstructs a custom entity : how to recover data ?

Posted: Sat Sep 10, 2016 6:50 pm
by Adil
You know there's on_entity_died event? You can store whatever you need while in that handler. Or you can just store your data in a way that is agnostic to entity death.

Re: Robot reconstructs a custom entity : how to recover data ?

Posted: Sat Sep 10, 2016 6:57 pm
by binbinhfr
Adil wrote:You know there's on_entity_died event? You can store whatever you need while in that handler. Or you can just store your data in a way that is agnostic to entity death.
Yes I know about this event, but how could I link to a ghost revival ? I suppose that a ghost revival triggers the creation event, but how to link the destruction and the creation event... Of course you have the coordinates, but as there can be several entity at the same place, it is not so obvious...

Re: Robot reconstructs a custom entity : how to recover data ?

Posted: Sat Sep 10, 2016 7:30 pm
by aubergine18
When a ghost is revived, does the resulting entity have the same unit_id and other settings as the original entity that was destroyed?

Re: Robot reconstructs a custom entity : how to recover data ?

Posted: Sat Sep 10, 2016 7:44 pm
by Adil
binbinhfr wrote:
Adil wrote:You know there's on_entity_died event? You can store whatever you need while in that handler. Or you can just store your data in a way that is agnostic to entity death.
Yes I know about this event, but how could I link to a ghost revival ? I suppose that a ghost revival triggers the creation event, but how to link the destruction and the creation event... Of course you have the coordinates, but as there can be several entity at the same place, it is not so obvious...
Under normal conditions entities don't overlap. And ones that are buildable by robots have their coordinates set exactly.
I have trouble imagining a reason to have several entities of the same name at the same coordinates and to need them to be destructible at the same on top of that.

Re: Robot reconstructs a custom entity : how to recover data ?

Posted: Sat Sep 10, 2016 9:22 pm
by binbinhfr
aubergine18 wrote:When a ghost is revived, does the resulting entity have the same unit_id and other settings as the original entity that was destroyed?
Very good question, I don't know the answer, because I don't know how to address this ID.

Adil wrote:Under normal conditions entities don't overlap. And ones that are buildable by robots have their coordinates set exactly.
I have trouble imagining a reason to have several entities of the same name at the same coordinates and to need them to be destructible at the same on top of that.
Well, in last extremity, I could use this complex trick, but I prefer to ask devs to a possible link between revived ghost and the dead "ancestor". It would be easier. And this link must exist in the data, because, as I told before, for requester chests, the revived entity inherits the slot settings of the dead entity.

So if there is a dev around here, please give us a hint ;-)