Howto fix "LuaCustomTable cannot be serialized."

Place to get help with not working mods / modding interface.
ixu
Fast Inserter
Fast Inserter
Posts: 107
Joined: Sun May 29, 2016 6:18 pm
Contact:

Howto fix "LuaCustomTable cannot be serialized."

Post by ixu »

When trying to save my mod crashes by saying "LuaCustomTable cannot be serialized."
My global structure seems to be harmless:

Code: Select all

serpent.block(global)
"{\
  Current = {\
    Gui = {},\
    Links = {},\
    Location = {},\
    PendingTranslation = {}\
  }\
}"
⏱️ 0.193600 ms
Logfile say this only:

Code: Select all

database initialize categories...
Database.lua:67
database initialize recipes...
Database.lua:72
database initialize complete.
Database.lua:77
2803.312 Warning WriteFileGuard.cpp:57: Writing D:\Data\Games\factorio\develop\saves\asdf.zip failed; previous version (if any) should still be available
2803.313 Error ParallelScenarioSaver.cpp:133: Saving scenario failed: Die Mod Ingame-Technologie-Browser (0.2.23) hat einen Fehler verursacht, der nicht behoben werden kann.
Bitte informiere den Autor der Mod über diesen Fehler.
Error while running event ingteb::on_save()
LuaCustomTable cannot be serialized.
debugadapter registered for level in Instrument Mode
debugadapter.lua:291
debugadapter registered for level in Instrument Mode
debugadapter.lua:291
debugadapter registered for level in Instrument Mode
debugadapter.lua:291
debugadapter registered for level in Instrument Mode
This also happens without debugger. (Users originally reported it)
Mods: ingteb, ixuClock, ixuAutoSave, NoCheating, hardCrafting (Maintainer) and more
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by eradicator »

Post some actual code. You're storing a LuaCustomTable, i.e. something like game.entity_prototypes.

See the doc list of various "CustomDictionary" types:
https://lua-api.factorio.com/latest/Lua ... ipt.forces
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
ixu
Fast Inserter
Fast Inserter
Posts: 107
Joined: Sun May 29, 2016 6:18 pm
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by ixu »

I added my global structure. There is nothing.
Other global variables are not important, right?
My mod has some lines. It is called ingteb.
https://mods.factorio.com/mod/ingteb
If you want you can go into it
Mods: ingteb, ixuClock, ixuAutoSave, NoCheating, hardCrafting (Maintainer) and more
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by eradicator »

ixu wrote: Sat Dec 05, 2020 11:45 am I added my global structure. There is nothing.
Other global variables are not important, right?
Doesn't say anything about what you're storing in that "structure".
ixu wrote: Sat Dec 05, 2020 11:45 am My mod has some lines. It is called ingteb.
https://mods.factorio.com/mod/ingteb
If you want you can go into it
A weird way to ask for help.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
ixu
Fast Inserter
Fast Inserter
Posts: 107
Joined: Sun May 29, 2016 6:18 pm
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by ixu »

I hoped there is some howto or some hints more than "something went wrong - please fix and reboot".
I am so sorry for asking. Lets close this post.
Mods: ingteb, ixuClock, ixuAutoSave, NoCheating, hardCrafting (Maintainer) and more
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by eradicator »

Like i said in my first post. The error message it tells you that you're storing a specific type of data that is not supported for save/load. As LuaCustomTable isn't used that much it should be fairly obvious if you know your own code. I didn't write the error message.

But if "please do a full code review" and then getting stroppy is your way of asking for help then i'll know better in the future than even trying to help.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
ixu
Fast Inserter
Fast Inserter
Posts: 107
Joined: Sun May 29, 2016 6:18 pm
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by ixu »

Excuse me for giving this impression. It was really a misunderstanding. I really only wanted help to help myself.
ixu wrote: Sat Dec 05, 2020 11:45 am If you want you can go into it
That saying was meant ironically.
But you helped me anyway. Because now I know that there is no howto. Please don't be mad at me.
Mods: ingteb, ixuClock, ixuAutoSave, NoCheating, hardCrafting (Maintainer) and more
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by eradicator »

ixu wrote: Sat Dec 05, 2020 12:29 pm That saying was meant ironically.
Poe's Law
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14339
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by Rseding91 »

You're storing a LuaCustomTable somewhere outside of global between ticks. Either you did it explicitly or you forgot to stick "local" in front of something when you read it from the game. Either way you aren't supposed to keep LuaObjects around outside of the global table because they will not get saved/loaded correctly and can cause desyncs in multiplayer because of that.
If you want to get ahold of me I'm almost always on Discord.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by eradicator »

Rseding91 wrote: Sat Dec 05, 2020 7:50 pm You're storing a LuaCustomTable somewhere outside of global between ticks.
Didn't know the engine checks for storage outside of global too... but also only on save/load?
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14339
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by Rseding91 »

eradicator wrote: Sat Dec 05, 2020 10:44 pm
Rseding91 wrote: Sat Dec 05, 2020 7:50 pm You're storing a LuaCustomTable somewhere outside of global between ticks.
Didn't know the engine checks for storage outside of global too... but also only on save/load?
The game tracks all LuaObjects created because most of them mutate the game state by existing. They're not supposed to be stored outside of an event handler and should be in the 'global' table so they get saved/loaded correctly.
If you want to get ahold of me I'm almost always on Discord.
ixu
Fast Inserter
Fast Inserter
Posts: 107
Joined: Sun May 29, 2016 6:18 pm
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by ixu »

