The importance of loops

You must understand the Game Editor concepts, before post here.

The importance of loops

Postby DST » Thu Feb 05, 2009 5:25 pm

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!
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: The importance of loops

Postby Game A Gogo » Thu Feb 05, 2009 11:16 pm

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 :)
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: The importance of loops

Postby Bee-Ant » Sat Feb 07, 2009 12:36 pm

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...
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: The importance of loops

Postby DST » Sat Feb 07, 2009 9:39 pm

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
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: The importance of loops

Postby jimmynewguy » Sun Feb 08, 2009 4:48 pm

is there a way of a loop where it does the action after looping x amount of times? thanx in advance :D
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: The importance of loops

Postby DST » Sun Feb 08, 2009 5:04 pm

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.
Last edited by DST on Mon Mar 23, 2009 2:51 am, edited 1 time in total.
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: The importance of loops

Postby jimmynewguy » Sun Feb 08, 2009 6:31 pm

excellent! Thanx
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest