Page 1 of 1

Serializing function references in global table

Posted: Mon Jun 17, 2019 6:55 am
by ownlyme

Code: Select all

Error while running event level::on_tick (ID 0)
...Data/Roaming/Factorio/temp/currently-playing/control.lua:958: attempt to index upvalue '_ENV' (a nil value)
stack traceback:
	...Data/Roaming/Factorio/temp/currently-playing/control.lua:958: in function 'func'
	...Data/Roaming/Factorio/temp/currently-playing/control.lua:617: in function <...Data/Roaming/Factorio/temp/currently-playing/control.lua:575>

Code: Select all

line957: table.insert(global.on_tick[game.tick + 18000], function ()
line958: global.buildings[force].radars[i] = game.surfaces[1].create_entity{name = "dota-radar", position = pos, force = force}
attached savegame and mod... crash happens approx 1 minute after loading (at 10x game speed)

maybe because it forgets the local variables that were attached to the function?

Re: scenario crashes after loading

Posted: Mon Jun 17, 2019 7:07 am
by posila
Thanks for the report.
Serializing function references in global table is not supported.

Instead you can solve the problem like this:

Code: Select all

-- "static" global dispatch table
actions = {}
actions["create_dota_radar"] = 
  function (args) 
    global.buildings[args.force].radars[args.i] = game.surfaces[1].create_entity{name = "dota-radar", position = args.position, force = args.force} 
  end

...
  -- enqueue
  table.insert(global.on_tick[game.tick + 18000], { action = "create_dota_radar", args = { force = force, i = i, position = pos }})
...

...
  -- dispatch 
  for k,action in pairs(global.on_tick[event.tick]) do
    actions[action.action](action.args)
  end
...


Re: scenario crashes after loading

Posted: Mon Jun 17, 2019 7:13 am
by ownlyme
oh god... i recently started using this method like everywhere :/
so much work......

Re: Serializing function references in global table

Posted: Tue Jun 25, 2019 10:34 pm
by Rseding91
https://lua-api.factorio.com/latest/Global.html mentions what you can store in global.

Re: Serializing function references in global table

Posted: Tue Jun 25, 2019 10:41 pm
by ownlyme
functions work too, i guess - just not the local references