I use global variables (beyond the global-table) for performance reasons - with lots of LuaCustomTable. They are stateless. When a save file is loaded, they are always redefined.
Rseding91 wrote: Sat Dec 05, 2020 7:50 pm You're storing a LuaCustomTable somewhere outside of global between ticks. Either you did it explicitly or you forgot to stick "local" in front of something when you read it from the game. Either way you aren't supposed to keep LuaObjects around outside of the global table because they will not get saved/loaded correctly and can cause desyncs in multiplayer because of that.
I don't understand, does that mean that it doesn't work or does that mean it works?
I'm getting desperate, because I'm pretty sure that the variable "global" doesn't contain any LuaCustomTable anymore, but the message is still there.
Mods: ingteb, ixuClock, ixuAutoSave, NoCheating, hardCrafting (Maintainer) and more
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5284
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by Klonan »

ixu wrote: Sun Dec 06, 2020 11:27 am I'm getting desperate, because I'm pretty sure that the variable "global" doesn't contain any LuaCustomTable anymore, but the message is still there.
It isn't just about 'global', if they are stored anywhere except in locals, the error will occur

If you are looking to cache value, you should do it in a different way, such as:

Code: Select all

local repair_tools
local get_repair_tools = function()
  if repair_tools then
    return repair_tools
  end
  repair_tools = {}
  for k, item in pairs (game.item_prototypes) do
    if item.type == "repair-tool" then
      repair_tools[item.name] = {name = item.name, count = 1}
    end
  end
  return repair_tools
end
ixu
Fast Inserter
Fast Inserter
Posts: 107
Joined: Sun May 29, 2016 6:18 pm
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by ixu »

Klonan wrote: Sun Dec 06, 2020 11:36 am It isn't just about 'global', if they are stored anywhere except in locals, the error will occur
So then everything is clear. Thank you very much.
Mods: ingteb, ixuClock, ixuAutoSave, NoCheating, hardCrafting (Maintainer) and more
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by eradicator »

Code: Select all

/c a = game.entity_prototypes game.auto_save()
Ok. I didn't expect that to error before this tread. Learned something new :).
_______
ixu wrote: Sun Dec 06, 2020 11:27 am I use global variables (beyond the global-table) for performance reasons - with lots of LuaCustomTable. They are stateless. When a save file is loaded, they are always redefined.
Just out of curiosity do you have an example for that? It sounds like you're saying you're doing something like... uhm, not sure. Initially i thought you meant:

Code: Select all

script.on_load(function()
  GLOBAL_EXAMPLE = game.entity_prototypes
  end)
but game isn't available in on_load so... what LuaCustomTables are even available in on_load to be "always redefined"? LuaForce.technologies/recipes maybe?

In any case what i really wanted to say is: According to my understanding (which may be wrong) there wouldn't be any performance gained in "caching" LuaForce.recipes because the expensive part is iterating it, not assigning a pointer to it. Thus as in @Klonans example in such cases the desired result of the iteration should be cached instead.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
ixu
Fast Inserter
Fast Inserter
Posts: 107
Joined: Sun May 29, 2016 6:18 pm
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by ixu »

What I now had to realize with pain is that it is not only about "local" as a declaration. You can not store these objects at all between two ticks. I should have known that before.
But it is ok.

But now I have the following question: Does this mean that everything I store anywhere between two ticks (even when declared as local) is serialized into the savefile? I use this for caches, for example, to get acceptable user experience. When I load a savefile, I consider this data as obsolete anyway, because the other mods or factorio might have changed everything. So I don't care about user experience at this point and re-calculate everything. That's ok too.

Is all this unwanted data really put into the save-file?
Mods: ingteb, ixuClock, ixuAutoSave, NoCheating, hardCrafting (Maintainer) and more
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by eradicator »

No, only the content of _ENV.global is serialized into the save.zip file. When you store local data between two ticks you gotta be wary of desyncs though.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
ixu
Fast Inserter
Fast Inserter
Posts: 107
Joined: Sun May 29, 2016 6:18 pm
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by ixu »

eradicator wrote: Mon Dec 07, 2020 1:01 am No, only the content of _ENV.global is serialized into the save.zip file. When you store local data between two ticks you gotta be wary of desyncs though.
That is what I understood. But why is there an error message about a variable outside of ENV.global that it can't be saved?
Mods: ingteb, ixuClock, ixuAutoSave, NoCheating, hardCrafting (Maintainer) and more
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by eradicator »

ixu wrote: Mon Dec 07, 2020 1:10 am
eradicator wrote: Mon Dec 07, 2020 1:01 am No, only the content of _ENV.global is serialized into the save.zip file. When you store local data between two ticks you gotta be wary of desyncs though.
That is what I understood. But why is there an error message about a variable outside of ENV.global that it can't be saved?
Dunno. Not a dev. Probably because nobody bothered to write a seperate error messages for in/out-side of global. And they're just trying to help modders not shoot themselfs in the foot?
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
ixu
Fast Inserter
Fast Inserter
Posts: 107
Joined: Sun May 29, 2016 6:18 pm
Contact:

Re: Howto fix "LuaCustomTable cannot be serialized."

Post by ixu »

It is a question to devs:
The following code prevents the game from being saved. The above error message appears.

Code: Select all

local LocalVariable 
script.on_init(function()
    LocalVariable = game.entity_prototypes
end)
I know this code is scary, but it is the simplified version of a real situation.
The question arises why it should cause problems when saving. But only because the game tries to save it, right?
I would think it would be cool if it didn't try that.

When I change it to this it does not complain:

Code: Select all

local LocalVariable ={} 
script.on_init(function()
    for key, value in pairs(game.entity_prototypes) do
        LocalVariable[key] = value
    end
end)
Does this mean LocalVariable is saved now? I don't want that.
Mods: ingteb, ixuClock, ixuAutoSave, NoCheating, hardCrafting (Maintainer) and more
Post Reply

Return to “Modding help”