Jump to content

"Script component Player of entity 3 failed to serialize"


Atrik
 Share

Recommended Posts

I get the error below on games when I play in local: (seems to occur only when the game is fast paced).
Any ideas on why this could occur or how to debug this? I'm still pretty noob.
Could this be occur because I deepclone player state multiple times per tick?
Any interpretations or idea on where this serialization fail could occur? Or how could I gain this information?


ERROR: Cannot serialize NaN values.
ERROR: Script component Player of entity 3 failed to serialize: Serialize_InvalidScriptValue
Serializing:
({playerID:1, color:{r:0.08235294117647059, g:0.21568627450980393, b:0.5843137254901961, a:1}, diplomacyColor:(void 0), displayDiplomacyColor:false, popUsed:42, popBonuses:70, maxPop:300, trainingBlocked:false, resourceCount:{food:48250, wood:47799, stone:50000, metal:49700}, resourceGatherers:{food:0, wood:10, stone:0, metal:0, undefined:NaN}, tradingGoods:[{goods:"food", proba:25}, {goods:"wood", proba:25}, {goods:"stone", proba:25}, {goods:"metal", proba:25}], team:0, teamsLocked:true, state:"active", diplomacy:[-1, 1, 1, -1], sharedDropsites:false, formations:["special/formations/null", "special/formations/box", "special/formations/column_closed", "special/formations/line_closed", "special/formations/column_open", "special/formations/line_open", "special/formations/flank", "special/formations/battle_line", "special/formations/skirmish", "special/formations/wedge", "special/formations/phalanx"], startCam:(void 0), controlAllUnits:false, isAI:false, cheatsEnabled:true, panelEntities:[], resourceNames:{food:"Food", wood:"Wood", stone:"Stone", metal:"Metal"}, disabledTemplates:{}, disabledTechnologies:{}, spyCostMultiplier:1, barterEntities:[], barterMultiplier:{buy:{food:"1.0", metal:"1.0", stone:"1.0", wood:"1.0"}, sell:{food:"1.0", metal:"1.0", stone:"1.0", wood:"1.0"}}})
terminate called after throwing an instance of 'PSERROR_Serialize_InvalidScriptValue'
  what():  Serialize_InvalidScriptValue
Aborted (core dumped)

Link to comment
Share on other sites

14 minutes ago, Yekaterina said:

Seems like an undefined parameter or some parameter that the game cannot recognise. Maybe check your starting condition files and the civ files, especially the section which talks about Starting units.

Thanks @Yekaterina! I don't quite get what files you are referring to, I haven't touched them, where are they located?

Also I must insist this isn't like a code typo or something as you can play entire games without problems and the games when it occurs are really when I push the speed up. Also doesn't seems to occur on any specific player action or game event.

I fear a bad practice in my code like maybe too much deepcloning but nothing (looks) susceptible to inject a new unknown resource.

Edited by Atrik
Link to comment
Share on other sites

Do you have a replay that reproduces the error?

The "undefined:NaN" is the problem here, I can try to help you debug it. Did you add a resource to the game? Can you check with an if-statement (if (type === undefined)) at https://code.wildfiregames.com/source/0ad/browse/ps/trunk/binaries/data/mods/public/simulation/components/Player.js$263 the stacktrace what called that function? (warn(new Error().stack))

 

  • Thanks 1
Link to comment
Share on other sites

 

2 hours ago, Freagarach said:

Can you check with an if-statement (if (type === undefined)) at https://code.wildfiregames.com/source/0ad/browse/ps/trunk/binaries/data/mods/public/simulation/components/Player.js$263 the stacktrace what called that function? (warn(new Error().stack))

Thanks that seems like the perfect thing to do!

However I'm probably doing something wrong, with the following code only "player" will ever print. (Also tested until getting game crash).

function Player() { }
warn("player");
Player.prototype.Schema =

[...]

