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.
Migration entity type + global ?
Migration entity type + global ?
My mods on the Factorio Mod Portal 

Re: Migration entity type + global ?
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.
I also update mods, some of them even work.
Recently I did a mod tutorial.
Re: Migration entity type + global ?
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 ?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.
My mods on the Factorio Mod Portal 

Re: Migration entity type + global ?
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):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 ?
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 ?
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 

- bobingabout
- Smart Inserter
- Posts: 7352
- Joined: Fri May 09, 2014 1:01 pm
- Contact:
Re: Migration entity type + global ?
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.
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 ?
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...
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 

- bobingabout
- Smart Inserter
- Posts: 7352
- Joined: Fri May 09, 2014 1:01 pm
- Contact:
Re: Migration entity type + global ?
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.binbinhfr wrote:Yes, but my problem is that old entities were stored in a global-per-player table.
Re: Migration entity type + global ?
Yes that was my previous conclusion. Not very clean, but...
Thanks for your help Bob.

Thanks for your help Bob.
My mods on the Factorio Mod Portal 
