Page 1 of 1

[0.17.79] LuaCustomTable cannot be serialized error occures even when stored in a local

Posted: Tue Nov 19, 2019 10:39 pm
by Hornwitser
The following code cases the game to be un-savable even though script local variables are not saved:

Code: Select all

local foo
script.on_init(function()
    foo = game.players
end)

Re: [0.17.79] LuaCustomTable cannot be serialized error occures even when stored in a local

Posted: Wed Nov 20, 2019 8:24 am
by darkfrei
Just place all variables to global:

Code: Select all

script.on_init(function()
    global.foo = game.players
end)
Or like that

Code: Select all

script.on_init(function()
    local players = game.players
    global.foo = {}
    for p_name, player in pairs (players) do
      global.foo[p_name] = player
    end
end)

Re: [0.17.79] LuaCustomTable cannot be serialized error occures even when stored in a local

Posted: Wed Nov 20, 2019 10:40 am
by Rseding91
Thanks for the report. That's intended and you should never be storing any LuaObject you get from the API like that. They are meant to be stored in function local variables and thrown away after a function ends or in global and persist until you erase them.

By them existing they change the game state and it can cause desyncs in multiplayer and break replays if they aren't saved/loaded correctly. By not having them in global they will not be saved/loaded correctly.

Re: [0.17.79] LuaCustomTable cannot be serialized error occures even when stored in a local

Posted: Wed Nov 20, 2019 4:06 pm
by Hornwitser
These rules don't seem to be written down anywhere. Where do I go to learn about rules like this one about the use of the API?

Re: [0.17.79] LuaCustomTable cannot be serialized error occures even when stored in a local

Posted: Wed Nov 20, 2019 6:58 pm
by Rseding91
I'm not sure if they are written anywhere. I don't know if it would really help because people just don't read and even if they do they would have to read the 1 place it was mentioned. This: https://lua-api.factorio.com/latest/Data-Lifecycle.html can be helpful but isn't exactly about this topic.

The basics of it are: store everything in global so it saves/loads correctly. There are of course exceptions to that but it's a good place to start.

The Modding help sub-forum may also have more helpful stuff.