Page 1 of 1

Trouble getting data to persist

Posted: Sat Mar 16, 2019 2:58 pm
by danielbrauer
Hey, I'm having trouble keeping track of animations I've created across loads. My code looks like this:

Code: Select all

if global.labAnimations == nil then
    global.labAnimations = {}
end
local labAnimations = global.labAnimations

-- table is filled out like this when iterating through all labs:
            if labAnimations[lab.unit_number] == nil then
                labAnimations[lab.unit_number] = rendering.draw_animation({
                ...
This code works fine during a given run, in that only one animation is created per lab. However, when I save and load the game, new animations get created in addition to the ones in the save. I expect the table global.labAnimations to persist, and lab.unit_number to be persistent across save/load. What am I missing?

Re: Trouble getting data to persist

Posted: Sat Mar 16, 2019 3:05 pm
by eduran
https://lua-api.factorio.com/latest/Data-Lifecycle.html

Check out point 3. The code snippet looks like you are trying to read from and write to the global table during control.lua initialization.

Re: Trouble getting data to persist

Posted: Sat Mar 16, 2019 5:23 pm
by danielbrauer
That was it, thank you! Moving the global references into callbacks fixed the issue, and now my table appears to persist.