Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tutorial Basic GSC Coding: Switch
#1
Good evening

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.

Ill finish soon with this basic gsc commands.

Thanks for reading. @Yamato
Reply

#2
IF is much cooler then switch
[Image: ctoc.jpg]
Reply

#3
(03-31-2012, 19:13)Tomsen1410 Wrote: IF is much cooler then switch

Yes, indeed, Tongue
Reply

#4
(03-31-2012, 19:13)Tomsen1410 Wrote: IF is much cooler then switch

Code:
if( method == switch ){
load_ivankec();
}
Reply

#5
(03-31-2012, 19:13)Tomsen1410 Wrote: IF is much cooler then switch

That's not what @SuperNovaAO said...


Inb4 we all get banned for using "if"!

OMA is waiting for your surprise.
Reply

#6
(03-31-2012, 19:45)Rendflex Wrote:
(03-31-2012, 19:13)Tomsen1410 Wrote: IF is much cooler then switch

That's not what @SuperNovaAO said...


Inb4 we all get banned for using "if"!

OMA is waiting for your surprise.

Ofcourse not

if (supernova == reading this){
too_lazy_switch();
}
Reply

#7
Yeah! Thank you for letting as know!
[Image: 1nzceg.png]
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tutorial] Basic GSC Coding: Building your first mod and your first code Yamato 2 11,205 06-07-2016, 21:49
Last Post: FrostbytePG
  [Tutorial] Basic GSC Coding: Functions Yamato 15 12,284 08-17-2013, 10:17
Last Post: hamza-q99
  [Tutorial] Basic GSC Coding: Making your second code Yamato 9 8,636 12-28-2012, 13:30
Last Post: Yamato
  [Tutorial] Basic GSC Coding: Operations Yamato 0 8,004 04-04-2012, 13:07
Last Post: Yamato
  [Tutorial] Basic GSC Coding: Foreach, Continue and Return Yamato 6 7,574 04-04-2012, 08:33
Last Post: Yamato
  [Tutorial] Basic GSC Coding: If, Else and Else If Yamato 2 5,795 03-30-2012, 10:47
Last Post: alvarogt95
  [Tutorial] Basic GSC Coding: Downloading and Installing a mod Yamato 1 5,195 03-30-2012, 10:42
Last Post: alvarogt95
  [Tutorial] Basic GSC Coding: For Yamato 4 5,925 03-26-2012, 18:39
Last Post: tsoPANos
  [Tutorial] Basic GSC Coding: While, Break and Wait Yamato 6 7,516 03-06-2012, 13:48
Last Post: Yamato

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum Powered By MyBB, Theme by © 2002-2024 Melroy van den Berg.