1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Tutorial Basic GSC Coding: If, Else and Else If

Discussion in 'Tutorial Section' started by CabCon, Aug 1, 2016.

  1. CabCon

    CabCon Head Administrator Staff Member

    3,044
    1,815
    263
    Credits:
    59,111
    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" );
    Question?
    Please login or register to view links or downloads!.

    Credits to Yamato!
     
    Harry likes this.
  2. Harry

    Harry I'm 'Serious'ly a better Developer

    821
    579
    268
    Credits:
    2,617
    Pretty self explanatory. Good for newbies. Can I add this to my thread tomorrow?
     
    CabCon likes this.
  3. CabCon

    CabCon Head Administrator Staff Member

    3,044
    1,815
    263
    Credits:
    59,111
    Thank you,
    yeah I also like these tutorials! Credits are going to Yamato! Best tutorials out there!


    Also which kind of thread are you planning to create? And of course you can use it!
     
  4. Harry

    Harry I'm 'Serious'ly a better Developer

    821
    579
    268
    Credits:
    2,617
    I want to expand my re-opened thread. Make it with some more stuff :grinning:
     
    CabCon likes this.
  5. CabCon

    CabCon Head Administrator Staff Member

    3,044
    1,815
    263
    Credits:
    59,111
    That's a good idea! :y: +1
     

Share This Page