PDA

View Full Version : Recursive Loops



andi06
June 25th, 2015, 07:49 AM
I recently wrote this script to run an animation. I needed a way of switching it on for testing in Surveyor so I just listened for Junction,Toggled of a specific junction nearby.



class temp isclass MapObject {

bool state = false;

public void Init (Asset self) {
inherited(self);
AddHandler(me,"Junction","Toggled","OnStateChange");
}

void OnStateChange(Message msg) {
Junction junction = cast<Junction>(msg.src);
if (junction and junction.GetName() == "xxx") {
state = !state;
SetMeshAnimationState("indicator", state);
}
}

};


Before I added the line in red the script ran on any Junction,Toggled (including the one that is caused by the message handler). The result was an endless loop which hung Trainz. Perfectly predictable of course but I thought that we now had a mechanism that closed things down if scripts were using excessive resources. Is this sort of loop not included in that?

WindWalkr
June 25th, 2015, 09:09 AM
Before I added the line in red the script ran on any Junction,Toggled (including the one that is caused by the message handler). The result was an endless loop which hung Trainz. Perfectly predictable of course but I though that we now had a mechansim that closed things downn if scripts were using excessive resources. Is this sort of loop not included in that?

There's very little in the way of protection against effective hangs. There is a per-script timeout which will catch trivial cases, but it's expressed in terms of opcodes retired rather than time elapsed, so if you manage to do something very slow with very few script opcodes, you can still effectively hang the game. Likewise, there are various mechanisms such as the one you've highlighted, whereby no single script execution reaches the opcode timeout, yet the game as a whole is still unable to progress to the next frame.

chris