• 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tutorial] Basic GSC Coding: Foreach, Continue and Return
#1
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
  Reply
#2
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" );
}
[Image: azuw.jpg]
  Reply
#3
OMA
Away
  Reply
#4
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();
}
[Image: 30xhrep.png]

A casual conversation between barata and I about Nukem.
  Reply
#5
(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.
crAyon makes no warranty with respect to documents or other information available from this place, assumes no legal liability or responsibility whatsoever for the accuracy, completeness, or usefulness of any such information, and does not represent that its use would not infringe privately owned rights. crAyon disclaims all warranties, express and implied, including the warranties of merchantability and fitness for a particular purpose.
  Reply
#6
(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

[Image: 30xhrep.png]

A casual conversation between barata and I about Nukem.
  Reply
#7
(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.
  Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tutorial] Basic GSC Coding: Building your first mod and your first code Yamato 2 10,358 06-07-2016, 21:49
Last Post: FrostbytePG
  [Tutorial] Basic GSC Coding: Functions Yamato 15 11,353 08-17-2013, 10:17
Last Post: hamza-q99
  [Tutorial] Basic GSC Coding: Making your second code Yamato 9 7,843 12-28-2012, 13:30
Last Post: Yamato
  [Tutorial] Basic GSC Coding: Operations Yamato 0 7,540 04-04-2012, 13:07
Last Post: Yamato
  [Tutorial] Basic GSC Coding: Switch Yamato 6 5,576 04-03-2012, 18:32
Last Post: tsoPANos
  [Tutorial] Basic GSC Coding: If, Else and Else If Yamato 2 5,331 03-30-2012, 10:47
Last Post: alvarogt95
  [Tutorial] Basic GSC Coding: Downloading and Installing a mod Yamato 1 4,942 03-30-2012, 10:42
Last Post: alvarogt95
  [Tutorial] Basic GSC Coding: For Yamato 4 5,553 03-26-2012, 18:39
Last Post: tsoPANos
  [Tutorial] Basic GSC Coding: While, Break and Wait Yamato 6 6,886 03-06-2012, 13:48
Last Post: Yamato

Forum Jump:


Users browsing this thread: 1 Guest(s)