while loop IN another while loop

Posted By: mEnTaL

while loop IN another while loop - 10/11/10 19:53

I have a while loop #1 which has wait time 1 step and while loop #2 which has wait time 2 seconds and must stay INSIDE while loop#1, . And here comes the problem: the engine executes WhileLoop #1 properly, but doesn't wait 2 seconds for "#2" and executes it every frame instead!

here is a code which examinates this:

Code:
function shooting()
{
while(1)// this is while loop #2 
{
  create_bullet();
  wait(-2);
}

}


action npc()
{

while(1) //this is while loop #1 
{
  //do some stuff constantly 
  shooting(); //THIS little function is executed every frame instead of 2 sec interval >: ( 
 wait(1);
}

}


So how can I manage to execute properly while loop with 2 sec wait() time inside a while loop with 1 frame wait time?
Posted By: Pappenheimer

Re: while loop IN another while loop - 10/11/10 20:00

action npc()
{
var counter = 0;
var shooting_time = 20;//don't know the number that you need for 2 seconds
while(1)
{
counter += time_step;
if(counter > shooting_time)
{
create_bullet();
counter = 0;
}
wait(1);
}
}
Posted By: mEnTaL

Re: while loop IN another while loop - 10/11/10 20:38

Great. Thx man
Posted By: Joey

Re: while loop IN another while loop - 10/11/10 20:38

Code:
action npc()
{
  shooting(); //place outside the inner while loop, will start a new coroutine
while(1) //this is while loop #1 
{
  //do some stuff constantly 
 wait(1);
}
}


Posted By: Aku_Aku

Re: while loop IN another while loop - 10/11/10 22:22

Another way maybe more precise, use the built-in sys_seconds variable to detect the elapsed time, in your case 2 secs.
© 2024 lite-C Forums