Post Reply 
 
Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tutorial Basic GSC Coding: Foreach, Continue and Return
04-02-2012, 20:00 (This post was last modified: 04-04-2012 08:35 by Yamato.)
Post: #1
Basic GSC Coding: Foreach, Continue and Return
Hello

This is the last Basic GSC coding tutorial, I´ll 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
Related links

[Image: veovuq.png]
Find all posts by this user
Add Thank You Quote this message in a reply
[-] The following 2 users say Thank You to Yamato for this post:
Bloodfocus (04-02-2012), tsoPANos (04-03-2012)
04-02-2012, 20:25 (This post was last modified: 04-02-2012 20:27 by zxz0O0.)
Post: #2
RE: Basic GSC Coding: Foreach, Continue and Return
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]
Find all posts by this user
Add Thank You Quote this message in a reply
[-] The following 1 user says Thank You to zxz0O0 for this post:
Yamato (04-04-2012)
04-02-2012, 20:34
Post: #3
RE: Basic GSC Coding: Foreach, Continue and Return
OMA
Find all posts by this user
Add Thank You Quote this message in a reply
[-] The following 1 user says Thank You to The Clay Man for this post:
Yamato (04-04-2012)
04-02-2012, 23:48
Post: #4
RE: Basic GSC Coding: Foreach, Continue and Return
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();
}

Related links

[Image: 30xhrep.png]

A casual conversation between barata and I about Nukem.
Find all posts by this user
Add Thank You Quote this message in a reply
[-] The following 1 user says Thank You to master131 for this post:
Yamato (04-04-2012)
04-03-2012, 00:51
Post: #5
RE: Basic GSC Coding: Foreach, Continue and Return
(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.
Visit this user's website Find all posts by this user
Add Thank You Quote this message in a reply
04-03-2012, 05:44
Post: #6
RE: Basic GSC Coding: Foreach, Continue and Return
(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.
Find all posts by this user
Add Thank You Quote this message in a reply
04-04-2012, 08:33 (This post was last modified: 04-04-2012 08:36 by Yamato.)
Post: #7
RE: Basic GSC Coding: Foreach, Continue and Return
(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.

[Image: veovuq.png]
Find all posts by this user
Add Thank You Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  [Tutorial] Basic GSC Coding: Making your second code Yamato 9 740 12-28-2012 13:30
Last Post: Yamato
  [Tutorial] Basic GSC Coding: Making your third code Yamato 2 565 04-20-2012 23:04
Last Post: SuperNovaAO
  [Tutorial] Basic GSC Coding: Building your first mod and your first code Yamato 1 1,249 04-05-2012 17:53
Last Post: Pozzuh
  [Tutorial] Basic GSC Coding: Operations Yamato 0 680 04-04-2012 13:07
Last Post: Yamato
  [Tutorial] Basic GSC Coding: Switch Yamato 6 431 04-03-2012 18:32
Last Post: tsoPANos
  [Tutorial] Basic GSC Coding: If, Else and Else If Yamato 2 498 03-30-2012 10:47
Last Post: alvarogt95
  [Tutorial] Basic GSC Coding: Downloading and Installing a mod Yamato 1 541 03-30-2012 10:42
Last Post: alvarogt95
  [Tutorial] Basic GSC Coding: For Yamato 4 424 03-26-2012 18:39
Last Post: tsoPANos
  [Tutorial] Basic GSC Coding: While, Break and Wait Yamato 6 516 03-06-2012 13:48
Last Post: Yamato
  [Tutorial] Basic GSC Coding: Functions Yamato 10 731 03-05-2012 00:04
Last Post: alvarogt95

Forum Jump:


User(s) browsing this thread: 1 Guest(s)
Media Embeding by Simple Audio Video Embeder