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: Switch

Discussion in 'Tutorial Section' started by CabCon, Feb 4, 2016.

  1. CabCon

    CabCon Head Administrator Staff Member

    2,803
    1,668
    113
    Credits:
    40,827
    Hello CCM,
    In this tutorial I am going to explain how does the function "switch" works and how to use it. It is used when you have to use a lot of "if"s, to reduce the ammount of "if"s you use this switch. See the comparison below:

    Code:
    number = randomIntRange( 1, 5 );
    if( number == 1 )
        a = 1;
    if( number == 2 )
        a = 2;
    if( number == 3 )
        a = 3;
    if( number == 4 )
        a = 4;
    if( number == 5 )
        a = 5;
    
    You can do:
    Code:
    number = randomIntRange( 1, 5 );
    switch( number )
    {
        case 1:
            a = 1;
        break;
    
        case 2:
            a = 2;
        break;
    
        case 3:
            a = 3;
        break;
    
        case 4:
            a = 4;
        break;
    
        case 5:
            a = 5;
        break;
    }
    
    The switch structure is like this:
    Code:
    switch( something (variable for example) )
    {
        case 0: //After the "case" name add a ":"
        HERE ADD CASE CODE
        break; //Add break to stop the switch "searching"
    }
    
    You can add as many cases as you want, is not necesary to call cases with numbers, you can call them with different names, the names go beetween " ".
    Code:
    switch( colour )
    {
        case "Green":
        SOMETHING
        break;
    
        case "Blue":
        SOMETHING
        break;
    }
    
    "switch" command also allows you to make some kind of "else" function, in this case is called "default", you use it like this:
    Code:
    number = RandomInt( 5 )
    switch( number )
    {
        case 0:
            a = 0;
        break;
    
        case 1:
            a = 1;
        break;
    
        default:
            a = 6;
        break;
    }
    
    In the previous example, number will be a random number beetween 0 and 5, if the number is equal to 0: a = 0. If the number is 1: a = 1, If number is not 0 or 1, it will be the default, in this case: a = 7.

    This kind of functions allow you to give the same effect to different "case"s like this, it helps to save space (in the example case 0, 1 and 2 will have the same effect):
    Code:
    number = RandomInt( 5 )
    switch( number )
    {
        case 0:
        case 1:
        case 2:
            SOMETHING
        break;
    
        case 3:
            SOMETHING
        break;
    }
    
    Lets put a decent example (is a code I released sometime ago):
    Code:
    WaitT( unit, ammount )
    {
        switch( unit )
        {
            case "minutes":
                wait ( ammount * 60 );
            break;
    
            case "hours":
                wait ( ammount * 3600 );
            break;
    
            case "miliseconds":
                wait ( ammount / 1000 );
            break;
    
            case "deciseconds":
                wait ( ammount / 10 );
            break;
    
            case "centiseconds":
                wait ( ammount / 100 );
            break;
    
            case "microseconds":
                wait ( ammount * 0.000001 );
            break;
    
            case "frames":
                wait ( 0.05 * ammount );
            break;
    
            case "days":
                wait ( ammount * 86400 );
            break;
    
            case "weeks":
                wait ( ammount * 604800 );
            break;
    
            case "tenminutes":
                wait ( 600 * ammount );
            break;
        }
    }
    
    In the previous example you add on the function the variable unit, which has to match in name with any of "case"s names (minutes, centiseconds, weeks...), then, after you assigned the unit, the switch will make your mod wait the ammount of time units you specified on the function name.

    Thanks for reading. @CabCon. Credits are going to Yamato.
     
    iRnZ likes this.
  2. BaDSooD

    BaDSooD Old CCM Member

    617
    358
    63
    Credits:
    63
    Thank's
    (realy thank's for all tut on mw2/bo2) :smile:
     

Share This Page