1 registered members (AndrewAMD),
827
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
wait(1) in a loop
#84958
08/07/06 12:38
08/07/06 12:38
|
Joined: Feb 2003
Posts: 211 Unna
Freddy_dup1
OP
Member
|
OP
Member
Joined: Feb 2003
Posts: 211
Unna
|
Hello, when I learned C-Script I learned that I should always put “wait(1);” into a loop to prevent an endless loop. Some time ago I tried to use sprites as grass in one of my levels but it took too long to place them. So it never worked. Then I found a post in this forum where someone made it work. He did it exactly like me but he left out the “wait(1);”. Can I leave it out, when I want to create many sprites? (When using “effect“ the manual says: “don’t use wait”) When can I leave it out and when do I have to use it?
|
|
|
Re: wait(1) in a loop
[Re: Freddy_dup1]
#84959
08/07/06 12:49
08/07/06 12:49
|
Anonymous
Unregistered
|
Anonymous
Unregistered
|
If a loop has an defined end/exit and you do not want to see someting happening in your loop, you do not need a wait(). max_loops defines the max. loops without a wait(). wait "waits" for the next screen-refresh.
mercuryus
|
|
|
Re: wait(1) in a loop
[Re: Freddy_dup1]
#84961
08/07/06 14:50
08/07/06 14:50
|
Joined: Feb 2003
Posts: 6,818 Minot, North Dakota, USA
ulillillia
Senior Expert
|
Senior Expert
Joined: Feb 2003
Posts: 6,818
Minot, North Dakota, USA
|
wait(1) should mainly be used in loops that never become false or where the results need to be processed once each frame. To, for example, bulk-set array values with patterns, you'd use a loop without a wait. My 2D game's save/load system doesn't have this and I have one loop that runs over 7500 times, which creates a backup of a previous save file. Without the wait(1), it takes but a fraction of a second so minute, the human mind is completely unable to pick it out. If I did use it, you'd have to wait over 2 minutes for it to save the backup, let alone the actual contents which would add another 40 seconds or so. Waiting 2 1/2 minutes to save a game makes no sense, but under a millisecond does make sense. As an example, to bulk set array values with patterns, you'd have something like this: Code:
var array_with_pattern[21];
function bulk_set_array() { var array_index = 0; // a temporary variable to work with the array // breakpoint; while(array_index < 21) // 21 is array size { array_with_pattern[array_index] = pow(array_index+1, 2)-1; // a pattern array_index += 1; } }
Try running this code and see what happens. This is 20 loops. It's done all within one frame and you don't get the "endless loop" error (provided I didn't make a stupid mistake). You can tell if the code is running or not by simply uncommenting the breakpoint line which activates the debugger and step through it line by line. You should see it starting with 0, 1, 3, 7, 15, 31, 63, and going on clear until you see 2097151 from which the loop becomes false. Edit: In my 2D game, if you set the time control to 2x time (press the W key), the game's back end functions run twice in one frame. At 64x time (in the full edition only), a loop is processed 64 times in one frame with a wait after it within if statements. Even with that, it's barely even registering anything in Windows Task Manager for CPU usage, 2% on average. My code is far from optimized and I have numerous cases of using my complex algorithms to work around the limits of the var.
Last edited by ulillillia; 08/07/06 14:56.
"You level up the fastest and easiest if you do things at your own level and no higher or lower" - useful tip
My 2D game - release on Jun 13th; My tutorials
|
|
|
Re: wait(1) in a loop
[Re: Lion_Ts]
#84963
08/08/06 19:36
08/08/06 19:36
|
Joined: Feb 2003
Posts: 211 Unna
Freddy_dup1
OP
Member
|
OP
Member
Joined: Feb 2003
Posts: 211
Unna
|
Thanks. This is what I have done: Code:
(default of max_loops is 5000)
var dust_num=0; var count_1=0;
function set_dust(){ while(1){
while(dust_num<40){ count_1+=1; if(count_1>=4500){wait(1);count_1=0;}
temp.x=random(screen_size.x); temp.y=random(screen_size.x); temp.z=400+random(50); vec_for_screen(temp.x,camera); create(<dust1.bmp>,temp.x,dust);dust_num+=1;}
wait(1);}} "count_1" will call "wait(1);" if the loop looped 4500 times. But the dust sprites are not disappearing all the time so new ones don't have to be created all the time. "count_1" is not necessary but maybe it's saver.
Last edited by Freddy; 08/08/06 19:37.
|
|
|
Re: wait(1) in a loop
[Re: Freddy_dup1]
#84964
08/08/06 22:35
08/08/06 22:35
|
Joined: Aug 2005
Posts: 1,185 Ukraine
Lion_Ts
Serious User
|
Serious User
Joined: Aug 2005
Posts: 1,185
Ukraine
|
Sorry, but looks like a bad planned code. You don't need the count_1 var. Do you want the set_dust function running all the time? If so, do you remove sprite in the DUST action with dust_num decrementing? Code:
var dust_num=0; action dust { while(!player){wait(1);}//wait for a player in the level while(vec_dist(my.x, player.x)<500){wait(1);}//wait while player near me dust_num -= 1; ent_remove(me); } function set_dust() { while(1) { while(dust_num > 40) {wait(1);}//wait while no free places temp.x=random(screen_size.x); temp.y=random(screen_size.x); temp.z=400+random(50); vec_for_screen(temp.x, camera); create(<dust1.bmp>, temp.x, dust); dust_num += 1; } }
If no, for initial dust setup, You have not to use while(1) at all. Code:
function set_dust() { while(dust_num < 40) { temp.x=random(screen_size.x); temp.y=random(screen_size.x); temp.z=400+random(50); vec_for_screen(temp.x, camera); create(<dust1.bmp>, temp.x, dust); dust_num += 1; } }
P.S. Search for a 'grass following the player', 'placing grass' or alike in the contrib or tut thread.
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|