Page 1 of 1
Howto fix "LuaCustomTable cannot be serialized."
Posted: Sat Dec 05, 2020 11:37 am
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)
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Sat Dec 05, 2020 11:40 am
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
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Sat Dec 05, 2020 11:45 am
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
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Sat Dec 05, 2020 12:08 pm
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".
A weird way to ask for help.
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Sat Dec 05, 2020 12:13 pm
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.
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Sat Dec 05, 2020 12:20 pm
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.
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Sat Dec 05, 2020 12:29 pm
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.
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Sat Dec 05, 2020 6:12 pm
by eradicator
ixu wrote: ↑Sat Dec 05, 2020 12:29 pm
That saying was meant ironically.
Poe's Law
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Sat Dec 05, 2020 7:50 pm
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.
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Sat Dec 05, 2020 10:44 pm
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?
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Sun Dec 06, 2020 12:14 am
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.
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Sun Dec 06, 2020 11:27 am
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.
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Sun Dec 06, 2020 11:36 am
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
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Sun Dec 06, 2020 11:42 am
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.
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Mon Dec 07, 2020 12:16 am
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.
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Mon Dec 07, 2020 12:48 am
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?
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Mon Dec 07, 2020 1:01 am
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.
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Mon Dec 07, 2020 1:10 am
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?
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Mon Dec 07, 2020 1:13 am
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?
Re: Howto fix "LuaCustomTable cannot be serialized."
Posted: Mon Dec 07, 2020 1:45 am
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.