Migration entity type + global ?

Place to post guides, observations, things related to modding that are not mods themselves.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Migration entity type + global ?

Post 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.
My mods on the Factorio Mod Portal :geek:
User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Migration entity type + global ?

Post 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.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: Migration entity type + global ?

Post 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 ?
My mods on the Factorio Mod Portal :geek:
Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

Re: Migration entity type + global ?

Post 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
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: Migration entity type + global ?

Post 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 ?
My mods on the Factorio Mod Portal :geek:
User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Migration entity type + global ?

Post 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.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: Migration entity type + global ?

Post 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... :)
My mods on the Factorio Mod Portal :geek:
User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Migration entity type + global ?

Post 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.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: Migration entity type + global ?

Post by binbinhfr »

Yes that was my previous conclusion. Not very clean, but... ;)
Thanks for your help Bob.
My mods on the Factorio Mod Portal :geek:
Post Reply

Return to “Modding discussion”