For Loop Headaches
by Gina-Marie -Netjera- Hammer · in Torque Game Builder · 03/24/2005 (3:57 am) · 3 replies
I'm trying to use a for loop to schedule enemies and to determine when all the enemies have deployed. After all enemies have deployed, the level should end with a "Level End" screen.
Here's the code I'm using:
These are just two parts of the function.
Here's my problem. The for loop iterates, schedules all of the enemies, and exits the loop before all of the enemies have been deployed. So essentially, I have a "Start Level" screen, and then it immediately goes to the "End Level" screen, even though I can still see the particle effects from the ship in the background. This brings me to two questions:
1) Is there some way to pause the for loop, so that it doesn't iterate again until after the enemy has been deployed (not just scheduled);
2) If not, is there some way to check to see if all the enemies have been deployed (something along the lines of "if instance_exists" if anyone here has used GML)?
Thanks!
Here's the code I'm using:
// Spawn all the enemies
for (%i=0; %i< $nEnemies; %i++)
{
// If this is the first enemy of the level, create an enemy
if (%i == 0)
{
createEnemy();
}
// If this is not the first enemy of the level, wait a bit, then create another one
else
{
schedule(2000, 0, "createEnemy()");
}
}
// End the level
if (%i == $nEnemies)
{
$player.setVisible( false );
playerMap.pop();
showLevelEnd();
}These are just two parts of the function.
Here's my problem. The for loop iterates, schedules all of the enemies, and exits the loop before all of the enemies have been deployed. So essentially, I have a "Start Level" screen, and then it immediately goes to the "End Level" screen, even though I can still see the particle effects from the ship in the background. This brings me to two questions:
1) Is there some way to pause the for loop, so that it doesn't iterate again until after the enemy has been deployed (not just scheduled);
2) If not, is there some way to check to see if all the enemies have been deployed (something along the lines of "if instance_exists" if anyone here has used GML)?
Thanks!
About the author
Art, Games, Life www.netjera.com See my in-progress website and game design work at: www.netjersoft.com!
#2
That would spawn one enemy when you first call the function, then another enemy every 2 seconds. then 2 seconds after the last enemy was spawned the end level will execute. You could add additional checks/schedules to only end the level when all enemies have been destroyed (depending on how your game works) in there as well.
03/24/2005 (4:55 am)
Maybe you should split this out into multiple calls (off the top of my head so excuse any errors)$maxEnemies = 10;
$enemiesSpawned = 0;
function spawnEnemy()
{
if ($enemiesSpawned < $maxEnemies)
{
createEnemy();
$enemiesSpawned++;
// spawn next enemy in 2 seconds
schedule(2000, 0, "spawnEnemy()");
}
else
{
$player.setVisible( false );
playerMap.pop();
showLevelEnd();
}
}That would spawn one enemy when you first call the function, then another enemy every 2 seconds. then 2 seconds after the last enemy was spawned the end level will execute. You could add additional checks/schedules to only end the level when all enemies have been destroyed (depending on how your game works) in there as well.
#3
A) Spawn a new enemy every 2 seconds.
B) If the player is still alive when the last enemy is spawned, end the level.
Greg shows one way of doing it--remove the structure of the for loop itself, since a for loop is "all at once", and implement the logic based on how schedule works instead. Another way would be to keep the for loop in place, but not increment your $enemiesSpawned in this loop, but in the createEnemy() function. That way, you are only incrementing the number (just like in Greg's example) when the actual spawning occurs, not at the time of it being scheduled to occur.
03/24/2005 (6:05 am)
Greg's nudging you in the right direction--your main problem is that you are probably not fully aware yet of the simulation itself, and how it relates to scripting. For example, it appears based on your logic flow that what you want is:A) Spawn a new enemy every 2 seconds.
B) If the player is still alive when the last enemy is spawned, end the level.
Greg shows one way of doing it--remove the structure of the for loop itself, since a for loop is "all at once", and implement the logic based on how schedule works instead. Another way would be to keep the for loop in place, but not increment your $enemiesSpawned in this loop, but in the createEnemy() function. That way, you are only incrementing the number (just like in Greg's example) when the actual spawning occurs, not at the time of it being scheduled to occur.
Torque Owner Greg Lincoln
2. I think isObject() might be what you are looking for here.