Player.prototype.Serialize = function () {
warn("Serialize");
let state = {};

[...]


Player.prototype.Init = function () {
warn("init");

[...]

Player.prototype.GetNeededResources = function (amounts) {

// Check if we can afford it all.
warn("GetNeededResources");
let amountsNeeded = {};
for (let type in amounts)
	if (type === undefined)
	warn(new Error().stack);
	if (this.resourceCount[type] != undefined && amounts[type] > this.resourceCount[type])
		amountsNeeded[type] = amounts[type] - Math.floor(this.resourceCount[type]);

	[...]

Engine.ReRegisterComponentType(IID_Player, "Player", Player);

Context:
I've installed 0ad with snap.
I haven't figure out how to write on the original file (on Windows I think I was able to do it somehow). So it is ReRegistered in a mod by Player_MyMod.js.

I haven't ever tried to add resource to the game, and the bug really seems to occur randomly..

commands.txt

Edited by Atrik
Link to comment
Share on other sites

44 minutes ago, Atrik said:

However I'm probably doing something wrong, with the following code only "player" will ever print. (Also tested until getting game crash).

You should ditch the

warn("player");

and add the if-statement at "AddResourceGatherer". But the rest should still be called.

44 minutes ago, Atrik said:

Context:
I've installed 0ad with snap.
I haven't figure out how to write on the original file (on Windows I think I was able to do it somehow). So it is ReRegistered in a mod by Player_MyMod.js.

Snap is annoying. ;) But using a mod should work.

I get an OOS at turn 20, so can't use that replay? Do you get the crash when watching that replay?

  • Like 1
Link to comment
Share on other sites

2 hours ago, Freagarach said:

I get an OOS at turn 20, so can't use that replay? Do you get the crash when watching that replay?

I will get the OOS turn 20 as you do but the game doesn't crash. Weirdly it also happens for replays of online game where no OOS errors prompted then.

 

2 hours ago, Freagarach said:

You should ditch the

warn("player");

I did, now if nothing is printing even with a "warn" in the init, that can only be that the component isn't read, right?

 

   g_UnitActions["set-rallypoint"].execute(
            closestResourcePosition,
            {
                data: {
                    command: "gather",
                    resourceType: resourceType,
                    resourceTemplate: closestResourceTemplate,
                    target: closestResource
                }
            }

I think the command above possibly adding that unknown Resource Gatherer. That does make sens?
It's executed around turn 20 and the only command "gather" executing in my code. I just haven't though about it because I was in a method I haven't edited...



 

Edit:
Testing with

 warn(JSON.stringify(GetSimState().players[Engine.GetPlayerID()].resourceGatherers));

The change doesn't occur at turn 20, but much later.
WARNING: {"food":0,"wood":39,"stone":0,"metal":0}
WARNING: {"food":0,"wood":39,"stone":0,"metal":0,"undefined":null}

But disabling "QuickStart", the class with the code in question do remove the bug.

Trying to understand...

Edited by Atrik
Link to comment
Share on other sites

Ok sorry for the mess above. Now I have logged the stack:

if (type === undefined)
	warn(new Error().stack);
	
WARNING: Caught error: Player.prototype.AddResourceGatherer@simulation/components/Player_MyMod.js:234:27
ResourceGatherer.prototype.AddToPlayerCounter@simulation/components/ResourceGatherer.js:435:13
enter@simulation/components/UnitAI.js:2410:26
FSM.prototype.SwitchToNextState@globalscripts/FSM.js:366:14
FSM.prototype.ProcessMessage@globalscripts/FSM.js:274:8
UnitAI.prototype.PushOrder@simulation/components/UnitAI.js:3949:16
UnitAI.prototype.AddOrder@simulation/components/UnitAI.js:5302:8
UnitAI.prototype.GatherNearPosition@simulation/components/UnitAI.js:5656:8
gather-near-position/<@simulation/helpers/Commands.js:258:14
gather-near-position@simulation/helpers/Commands.js:257:67
ProcessCommand@simulation/helpers/Commands.js:53:23
Trainer.prototype.Item.prototype.Spawn@simulation/components/Trainer.js:294:18
Trainer.prototype.Item.prototype.Finish@simulation/components/Trainer.js:201:7
Trainer.prototype.Item.prototype.Progress@simulation/components/Trainer.js:347:7
Trainer.prototype.Progress@simulation/components/Trainer.js:688:24
ProductionQueue.prototype.Item.prototype.Progress@simulation/components/ProductionQueue.js:128:31
ProductionQueue.prototype.ProgressTimeout@simulation/components/ProductionQueue.js:423:16
Timer.prototype.OnUpdate@simulation/components/Timer.js:139:44

ERROR: Cannot serialize NaN values.

I can't really make sens of it.

I've pinpointed the event triggering the bug: the rally point is set to a resource that no longer exist.
That shouldn't make bugs normally, but it seems with my others mods it does.
It also only triggers it if you let the rally point set where the "g_UnitActions["set-rallypoint"].execute()" have previously set it.

The only components edited in my setup are unedited (excepted the GUIInterface):
https://github.com/LangLangBart/boonGUI/tree/main/simulation/components

Deleting the RallyPoint~boongui.js doesn't change anything to it.
The GUI has a function GuiInterface.prototype.DisplayRallyPoint but not sure it does anything.

Edited by Atrik
Link to comment
Share on other sites

On 17/03/2023 at 8:23 PM, Atrik said:

I've pinpointed the event triggering the bug: the rally point is set to a resource that no longer exist.
That shouldn't make bugs normally, but it seems with my others mods it does.

What mods do you have that could cause this? If it can be triggered without mods (that doesn't seem the case) or from the GUI we should find out how to reproduce and fix it. :)

On 18/03/2023 at 1:11 AM, Atrik said:

Thank you so much @Freagarach for finding where the problem was and helping/explain me how to debug this. :thumbsup:

Glad to be of service! :)

  • Like 2
Link to comment
Share on other sites

4 hours ago, Freagarach said:

What mods do you have that could cause this?

It seems I need to have boonGUI active and some code trying to do "g_UnitActions["set-rallypoint"].execute()" on a resource that will disappear.

The bug occurs when units finish producing and the rally point has been set by a script with the "execute" method.

So I know what's causing it and how to reproduce it, but my digging wasn't too successful to understand why this occurs when using boonGUI only.


Anyway, don't worry @Freagarach a patch sanitizing AddResourceGatherer works just fine, and the triggering of this bug seems niche enough to just go for this easy fix. :D

Nothing breaks, the gatherers still go for instance to the next closest tree, but something tries to add a "undefined" gatherer.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...