GMS 2 Help with my switch statement plz

S

Sroft

Guest
So this is my first time coding anything and I think its going really well... but i do have a problem.
Switch statements- I made one successful switch but now im really stuck with the statement im using right now here it is
create
//weapons
enum WEAPONS_MODE
{
HANDS,
PLASMA
}
weapons = WEAPONS_MODE.HANDS
step
//Animation
switch (weapons)
{
case WEAPONS_MODE.HANDS:
{
weapons = WEAPONS_MODE.HANDS
Unarmed();
}
case WEAPONS_MODE.PLASMA:
{
weapons = WEAPONS_MODE.PLASMA
Plasmagun();
}
}
im trying to make the player start out with no gun(aka unarmed) then i have a collision event where when the player runs into the gun on the floor they go to plasmagun. the problem is when i start the game the player is already holding the plasmagun and skips unarmed.
unarmed and plasmagun are scripts plz comment back if i need to add those too
thank you
 
You have to break out of each case, otherwise any below will continue to run. Also, if "weapons" already holds the value you're checking for, there's no point in setting the value again. On top of that, I'd just call these scripts upon a collision event instead of running a switch statement all the time.
 

samspade

Member
Looks like you're missing the keyword break. If you don't put break at the end of a case, it will continue running through the cases.

Code:
switch (variable) {
    case 1:
        /* some code */
    case 2:
        /* some code */
        break;

    case 3:
        /* some code */
        break;

}
In the above example if variable equals 1, then both case 1 and 2 will run. If 2, then only case 2. If 3, then only case 3. All of your code appears to be like case 1 in my example. Where it runs the code but then immediately runs the next code as well.

See the manual as well for more:

https://docs2.yoyogames.com/source/_build/3_scripting/3_gml_overview/14_language_features.html
 
Top