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

Bugs that are actually features.
Post Reply
Hornwitser
Fast Inserter
Fast Inserter
Posts: 205
Joined: Fri Oct 05, 2018 4:34 pm
Contact:

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

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

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

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

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

Rseding91
Factorio Staff
Factorio Staff
Posts: 13204
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post 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.
If you want to get ahold of me I'm almost always on Discord.

Hornwitser
Fast Inserter
Fast Inserter
Posts: 205
Joined: Fri Oct 05, 2018 4:34 pm
Contact:

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

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

Rseding91
Factorio Staff
Factorio Staff
Posts: 13204
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post 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.
If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Not a bug”