Common Serialization (Saving) Pitfalls

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Azel
Posts: 808
Joined: Thu Nov 06, 2014 10:40 pm

Re: Common Serialization (Saving) Pitfalls

Post by Azel » Fri Feb 13, 2015 12:19 am

Drakkan wrote:ok I found when game is save-crashing, but it makes no sense to me. When I kill creature named gaint_snake_4 or green_slime_2 (regular monster placed, not spawned or antyhing) and then save, game crash with string "Script.lua"]:0: attempt to concatenate field 'id' (a nil value). I do not use these monsters in any script or any trigger...
any clues ?
I have noticed I have in my dungeon monster green_slime_2, green_slime_3, green_slime_4 - missing number one. the same "issue" for snakes. could this be the problem ?
When I face unexplainable challenges like this, I found that a great way to pinpoint the issue is to create a brand new Mod with a single map, and slowly import each Asset from the original mod that is involved in the problem situation.

In your case, bring over the monster that causes the game crash after killed. Test it out. If that works fine, then bring over other assets on that map that are tied to either the monster or other structures that interact with that monster and/or the environment it resides in. Eventually you will come across that one asset, or one line of script, that is culprit.

User avatar
Drakkan
Posts: 1315
Joined: Mon Dec 31, 2012 12:25 am

Re: Common Serialization (Saving) Pitfalls

Post by Drakkan » Fri Feb 13, 2015 12:28 am

minmay wrote:
Drakkan wrote:ok I found when game is save-crashing, but it makes no sense to me. When I kill creature named gaint_snake_4 or green_slime_2 (regular monster placed, not spawned or antyhing) and then save, game crash with string "Script.lua"]:0: attempt to concatenate field 'id' (a nil value). I do not use these monsters in any script or any trigger...
any clues ?
As the original post says, this means you have a reference to an unserializable type. Have you changed the monsters' definitions in any way? Do they have any connectors? Look at the scripts they're connected to and see if you're creating any unserializable references there. Also, make sure that it's actually killing the monsters that causes it, not something else.
If you're having trouble keeping track of what you're doing in scripts, you might want to adopt a coding convention such as Hungarian notation that will make it easy to automatically search for specific variable types.
Drakkan wrote:I have noticed I have in my dungeon monster green_slime_2, green_slime_3, green_slime_4 - missing number one. the same "issue" for snakes. could this be the problem ?
Definitely not.
no connectors, no modifications... when I kill the monster and try to save, game just crash, its quite a mystery. I know there are some scripts running "behind" in my dungeon, like some timers etc... , however none is connected in any way to slimes / snakes... I tried to simulate in other dungeon but failed of course.
I will make some more runs and will see if problem is persisting. thanks for advices
Azel wrote:
Drakkan wrote:ok I found when game is save-crashing, but it makes no sense to me. When I kill creature named gaint_snake_4 or green_slime_2 (regular monster placed, not spawned or antyhing) and then save, game crash with string "Script.lua"]:0: attempt to concatenate field 'id' (a nil value). I do not use these monsters in any script or any trigger...
any clues ?
I have noticed I have in my dungeon monster green_slime_2, green_slime_3, green_slime_4 - missing number one. the same "issue" for snakes. could this be the problem ?
When I face unexplainable challenges like this, I found that a great way to pinpoint the issue is to create a brand new Mod with a single map, and slowly import each Asset from the original mod that is involved in the problem situation.

In your case, bring over the monster that causes the game crash after killed. Test it out. If that works fine, then bring over other assets on that map that are tied to either the monster or other structures that interact with that monster and/or the environment it resides in. Eventually you will come across that one asset, or one line of script, that is culprit.
thanks Azel. unfortunately as in the state my dungeon is, it would take hours just to import scripts and simulate conditions which could be root of this problem :/ I hope I will avoid this and problem will just dissapear in my next run...
Breath from the unpromising waters.
Eye of the Atlantis

Azel
Posts: 808
Joined: Thu Nov 06, 2014 10:40 pm

Re: Common Serialization (Saving) Pitfalls

