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:
"something" is a random name you give to each array variable, "array" is the array you are using. Real examples:
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:
Example:
The previous is the same as:
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:
Using a return can also force a function to continue running:
Real Example:
I hope it has helped. Thanks for reading.
@Yamato
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