Script call causes 'clamp' crash

On occasions i.e. not every time I run a particular battle, it crashes at the very beginning of the whole process with this error message in the console

Screenshot 2023-12-08 113137.png


The process leading to this is as follows:
The particular battle is one where I allow the player to retry, rather than reload to a save.
The battle event only has one item before the Battle Processing command and that is to call a common event.
1702045883307.png


The line in rmmzobjects.js which the error message refers to is this:
Screenshot 2023-12-08 113731.png


As it is references hp, it seems to me that it must be referring to the first set of event commands in the common event. Those commands are these:
Screenshot 2023-12-08 113632.png


Actor 1 does not have MP, that's why it's not included in that line.

Those scriptcalls come from this tutorial. The concise version comes towards the end.

If the script calls were incorrect, then I assume it would crash every time. But it doesn't, only sometimes.

Can anyone work out what is going on?
Thank you
The error basically means that the actor is undefined, because the _hp should automatically be defined with every actor.

with that info in the background, can you see a common case for the cases when the crash happens? Like a missing actor or something li8ke that?
Those things don't appear to be related to me. Your event is referencing the hp values, not calling the refresh function on anything.

As Andar says, the crash is saying the battle scene is trying to refresh a battler that doesn't exist; for example, if you deleted an enemy that was in a troop.
The error states that the actor's _hp property has a value of undefined (therefore cannot possess a method clamp). If the actor didn't exist, that would cause a different error, a la "cannot read property _hp of undefined".

Like Andar says, by default _hp is only ever assigned numerical values, e.g. _hp = 1 on initialisation. I imagine the most likely scenario is that you have made a mistake and passed an undefined value to setHp. Check for places where you have used those variables, e.g. in battle events or skills: the nth entry of a number, e.g. 123[0], is undefined.
Unfortunately the stack trace does not go back far enough to see what call may have triggered the problematic call. You could try to use
Error.stackTraceLimit = Infinity;
to increase the number of lines.

If you are sure that these event scripts caused the error a possible explanation might be that one of the actor had so far not been used in this savestate and is freshly created. Even with that possibility initializing the _hp property is one of the first thing that should happen before any refreshes should be needed, so caethyril may be on to something here.
@Andar There is no common case for the crash. This is the first and, so far in this playthrough, the only battle which has this setup as I am keeping it for boss battles only. Sometimes it happens and sometimes it doesn't.

the crash is saying the battle scene is trying to refresh a battler that doesn't exist; for
This might be the clue. At this stage I only have 3 actors in the party. Because I want to reuse that common event I have put in all actors. Could that be what's undefined? But why does it crash some times and not others with the identical setup?

Check for places where you have used those variables, e.g. in battle events or skills
I haven't used these variables for anything else. They were created specifically for this common event and have not been used otherwise.

You could try to use
Error.stackTraceLimit = Infinity;
to increase the number of lines.
Where would I put that line and at what point? Would it be something that I would put in the console at the beginning of each playtest?
Where would I put that line and at what point? Would it be something that I would put in the console at the beginning of each playtest?
That should work.
This might be the clue. At this stage I only have 3 actors in the party. Because I want to reuse that common event I have put in all actors. Could that be what's undefined?
No, the actors will be defined when the game boots. I was suggesting, as I said, an invalid troop member; but @caethyril is right, the clamp is a property of the hp, not the battler.

If it were a nonexistent battler, it would've crashed earlier when trying to read stateResistSet() of undefined.

Is there any place in the rest of that event where you set anything's hp equal to something?

If you check the troops that you could be encountering, are there any enemies that have a blank entry in the database for their HP? I'm not sure if the editor will even let you do that.

Did you check the stuff caethyril listed?
In order to pinpoint the issue as quickly as possible, I suggest Kes should add a line before that call to the clamp function. It could be something like this:

JavaScript:
// this one if name is a member of the actor's DB hash
console.log(this.name);
// or this one if name's an actual function
console.log(this.name());

Or it could use the id function instead. The point is that it will quickly tell Kes who got the wrong hp value at that point. There's a chance Kes will find many entries listed in the console window, but only the most recent one would matter here.
My Internet is down so I'm having to reply on my phone, so a bit terse.
@caethyril checked and those variables not used anywhere else.

@ATT_Turan I've done all the checks requested.

I have now run that battle event 15 times without a crash. Like I said, the error is intermittent so very hard for me to solve.
I think @kyonides' idea is a good one. Stick a console log in there - it'll produce a metric butt-load of spam, but you only care about the last one above the error trace next time it crashes.

The second version, function call with the parentheses, is the correct syntax for MZ.
My Internet is down so I'm having to reply on my phone, so a bit terse.
@caethyril checked and those variables not used anywhere else.

@ATT_Turan I've done all the checks requested.

I have now run that battle event 15 times without a crash. Like I said, the error is intermittent so very hard for me to solve.
 
In the tutorial that you've linked, those variables do get used again, a little further down in the common event.

Can you please show your full event?
@Arthran Sorry for the delay in replying but I had to wait for the internet to come back on before I could post screenshots.

Screenshot 2023-12-08 201245.png


Screenshot 2023-12-08 201305.png

Screenshot 2023-12-08 201417.png


When I said that I hadn't used the variables anywhere else, I meant anywhere else other than in this common event. I should have been more explicit.
The battle event only has one item before the Battle Processing command and that is to call a common event.
Piling on, the tutorial only calls this common event at turn 0 during the battle, not before the Battle Processing command.
If you are calling the common event before the battle begins I would expect this to crash if you already did another battle during the same session that does not use this continue feature (the turn counter would not yet be reset from the previous battle, so the common event would jump ahead past the conditional branch instead).

From what I've seen there is little reason not to split your common event into 2, since you'd always want to either save or restore your values.
@Another Fen In my time zone it's getting late and my head is starting to pack up for the evening. Can I just check that I have understood what you are suggesting.

Remove the If Turn count = 0 from the common event and instead call the common event from the Troop event.

When you say split the common event into 2, do you mean at the point where I have 'Exit Event Processing'? I would then call the rest in the new common event when the player chooses whether to continue or retry.
@Another Fen In my time zone it's getting late and my head is starting to pack up for the evening. Can I just check that I have understood what you are suggesting.

Remove the If Turn count = 0 from the common event and instead call the common event from the Troop event.

When you say split the common event into 2, do you mean at the point where I have 'Exit Event Processing'? I would then call the rest in the new common event when the player chooses whether to continue or retry.
Either leave the common event as-is, but call it from the Troop event (Condition: Turn 0, Span: Battle) instead of before the battle, *or* remove the conditional check and split the common event into two common events, calling the first one either before battle or at the very start of battle, and the second one whenever the skill is used.

If you opt for the second option, your two common events would look like this:

Common Event 1 (the removal of the If conditional is intentional):
1702077465596.png


Common Event 2:
1702077552493.png

1702077613967.png


The second option exemplifies better coding practices, as putting everything into one common event is kinda weird.
@Another Fen and @Arthran

This was indeed the cause and solution.
When I was endlessly trying to recreate the crash I was starting from a save file - so by definition I couldn't have the condition for the problem so never had the crash.

I have tested it both with and without a previous battle in the same session and it all flows perfectly.

I am deeply grateful to you both as I was on the verge of having to delete this mechanic and now I can leave it in.

Thank you, and thanks also to @Andar , @ATT_Turan and @caethyril because your comments have helped me understand better how things work.

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/script-call-causes-clamp-crash.163686/