wait(1) in a loop

Posted By: Freddy_dup1

wait(1) in a loop - 08/07/06 12:38

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?
Posted By: Anonymous

Re: wait(1) in a loop - 08/07/06 12:49

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
Posted By: Freddy_dup1

Re: wait(1) in a loop - 08/07/06 14:38

Thanks.
So I can use a loop without a "wait".
That's very useful when placing sprites.
Posted By: ulillillia

Re: wait(1) in a loop - 08/07/06 14:50

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.
Posted By: Lion_Ts

Re: wait(1) in a loop - 08/07/06 22:25

Freddy, running functions in the 3DGS like tasks in a cooperative multitasking environment (old versions of Windows). Cooperatively multitasked system must rely on each process to regularly give time to other processes on the system. A poorly designed task, or a "hung" process, can effectively bring the system to a halt.
Shure, You may use long loops without wait (for setup your level, for example). But those loops must be endable.
Posted By: Freddy_dup1

Re: wait(1) in a loop - 08/08/06 19:36

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.
Posted By: Lion_Ts

Re: wait(1) in a loop - 08/08/06 22:35

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.
© 2024 lite-C Forums