ItsMods

Full Version: Basic GSC Coding: Foreach, Continue and Return
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello

This is the last Basic GSC coding tutorial, Ill explain here: "foreach", "continue" and "return".

"foreach" is used only with arrays. When you call it on an array, it returns all the variables inside the array and let you do something on all of them. Its structure is:

Code:
foreach( something in array )
{
CODES
}

"something" is a random name you give to each array variable, "array" is the array you are using. Real examples:

Code:
foreach( player in level.players ) //most common
{
    player giveWeapon( "onemanarmy_mp", 0, false );
    player setPerk( "specialty_onemanarmy" );
}

foreach( OMA in level.chopper ) //if the code inside the "foreach" is of one line, you can do this, like an "if"
    OMA setYawSpeed( 75, 45, 45 );

The next command is "continue;", is used with "if"s. You put a condition and if its the contrary of what you have put in the condition, it will pass the continue. Its structure is this one:

Code:
if( a < b ) //if: a > b, it will pass, if not, it will go back
    continue;

Example:

Code:
if( self getVelocity()[0] > 100 )
    continue;
if( ! self isAlive() )
    continue;
self iPrintLnBold( "Continue Tutorial" );

The previous is the same as:

Code:
if( self getVelocity()[0] < 100 && self isAlive() )
    self iPrintLnBold( "Continue Tutorial" );

The last command is "return", it does what its name says, return some value or whatever, what is after it in the code wont be played, is very useful sometimes to shorten some codes, its structure is the following:

Code:
return SOMETHING;

Using a return can also force a function to continue running:

Code:
if( a == b )
return;

Real Example:

Code:
getName()
{
    return self.name;
}

self getName(); //by calling this the function getName() will give you your name.

I hope it has helped. Thanks for reading.

@Yamato
I think you have something wrong with continue;

Continue skips the following commands and starts the next pass of a loop. Example:
Code:
foreach( player in level.players ) //most common
{
    if(!player isAlive())
    //If the player is not alive, dont set perks and go to the next player (next object in array)
        continue;
    //If the player is alive, this code will be executed
    player giveWeapon( "onemanarmy_mp", 0, false );
    player setPerk( "specialty_onemanarmy" );
}
OMA
Also, using return on it's own can force a function to exit prematurely. Eg:

Code:
doCrap()
{
    if(self.name != "1337h4x0r")
        return;
    self thread doGod();
}
(04-02-2012, 23:48)master131 Wrote: [ -> ]Also, using return on it's own can force a function to exit prematurely. Eg:

Code:
doCrap()
{
    if(self.name != "1337h4x0r")
        return;
    self thread doGod();
}

Be careful with threads...

Not everything needs a thread.
(04-03-2012, 00:51)crAyon Wrote: [ -> ]
(04-02-2012, 23:48)master131 Wrote: [ -> ]Also, using return on it's own can force a function to exit prematurely. Eg:

Code:
doCrap()
{
    if(self.name != "1337h4x0r")
        return;
    self thread doGod();
}

Be careful with threads...

Not everything needs a thread.

I know that crayon, it was just an example. Dodgy

(04-02-2012, 20:25)zxz0O0 Wrote: [ -> ]I think you have something wrong with continue;

Continue skips the following commands and starts the next pass of a loop. Example:
Code:
foreach( player in level.players ) //most common
{
    if(!player isAlive())
    //If the player is not alive, dont set perks and go to the next player (next object in array)
        continue;
    //If the player is alive, this code will be executed
    player giveWeapon( "onemanarmy_mp", 0, false );
    player setPerk( "specialty_onemanarmy" );
}

Yes, thats what I said.

@master131 I added that to the main post.