Page 1 of 1

The importance of loops

PostPosted: Thu Feb 05, 2009 5:25 pm
by DST
Once you've decided to clone actors rather than creating new ones, you will find loops to be invaluable.

Here is a basic loop structure:
Code: Select all
int i;
for(i=0; i<10; i++)
{
action here;
}


note that int declarations (int i;) must come at the very beginning of the script window.

Now if our action is:
CreateActor('block", "block1", "no parent", "no path", i*50, 0, true);

then the loop will run till its finished (i<10; so when i reaches 10, the loop stops).
And create an actor every 50 pixels.(50*i)

You can avoid having lots of clones in your editor mode by creating clones in game using loops.

Likewise, it makes sorting things much easier;

Code: Select all
int i;
for(i=0; i<10; i++)
{
if(blockcolor[i]>0){
do this;
}
}


This loop will check for any values in the array blockcolor[] and then you can use the output as you see fit.
You can also run that same loop, with the action
blockcolor[i]=0; and reset every cell in the array to 0.

Loops help you create, edit, and manage large groups of clones with ease.

Best of luck to you!

Re: The importance of loops

PostPosted: Thu Feb 05, 2009 11:16 pm
by Game A Gogo
also if you go in file processing stuff, loop is a load off the back, it's fun creating a blank image like this:
Code: Select all
int i,j;
for(i=0;i<255;i++)
{
    for(j=0;j<255;j++)
    {
        red[i][j]=1;
        blue[i][j]=1;
        green[i][j]=1;
    }
}


It's easy to go into square and cubic (Or beyond) like data processing with loops :)

Re: The importance of loops

PostPosted: Sat Feb 07, 2009 12:36 pm
by Bee-Ant
And dont forget to put the
Code: Select all
int i;   //or any other variables declaration

on the topmost of your code or else your srcipt would error...

Re: The importance of loops

PostPosted: Sat Feb 07, 2009 9:39 pm
by DST
You can nest these loops too, like this:

int i;
int j;
int k=ActorCount("enemy");
int l=ActorCount("missile");
for (i=0; i<k; i++){
if(healths[i]>0){

for (j=0; j<l; j++){
if missilex[j] > enemyx[i]){ //<-- notice how i'm still using i in the j loop, because the entire j loop
//is inside one instance of the i loop.
target[j]=i; //assign target i
j=l+1; //don't repeat the j loop after this
i=k+1; //don't repeat the i loop (target was found, and i has to be smaller
//than k for the loop to run).
} //end 'if missilex[j]' check
} //end j loop

} //end 'ifhealths[i]>0' check
} //end i loop

Re: The importance of loops

PostPosted: Sun Feb 08, 2009 4:48 pm
by jimmynewguy
is there a way of a loop where it does the action after looping x amount of times? thanx in advance :D

Re: The importance of loops

PostPosted: Sun Feb 08, 2009 5:04 pm
by DST
yes, just increment a solid variable (not i or j).

int i;
for (i=0; i<10; i++){
if(i==9){
limit+=1;
if(limit>20){
do this;
}
}
}

Now the variable 'limit' is counting how many times the loop has been run.

Re: The importance of loops

PostPosted: Sun Feb 08, 2009 6:31 pm
by jimmynewguy
excellent! Thanx