Serializing function references in global table

Things that we don't consider worth fixing at this moment.
User avatar
ownlyme
Filter Inserter
Filter Inserter
Posts: 433
Joined: Thu Dec 21, 2017 8:02 am
Contact:

Serializing function references in global table

Post 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?
Attachments
_autosave1.zip
(1.92 MiB) Downloaded 65 times
dota_0.17.12.zip
(5.39 MiB) Downloaded 65 times
Last edited by ownlyme on Mon Jun 17, 2019 7:36 am, edited 1 time in total.
creator of 52 mods
My requests: relAbove||Grenade arc||Blueprint allies||Creeps forget command/ don't get removed||Player Modifiers||textbox::selection||Better Heat IF||Singleplayer RCON||tank bug w/ min_range >= projectile_creation_distance
posila
Factorio Staff
Factorio Staff
Posts: 5409
Joined: Thu Jun 11, 2015 1:35 pm
Contact:

Re: scenario crashes after loading

Post 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
...

User avatar
ownlyme
Filter Inserter
Filter Inserter
Posts: 433
Joined: Thu Dec 21, 2017 8:02 am
Contact:

Re: scenario crashes after loading

Post by ownlyme »

oh god... i recently started using this method like everywhere :/
so much work......
creator of 52 mods
My requests: relAbove||Grenade arc||Blueprint allies||Creeps forget command/ don't get removed||Player Modifiers||textbox::selection||Better Heat IF||Singleplayer RCON||tank bug w/ min_range >= projectile_creation_distance
Rseding91
Factorio Staff
Factorio Staff
Posts: 14616
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Serializing function references in global table

Post by Rseding91 »

https://lua-api.factorio.com/latest/Global.html mentions what you can store in global.
If you want to get ahold of me I'm almost always on Discord.
User avatar
ownlyme
Filter Inserter
Filter Inserter
Posts: 433
Joined: Thu Dec 21, 2017 8:02 am
Contact:

Re: Serializing function references in global table

Post by ownlyme »

functions work too, i guess - just not the local references
creator of 52 mods
My requests: relAbove||Grenade arc||Blueprint allies||Creeps forget command/ don't get removed||Player Modifiers||textbox::selection||Better Heat IF||Singleplayer RCON||tank bug w/ min_range >= projectile_creation_distance
Post Reply

Return to “Won't fix.”