Delay a For Loop
by Tom Lenz · in Torque Game Builder · 02/23/2008 (10:05 am) · 4 replies
I am currently working on a tower defense game and was wondering if there is a way to delay anything. Currently I have a wave of enemies that I want to send down a path. The problem is that when I attach the enemies to the path they are all on top of each other. Right now I have:
{
for(%i = 0; %i < 20; %i++)
{
path.attachObject($attackers.getObject(0));
}
So I guess is there a way to delay the for loop from looping sooner than I want.
Thanks
{
for(%i = 0; %i < 20; %i++)
{
path.attachObject($attackers.getObject(0));
}
So I guess is there a way to delay the for loop from looping sooner than I want.
Thanks
#2
02/23/2008 (2:16 pm)
Thanks that works great, I'll have to remember to do that for a few other things too. Can you just explain why you need to have the "%spawnDelay = (%i * %delayInterval);" That part is just a little confusing.
#3
You'd get all of your objects spawned 200 milliseconds into the future, and all at the same time. Which is not much different than how you had it originally, they all spawned 200 milliseconds earlier is the only difference.
However, with %spawnDelay = (%i * %delayInterval)
You get the first object spawned immediately. The 2nd one is spawned 200 milliseconds after the first. The 3rd one will spawn 400 milliseconds after the 1st one, and 200 milliseconds after the 2nd one, and so on.
Each itteration through the FOR loop is increasing the delay by 200 milliseconds so that the next object in line is delayed from the previous one by 200.
I hope that's clear, I understood what I meant and typed what I thought and sometimes the two conflict with what you read.
03/03/2008 (6:27 pm)
If it was left: %spawnDelay = (%delayInterval)You'd get all of your objects spawned 200 milliseconds into the future, and all at the same time. Which is not much different than how you had it originally, they all spawned 200 milliseconds earlier is the only difference.
However, with %spawnDelay = (%i * %delayInterval)
You get the first object spawned immediately. The 2nd one is spawned 200 milliseconds after the first. The 3rd one will spawn 400 milliseconds after the 1st one, and 200 milliseconds after the 2nd one, and so on.
Each itteration through the FOR loop is increasing the delay by 200 milliseconds so that the next object in line is delayed from the previous one by 200.
I hope that's clear, I understood what I meant and typed what I thought and sometimes the two conflict with what you read.
#4
03/03/2008 (7:11 pm)
Thanks
Torque 3D Owner Stephen Zepp
Try something like this:
%delayInterval = 200; // in milliseconds %randomInterval = 200; // same, in millseconds %spawnDelay = 0; // for stacked spawning in case neither of the lines below are uncommented for (%i = 0; %i < 20; %i++) { // uncomment one of the below lines (but not both) depending on which style you want, random or interval // %spawnDelay = (getRandom(%randomInterval) -1); // %spawnDelay = (%i * %delayInterval); path.schedule(%spawnDelay, attachObject, $attackers.getObject(0)); }By the way, there is a flaw in your loop in any case--it's actually only ever attaching the first object in your $attackers SimSet (I'm assuming you have 20 attackers in that set), because you are using getObject(0). You probably meant to use getObject(%i).
If my assumption is true what you really want your code to look like would be:
%delayInterval = 200; // in milliseconds %randomInterval = 200; // same, in millseconds %spawnDelay = 0; // for stacked spawning in case neither of the lines below are uncommented for (%i = 0; %i < 20; %i++) { // uncomment one of the below lines (but not both) depending on which style you want, random or interval // %spawnDelay = (getRandom(%randomInterval) -1); // %spawnDelay = (%i * %delayInterval); path.schedule(%spawnDelay, attachObject, $attackers.getObject(%i)); }