Post by Azel » Fri Feb 13, 2015 12:45 am

Maybe a dumb question but... I assume you tried saving the game just before hitting the monster? Save it during the battle - after a few hits? Then save it immediately after the kill?

The only other thing I could think of is that maybe there is some sort of trigger or teleporter that has the "triggeredByMonster" option Enabled. Maybe a monster triggered one of these, which is now running code based on that event. Once the monster is dead, the trigger/event may have lost its reference to the monster.

I have disabled the triggeredByMonster in virtually every case to avoid strange things from happening. Another best practice is to make sure that almost every single line of code that references an object contains a "if myObject ~= nil" clause. Probably the best error handling available for Grimrock LUA coding. Quickly eliminates faulty code from running and making bad references.

Hope you find your problem!

minmay
Posts: 2654
Joined: Mon Sep 23, 2013 2:24 am

Re: Common Serialization (Saving) Pitfalls

Post by minmay » Fri Feb 13, 2015 1:10 am

Tip: you can use "if object" instead of "if object ~= nil". The only difference between the two is that "if object ~= nil" will evaluate to true if the object is false, and this behaviour is rarely useful.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.

Azel
Posts: 808
Joined: Thu Nov 06, 2014 10:40 pm

Re: Common Serialization (Saving) Pitfalls

Post by Azel » Fri Feb 13, 2015 1:33 am

I might use that to save on extra typing. Having the ~= nil evaluate to true even if the value within is "false" is perfectly fine, because the boolean check shouldn't even take place until the object has proven to exist (~= nil). Thus: if ~= nil then, if == false

If I'm using "if object" to make sure it exists I would not want to lump that together with checking its boolean value of true/false. Very sloppy, imo.

User avatar
Drakkan
Posts: 1315
Joined: Mon Dec 31, 2012 12:25 am

Re: Common Serialization (Saving) Pitfalls

Post by Drakkan » Mon Mar 09, 2015 5:25 pm

Hi guys. when trying to load the game from certain point (unfortunately I didnt noticed where it started went wrong) I get:

=== Software Failure ===

[string "Component.lua"]:0: invalid trigger action
stack traceback:
[C]: in function 'assert'
[string "Component.lua"]: in function 'addConnector'
[string "GameObject.lua"]: in function 'loadState'
[string "Map.lua"]: in function 'loadState'
[string "GameMode.lua"]: in function 'loadGame'
[string "GameMode.lua"]: in function 'update'
[string "Grimrock.lua"]: in function 'display'
[string "Grimrock.lua"]: in main chunk

any clues from a first look ?
Breath from the unpromising waters.
Eye of the Atlantis

minmay
Posts: 2654
Joined: Mon Sep 23, 2013 2:24 am

Re: Common Serialization (Saving) Pitfalls

Post by minmay » Wed Mar 11, 2015 2:55 am

I have seen this error once before but never managed to reproduce it. When a connector is loaded from the save file, its event, target, and action are validated. It will fail, resulting in that error, if you manage to save a connector with a bogus action and then load that saved game. I have never been able to do so.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.

User avatar
AndakRainor
Posts: 673
Joined: Thu Nov 20, 2014 5:18 pm

Re: Common Serialization (Saving) Pitfalls

Post by AndakRainor » Fri Apr 24, 2015 12:28 am

what about the delayedCall function ? Is it safe regarding saving / loading ?

minmay
Posts: 2654
Joined: Mon Sep 23, 2013 2:24 am

Re: Common Serialization (Saving) Pitfalls

Post by minmay » Fri Apr 24, 2015 1:20 am

I can't think of any way that you could possibly introduce a serialization issue with delayedCall, since it's basically a connector and you aren't allowed to pass anything except strings and numbers.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.

Azel
Posts: 808
Joined: Thu Nov 06, 2014 10:40 pm

Re: Common Serialization (Saving) Pitfalls

Post by Azel » Wed Jul 29, 2015 8:04 pm

minmay wrote:it's basically a connector and you aren't allowed to pass anything except strings and numbers.
Famous last words? :P

(IT nerd humor)

Post Reply