Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
2 registered members (TipmyPip, 1 invisible), 18,758 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
while loop IN another while loop #343922
10/11/10 19:53
10/11/10 19:53
Joined: Jul 2009
Posts: 96
M
mEnTaL Offline OP
Junior Member
mEnTaL  Offline OP
Junior Member
M

Joined: Jul 2009
Posts: 96
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?

Re: while loop IN another while loop [Re: mEnTaL] #343924
10/11/10 20:00
10/11/10 20:00
Joined: Sep 2003
Posts: 5,900
Bielefeld, Germany
Pappenheimer Offline
Senior Expert
Pappenheimer  Offline
Senior Expert

Joined: Sep 2003
Posts: 5,900
Bielefeld, Germany
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);
}
}

Re: while loop IN another while loop [Re: Pappenheimer] #343929
10/11/10 20:38
10/11/10 20:38
Joined: Jul 2009
Posts: 96
M
mEnTaL Offline OP
Junior Member
mEnTaL  Offline OP
Junior Member
M

Joined: Jul 2009
Posts: 96
Great. Thx man

Re: while loop IN another while loop [Re: mEnTaL] #343930
10/11/10 20:38
10/11/10 20:38
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
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);
}
}



Re: while loop IN another while loop [Re: mEnTaL] #343933
10/11/10 22:22
10/11/10 22:22
Joined: Sep 2009
Posts: 1,032
Budapest
Aku_Aku Offline
Serious User
Aku_Aku  Offline
Serious User

Joined: Sep 2009
Posts: 1,032
Budapest
Another way maybe more precise, use the built-in sys_seconds variable to detect the elapsed time, in your case 2 secs.


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1