• 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tutorial] Basic GSC Coding: If, Else and Else If
#1
Hello

Today I am going to explain the "if", "else if" and "else" command. The "if" checks if what its inside it is true or false and if its true, it does what its inside the "if". If what is inside the "if" is false and you have defined a "else", the program will run what you wrote in the else, this 3 commands are quite simple to use and understand, example:

Code:
if( a < b )
{
    c = 1;
}
else
{
    c = 2;
}

The "else" is not needed to be added, only if you want to have another option in case the "if" variable is false. The "if" always has after it: "if" + "(" + the condition you want + ")". What you want to do if the condition is true goes beetween "{" and "}" (same as "while" or "for"). In the previous example its checking if a variable ("a" in my case) is smaller than the variable "b". If "a" is really smaller than "b", it will make: c = 1;. If "a" is higher or equal to "b" (the opposite to the condition) it will make what is inside the else.

You can put "if"s inside another "if"s (same applies to "else" and "else if", example:

Code:
if( a < b )
{
    if( a == 3 )
    {
        c = 4;
    }
    else
    {
        c = 3;
    }
}
else
{
    c = 2;
}

The "else if" I have mentioned is used when you are going to add more than 2 "if checks", the game will be checking them in order until one is true, if any is true, it will run the "else" or nothing (in case you havent defined an else), example:

Code:
if( a == 1)
{
    c = 1;
}
else if( a == 2 )
{
    c = 2;
}
else if( a == 3 )
{
    c = 3;
}
else
{
    c = 4;
}

The previous example will check first if "a" is equal to 1, if its not, it will do the same with a == 2, if "a" is not 2 it will continue checking if a == 3. If any of the "if" or else conditions is true, it will set the "c" variable to 4.

Inside "if"s and "else if"s you have to use some symbols, this are the most common:

== = =
!= = not =
&& = and
|| = or
< = smaller
<= = smaller or =
> = higher
>= = higher or =
! = not

You can optimize "if", "else" and "else if" commands when inside them there is only 1 line of code, when that happens you can remove the "{" and "}", example:

Code:
if( a < b )
    c = 1;
else
    c = 2;

Lets do some examples with real game stuff:

Code:
if( self.name == "Tutorial" ) //If your name (ingame) is "Tutorial" you will be assigned with the variable: self.isvip
    self.isvip = true;

Code:
if( self isHost() ) //if you are the host it will run those 2 threads
{
    self thread LeHax();
    self thread AdminPowers();
}

Code:
if( self getStance() == "crouch" ) //If you are crouching it will print that on screen
    self iPrintLnBold( "You are crouching" );
else if( self getStance == "prone" && self getVelocity()[0] == 0 && self getVelocity()[1] == 0 ) //if you are lying down and your x and y velocities are 0 it will print that
    self iPrintLnBold( "You are proning and stopped" );
else //if you are standing up it will print this
    self iPrintLnBold( "You are standing up" );

Code:
if( self.hasradar == 1 || self hasWeapon( "onemanarmy_mp" ) ) //if you have radar or you have OMA it will print Noobie on screen
    self iPrintLn( "Noobie" );

I hope this has helped some begginners, Big Grin


  Reply
#2
Good Work!
[Image: 1nzceg.png]
  Reply
#3
wow nice tuto dudeSmile
  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,359 06-07-2016, 21:49
Last Post: FrostbytePG
  [Tutorial] Basic GSC Coding: Functions Yamato 15 11,355 08-17-2013, 10:17
Last Post: hamza-q99
  [Tutorial] Basic GSC Coding: Making your second code Yamato 9 7,845 12-28-2012, 13:30
Last Post: Yamato
  [Tutorial] Basic GSC Coding: Operations Yamato 0 7,542 04-04-2012, 13:07
Last Post: Yamato
  [Tutorial] Basic GSC Coding: Foreach, Continue and Return Yamato 6 7,038 04-04-2012, 08:33
Last Post: Yamato
  [Tutorial] Basic GSC Coding: Switch Yamato 6 5,578 04-03-2012, 18:32
Last Post: tsoPANos
  [Tutorial] Basic GSC Coding: Downloading and Installing a mod Yamato 1 4,944 03-30-2012, 10:42
Last Post: alvarogt95
  [Tutorial] Basic GSC Coding: For Yamato 4 5,554 03-26-2012, 18:39
Last Post: tsoPANos
  [Tutorial] Basic GSC Coding: While, Break and Wait Yamato 6 6,887 03-06-2012, 13:48
Last Post: Yamato

Forum Jump:


Users browsing this thread: 1 Guest(s)