Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by dr_panther. 05/18/24 11:01
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (7th_zorro, dr_panther), 724 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 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: 993
Budapest
Aku_Aku Offline
User
Aku_Aku  Offline
User

Joined: Sep 2009
Posts: 993
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 | chip programmers | 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