Page 1 of 1

Migration entity type + global ?

Posted: Fri May 06, 2016 9:31 am
by binbinhfr
Hi,

in my mod, I wanted to change an entity type "panel" from "container" to "decorative".

I renamed the entity to "panel2", and created a migration json to transfer the old entity to new ones. It works.

But I had references to old "panel" entities in global variables, and it seems that now they are lost/invalid, which breaks all previous game saves.

So I suppose that the json renaming created new "panel2" entities with new ids/pointers and that global variables still holds old references and has not been updated automatically.

Is there a way to update global references to old entities ? Because I red that the migration/lua does not access global.

Re: Migration entity type + global ?

Posted: Fri May 06, 2016 9:43 am
by Adil
I'd try manually replacing them with lua during script.on_configuration_changed. (As in call destroy() on old entity still present in game and create_entity() to provide new). I haven't done much fiddling with that but I believe it does have access to all data needed.

Re: Migration entity type + global ?

Posted: Fri May 06, 2016 10:00 am
by binbinhfr
Adil wrote:I'd try manually replacing them with lua during script.on_configuration_changed. (As in call destroy() on old entity still present in game and create_entity() to provide new). I haven't done much fiddling with that but I believe it does have access to all data needed.
Yes, old entity is still in the global, but it is invalid, and how can I know the reference to the new entity that is supposed to replace the old one ?

Re: Migration entity type + global ?

Posted: Fri May 06, 2016 10:23 am
by Choumiko
binbinhfr wrote:Yes, old entity is still in the global, but it is invalid, and how can I know the reference to the new entity that is supposed to replace the old one ?
remove the migration file and in on_configuration_changed check if the oldVersion is < than the version where the change should happen, something like this (untested):

Code: Select all

--[[
global.panels = {
  entity = reference to the panels
}
]]--
function on_configuration_changed(data)
    if data.mod_changes.YourModName then
      local old_version = data.mod_changes.YourModName.old_version
      if old_version and old_version < "1.0.0" then --(1.0.0 is the version where you introduced panel2 as the new type)
        for _, ent in pairs(global.panels) do
          local surface = ent.entity.surface
          local new_ent = surface.create_entity{name="panel2", position=ent.entity.position, force=ent.entity.force, direction=ent.entity.direction}
          ent.entity.destroy()
          ent.entity = new_ent
       end
      end
   end
end

Re: Migration entity type + global ?

Posted: Fri May 06, 2016 10:29 am
by binbinhfr
ok, so I have to keep both entity prototype in the game, forever, in case someone would migrate later from an old version ?

Re: Migration entity type + global ?

Posted: Tue May 10, 2016 10:02 am
by bobingabout
There are alternate mathods too... I mean, you could use a Migration json script to replace the old entity with the new, and also use a on_configuration_changed event script to reconstruct your global table using a find all entities in area script...

Or... the old entity, since you're replacing it, could be nothing but a dummy type entity, with basically blanks for most of it's data, the bare minimum for the game to accept it as the correct type of entity.

Re: Migration entity type + global ?

Posted: Tue May 10, 2016 12:28 pm
by binbinhfr
Yes, but my problem is that old entities were stored in a global-per-player table.
When I migrate entity names, new entities are created, but the old entity reference in the global variable is completely lost (infact entities are not renamed : it seems that old entites are destroyed and new entites are created, but not with the same reference/pointer), so, even if I can retrieve renamed entities by searching the map, I cannot tell which player they were belonging to, in order to store them back in the correct global-per-player variable... I don't know if I'm clear... :)

Re: Migration entity type + global ?

Posted: Wed May 11, 2016 8:06 am
by bobingabout
binbinhfr wrote:Yes, but my problem is that old entities were stored in a global-per-player table.
In that case, you're going to need to keep a dummy entity in your data files, and do a manual on_configuration_changed iterate and replace.

Re: Migration entity type + global ?

Posted: Wed May 11, 2016 8:17 am
by binbinhfr
Yes that was my previous conclusion. Not very clean, but... ;)
Thanks for your help Bob.