Game Development Community

While loop that isn't endless

by Teromous · in Torque Game Builder · 03/01/2008 (11:31 am) · 2 replies

Hey guys, I'm trying to make a piece of code that will loop 10 times calling a schedule every 3 seconds. So really the code would be running for 30 seconds. I'm using it as a poison script, but every time I do a while loop I get an endless loop:

function PoisonTick(%this)
{
%this.PTick=10;
while(%this.PTick>0)
{
{
schedule(3000, %this, "PoisonDamage", %this);
}
%this.PTick--; // remove one from PTick
}
}


Everything works code-wise outside of the loop if I do this:

function PoisonTick(%this)
{
schedule(3000, %this, "PoisonDamage", %this);
}

...but I would really like to loop it.

#1
03/01/2008 (1:50 pm)
Try this

function poisonTick(%objThatIsPoisoned)
{
   // if we don't have a poisoned object we can stop here
   if (!isObject(%objThatIsPoisoned))
      return;

   // make sure we aren't applying the poison more than once per tick
   if (isEventPending(%objThatIsPoisoned.poisonEvent))
      cancel(%objThatIsPoisoned.poisonEvent);
   
   // if this object is still poisoned, then tick
   if (%objThatIsPoisoned.poisonAmount > 0)
      %objThatIsPoisoned.poisonAmount--;
      
   // do something
   echo(%object.getName() SPC "is poisoned for" SPC %objThatIsPoisoned.poisonAmount);
      
   // reschedule if we are still poisoned
   if (%objThatIsPoisoned.poisonAmount > 0)
      %objThatIsPoisoned.poisonEvent = schedule(3000, %objThatIsPoisoned, poisonTick, %objThatIsPoisoned);
   else
      noLongerPoisoned(%objThatIsPoisoned);
}

function noLongerPoisoned(%object)
{
   // do something
   echo(%object.getName() SPC "is healthy again!");
}

function poisonObject(%objToBePoisoned, %amountOfPoison)
{
   %objToBePoisoned.poisonAmount += %amountOfPoison;
   
   // make sure we aren't applying the poison more than once per tick
   if (isEventPending(%objToBePoisoned.poisonEvent))
      cancel(%objToBePoisoned.poisonEvent);
   
   // schedule the poison event
   %objToBePoisoned.poisonEvent = schedule(3000, %objToBePoisoned, poisonTick, %objToBePoisoned);
}


Use it like this:
%myObject = new SimObject(MyObject); // or whatever object you have
poisonObject(%myObject, 3);
#2
03/01/2008 (3:17 pm)
Ah I see what you're saying. Instead of having a while loop, have the second function call the first which has the schedule. That way the two functions are playing ping-pong with each other instead of holding up one function with a "while".

I tried something similar to what you wrote and it worked. Thanks for the help!!