I'm getting a 'Infinite Loop' / 'Infinite Recursion' crash when trying to repeatedly call a method after a button press
by Randy Lutcavich · in Torque X 2D · 10/06/2009 (3:05 am) · 5 replies
It isn't an infinite loop. I just want it to act as if the player is holding the button and call the method every tick until an object has fully rotated.
Why would this be considered an infinite loop:
Error:
An unhandled exception of type 'System.StackOverflowException' occurred in GarageGames.TorqueX.Framework.dll
I've tried this many different ways but any time I have a call to the method that I am currently in the game will crash with the same error. If there is a test to stop the loop then how can this always be considered an infinite loop??
My ultimate goal is to be able to press a button and have it doing a task repeatedly on each tick. If someone knows a way to do this with a For loop without having to call the method each tick then please let me know. Thanks.
Why would this be considered an infinite loop:
if (move != null && move.Buttons[3].Pushed == true)
{
//_FireGreen();
_testRot();
}
protected void _testRot()
{
T2DSceneObject GreenDot = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("GreenDot");
if (GreenDot.Rotation < 360)
{
GreenDot.Rotation = GreenDot.Rotation + 10;
_testRot();
}
if (GreenDot.Rotation > 180)
{
//Loop is ending seeeeee. Totally not an infinite loop!
}
}Error:
An unhandled exception of type 'System.StackOverflowException' occurred in GarageGames.TorqueX.Framework.dll
I've tried this many different ways but any time I have a call to the method that I am currently in the game will crash with the same error. If there is a test to stop the loop then how can this always be considered an infinite loop??
My ultimate goal is to be able to press a button and have it doing a task repeatedly on each tick. If someone knows a way to do this with a For loop without having to call the method each tick then please let me know. Thanks.
About the author
Recent Threads
#2
Here is my real code:
If I place the _FireGreen(); call in the same brackets as the if statement that relies on the _greenFirePasses condition then it won't crash but for some reason it also doesn't loop.
I basically want the object to rotate a full 360 and then stop the loop.
10/06/2009 (4:19 am)
Ah I must be really tired. That was a bad example. Here is my real code:
protected void _FireGreen()
{
if (_greenDelayRemaining <= 0 && _greenActive == true && _greenFirePasses > 0)
{
int randomGreen = TorqueUtil.GetRandomInt(1, 5);
if (randomGreen == 1 && _greenFireRot >= 0)
{
...
}
if (randomGreen == 2 && _greenFireRot >= 0)
{
...
}
if (randomGreen == 3 && _greenFireRot >= 0)
{
...
}
if (randomGreen == 4 && _greenFireRot >= 0)
{
...
}
if (randomGreen == 5 && _greenFireRot >= 0)
{
...
}
if (_greenFireRot == 0)
{
_greenFireRot = 360;
}
_greenDelayRemaining = 0.05f;
_greenFirePasses = _greenFirePasses - 1;
}
if (_greenActive == false)
{
...
}
_FireGreen();
}This above code (but with all of the ...'s filled in) crashes with the infinite loop exception every time it calls _FireGreen();If I place the _FireGreen(); call in the same brackets as the if statement that relies on the _greenFirePasses condition then it won't crash but for some reason it also doesn't loop.
I basically want the object to rotate a full 360 and then stop the loop.
#3
Hmm let me ponder this and see if I can find a solution.
10/06/2009 (4:20 am)
Ah nevermind. I see my problem here. The conditional statement is failing for the other variables.Hmm let me ponder this and see if I can find a solution.
#4
Thanks for your help. This is about solved.
I'm just not seeing a way to continually call the method but only rotate the object once per tick. I tried reworking it to add in another conditional statement that only processed the rotation after a delay but then I had to increase how many times the method loops for that to have any real affect but that increase causes the infinite loop crash again.
I know I am just thinking about this all wrong. I might just need to come back to it.
10/06/2009 (4:31 am)
That makes sense that it would crash there because no matter what the conditional statement decides, the _FireGreen(); will always execute.Thanks for your help. This is about solved.
I'm just not seeing a way to continually call the method but only rotate the object once per tick. I tried reworking it to add in another conditional statement that only processed the rotation after a delay but then I had to increase how many times the method loops for that to have any real affect but that increase causes the infinite loop crash again.
I know I am just thinking about this all wrong. I might just need to come back to it.
#5
If you are wanting a loop, use a loop. Do a Do...While or something.
Alternately, if you want it to rotate just once per tick, how about playing with it's angular velocity?
10/07/2009 (6:27 am)
The main reason you are getting the infinite recursion, is that you are getting an infinite recursion by calling _FireGreen in _FireGreen. As it is now, if thats the code you are using, it will call no matter what as it's not in a conditional and there doesn't appear to be any return calls before it. If you are wanting a loop, use a loop. Do a Do...While or something.
Alternately, if you want it to rotate just once per tick, how about playing with it's angular velocity?
Torque Owner Jake Davis