Switches and Cases  


by Postal v1

 

I remember when a multi-mode gun was something special.  Now there a dime a dozen.  I have noticed many people are going the way of making a variable called int(i.e. var int mode) then using if and else if questions (i.e. if (mode == 1) etc then else if (mode == 2) and so forth) which is perfectly good, but Switch is so much better.  Switch allows you to select one of a set of options, then use that.  Well I shall get to the code so you can see how it works, instead my having my babble on incoherently.

//

Var() Enum EMode {M_1,M_2,M_3,M_4} Mode;

 

ok this goes up in with the variables

 

var[use () if you want to be able to select which mode it starts off in, with default props]

Enum E[mode series name, I use mode, but you can use what ever you want ]

{  

M_[a mode name]

,

M_[another mode name]

}

 

[mode series name]

;

 

So you may have something like this for weapon :

 

Var() Enum EMode {M_1,M_2,M_3,M_4} Mode;

 

 

or something like this for a bot

 

Var Enum EMood {M_Happy,M_Sad,M_Angry,M_Berserk} Mood;

 

but for this tutorial, I shall use:

 

Var() Enum EMode {M_MachineGun,M_RocketLauncher,M_FlameThrower} Mode;

 

copied straight from a multi mode grenade launcher

//

Now the switch code, the code is basically like what you will be using.  This is set up using the same names in my example.

 

switch (Mode)

{

            case M_MachineGun:

                        break;

            case M_RocketLauncher:

                        break;

            case M_FlameThrower:

                        break;

}

 

Each case is a option, and the break is where it ends

 

now for something practical, here is sort of like what you may use in a weapon.

 

function Fire( float Value )

{

            switch (Mode)

            {

                        case M_MachineGun:

                                    Spawn(class'bullet');

                                    break;

                        case M_RocketLauncher:

                                    Spawn(class'rocket');

                                    break;

                        case M_FlameThrower:

                                    Spawn(class'flameburst');

                                    break;

            }

}

 

I hope you get the idea.

//

Now you probably are wondering how to switch between cases, well all you need is:

Mode= M_RocketLauncher;

[Mode series name] = M_[The new Mode name];

 

so if for example you want to have altfire switch modes you could have code like:

 

function AltFire( float Value )

{

            ChooseMode();

}

 

Function ChooseMode()

{

            switch (Mode)

            {

                        case M_1:

                                    Mode= M_RocketLauncher;

                                    PlayerPawn(Owner).ClientMessage("Mode 2",'Event',true);

                                    break;

                        case M_2:

                                    Mode= M_FlameThrower;

                                    PlayerPawn(Owner).ClientMessage("Mode 3",'Event',true);

                                    break;

                        case M_3:

                                    Mode= M_MachineGun;

                                    PlayerPawn(Owner).ClientMessage("Mode 1",'Event',true);

                                    break;

            }

            GoToState('AltFiring');//A place to play animation/sounds and to kill time

}

 

Well that is all, now should have everything you need to use switches, and if you still do not understand, mess around with the code in a weapon, you should be able to get the hang of it quickly.

 

//

Thanks:

 

Kobain for making me do this

Kangus for help with spelling/ suggestions/ and having me change a good bit of it