Page 1 of 1

Migration script and global

Posted: Thu Sep 10, 2015 7:53 pm
by Boogieman14
So I'm working on an update to my mod, and I ran into a problem where I feel the easiest solution is to add an extra field to the global table. I made a migration script for that:

Code: Select all

local count = 0
local total = 0

if global.eqChests ~= nil then
	game.player.print("global isn't nil")
	for k,eqChest in pairs(global.eqChests) do
		total = total + 1
		if eqChest[5] == nil then
			count = count + 1
			eqChest[5] = eqChest[1].surface
		end
	end
end

game.player.print("updated "..count.."/"..total.." chests")
Don't mind the lua inside the for loop for now, that's probably waaaay off, but it doesn't even get executed. The migration does run, it outputs "updated 0/0 chests". So apperently, global.eqChests isn't available at this point. What am I overlooking here, or is this even the correct approach to updating that table?

Re: Migration script and global

Posted: Thu Sep 10, 2015 8:28 pm
by Choumiko
Migration scripts don't have access to global. Do it in control.lua in game.on_load()
From FARL:

Code: Select all

  local function initGlob()
    if global.version == nil or global.version < "0.3.0" then
      global = {}
      global.version = "0.0.0"
    end
    global.players = global.players or {}
    global.savedBlueprints = global.savedBlueprints or {}
    global.farl = global.farl or {}
    global.railInfoLast = global.railInfoLast or {}
    if global.godmode == nil then global.godmode = false end
    godmode = global.godmode
    global.destroyNextTick = global.destroyNextTick or {}

    for i,farl in ipairs(global.farl) do
      farl = resetMetatable(farl, FARL)
    end
    for name, s in pairs(global.players) do
      s = resetMetatable(s,Settings)
      s:checkMods()
    end
   --snip
    if global.version < "0.3.3" then
      for i,farl in pairs(global.farl) do
        farl:deactivate("reset")
      end
      for i,player in pairs(global.players) do
        player.parallelTracks = defaultSettings.parallelTracks
        player.parallelLag = defaultSettings.parallelLag
      end
    end
    if global.version < "0.3.4" then
      for i,player in pairs(global.players) do
        player.boundingBoxOffsets = util.table.deepcopy(defaultSettings.boundingBoxOffsets)
      end
      global.version = "0.3.4"
    end
    global.version = "0.4.0"
  end

  local function oninit() initGlob() end

  local function onload()
    initGlob()
  end

game.on_init(oninit)
game.on_load(onload)

Re: Migration script and global

Posted: Thu Sep 10, 2015 9:16 pm
by Boogieman14
Thanks, I guess that makes more sense. At least now the thing is trying to conversion, that's a start.. It's still bombing out though, but that's a new puzzle :lol: