How Can I Use Multiple Expression in a Switch Statement or in the Case Section of the Switch

  • Thread starter Vanavah Edwards
  • Start date
V

Vanavah Edwards

Guest
I am using the following code but I am getting an error in line 1

switch (RoomNum==250) && (CardType=="General") && (TriviaNum250) {
case 1:
draw_text(320, 530, "Button Pressed = " + PlayerClicked);
draw_text(420, 530, "Room = " + string(RoomNum));
draw_text(520, 530, "Card Type = " + CardType);
draw_text(620, 530, "Trivia No = " + string(TriviaNum));
break;
case 2:
draw_text(320, 530, "Button Pressed = " + PlayerClicked);
draw_text(420, 530, "Room = " + string(RoomNum));
draw_text(520, 530, "Card Type = " + CardType);
draw_text(620, 530, "Trivia No = " + string(TriviaNum));
break;
case 3:
draw_text(320, 530, "Button Pressed = " + PlayerClicked);
draw_text(420, 530, "Room = " + string(RoomNum));
draw_text(520, 530, "Card Type = " + CardType);
draw_text(620, 530, "Trivia No = " + string(TriviaNum));
break;
}
 
M

maartenvt

Guest
You entered an expression after switch which can only return true or false (this means you van have only two cases, case true: and case false:). This is not how a switch statement works, the way you are using it. You usually use a switch statement to describe what needs to happen for different values of a particular variable. Example (assume day is a variable that contains the name of a day):
Code:
switch (day) {
case "monday": show_message("I HATE MONDAYS"); break;
case "tuesday": show_message("TUESDAY SUCKS"); break;
default: show_message("Okay day");
}
So I don't know what scenarios you are describing at case 1, 2 and 3, but if there is a variable which contains a card number or something, you would probably want to put that after switch.

https://docs.yoyogames.com/source/dadiospice/002_reference/001_gml language overview/401_12_switch.html
 
Last edited by a moderator:

Nocturne

Friendly Tyrant
Forum Staff
Admin
Post the error? Your code won't work anyway because you are testing a series of boolean expressions so can only get 0 or 1 as a return... You really need to post more info if you want further help.
 

TsukaYuriko

☄️
Forum Staff
Moderator
You are mixing and matching syntax for two different types of statements without actually separating the statements.

You should read the manual page about switch statements so you can learn how to use them properly.
Next, you should read the manual page about if statements.

Once you're done with that and know how and in which situations they are used, decide which is the one you want to be using where in your code. You will probably have to use both, but I'm having trouble deciphering what your code is supposed to do.
 
V

Vanavah Edwards

Guest
I am trying to avoid using too many IF statements. Can this be restructured or I have to use IF statements e.g.

switch (RoomNum==250) {
case (CardType=="General") && (TriviaNum250==1):
draw_text(320, 530, "Button Pressed = " + PlayerClicked);
draw_text(420, 530, "Room = " + string(RoomNum));
draw_text(520, 530, "Card Type = " + CardType);
draw_text(620, 530, "Trivia No = " + string(TriviaNum));
break;
case (CardType=="General") && (TriviaNum250==2):
draw_text(320, 530, "Button Pressed = " + PlayerClicked);
draw_text(420, 530, "Room = " + string(RoomNum));
draw_text(520, 530, "Card Type = " + CardType);
draw_text(620, 530, "Trivia No = " + string(TriviaNum));
break;
case (CardType=="General") && (TriviaNum250==3):
draw_text(320, 530, "Button Pressed = " + PlayerClicked);
draw_text(420, 530, "Room = " + string(RoomNum));
draw_text(520, 530, "Card Type = " + CardType);
draw_text(620, 530, "Trivia No = " + string(TriviaNum));
break;
}
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Code:
if roomNum == 250
{
if CardType == "General"
    {
    switch(triviaNum250)
        {
        case 1:
             draw_text(320, 530, "Button Pressed = " + PlayerClicked);
            draw_text(420, 530, "Room = " + string(RoomNum));
            draw_text(520, 530, "Card Type = " + CardType);
            draw_text(620, 530, "Trivia No = " + string(TriviaNum));
        break;
        case 2:
            // etc...
        }
    }
}
Two "if's" isn't that much and the cleanest way to do what you posted...
 
V

Vanavah Edwards

Guest
Thanks very much for the above reply. I will use it. However, I have been using IF statements as I have around 27 + expressions to add. I have declared in a different script the following global variables as :
global.RoomNum = 0;
global.CardType = "";
global.PlayerClicked = "";
global.PlayerClicked2 = "WrongAnswer";
global.TriviaNum100 = 0;
global.TriviaNum250 = 0;
global.TriviaNum500 = 0;

**************************************
Below is the code I have been using with IF statement but I am getting the error posted afterwards. Can you please help me to figure out what is wrong with these IF statement as everything seems okay to me?

PlayerClicked2 = "WrongAnswer";
if (roomNum == 250) && (CardType == "General") && (TriviaNum == 1) && (PlayerClicked == "A") {
draw_text(320, 530, "Button Pressed = " + PlayerClicked);
draw_text(420, 530, "Room = " + string(RoomNum));
draw_text(520, 530, "Card Type = " + CardType);
draw_text(620, 530, "Trivia No = " + string(TriviaNum));
PlayerClicked2 = "Correct";
}

if (roomNum == 250) && (CardType == "General") && (TriviaNum == 2) && (PlayerClicked == "B") {
draw_text(320, 530, "Button Pressed = " + PlayerClicked);
draw_text(420, 530, "Room = " + string(RoomNum));
draw_text(520, 530, "Card Type = " + CardType);
draw_text(620, 530, "Trivia No = " + string(TriviaNum));
PlayerClicked2 = "Correct";
}

if (roomNum == 250) && (CardType == "General") && (TriviaNum == 3) && (PlayerClicked == "C") {
draw_text(320, 530, "Button Pressed = " + PlayerClicked);
draw_text(420, 530, "Room = " + string(RoomNum));
draw_text(520, 530, "Card Type = " + CardType);
draw_text(620, 530, "Trivia No = " + string(TriviaNum));
PlayerClicked2 = "Correct";
}

if (PlayerClicked2 == "WrongAnswer") {
draw_text(320, 530, "Button Pressed = " + PlayerClicked);
}


___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Mouse Event for Left Button
for object TriviaColorButton1:

Variable TriviaColorButton1.roomNum(100009, -2147483648) not set before reading it.
at gml_Script_AnswersScript (line 2) - if (roomNum == 250) && (CardType == "General") && (TriviaNum == 1) && (PlayerClicked == "A") {
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_AnswersScript (line 2)
called from - gml_Object_TriviaColorButton1_LeftButtonDown_1 (line 1) - AnswersScript();
 
V

Vanavah Edwards

Guest
Thanks very much I cant understand how I could have missed that. However, I am still getting this error:


___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Mouse Event for Left Button
for object TriviaColorButton1:

Variable TriviaColorButton1.RoomNum(100005, -2147483648) not set before reading it.
at gml_Script_AnswersScript (line 2) - if (RoomNum == 250) && (CardType == "General") && (TriviaNum == 1) && (PlayerClicked == "A")
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_AnswersScript (line 2)
called from - gml_Object_TriviaColorButton1_LeftButtonDown_1 (line 1) - AnswersScript();
 
V

Vanavah Edwards

Guest
I thought that global. declared it as a global variable. How then do i declare and call back a global variable.
 

TsukaYuriko

☄️
Forum Staff
Moderator
If you declare it as global.variable, you refer to it as global.variable everywhere else too.
 

FrostyCat

Redemption Seeker
Of course you need to restructure. But it's not the if blocks that need restructuring, it's the way you handle correct answers. When done properly, that group of draw_text() lines should only show up once.

Had you declared the correct answers as a separate array like this:
Code:
global.correctAnswer250[1] = "A";
global.correctAnswer250[2] = "B";
global.correctAnswer250[3] = "C";
//...
Then your checks would collapse into one:
Code:
if ((global.RoomNum == 250) && (CardType == "General") && (global.correctAnswer250[TriviaNum] == PlayerClicked)) {
  draw_text(320, 530, "Button Pressed = " + PlayerClicked);
  draw_text(420, 530, "Room = " + string(global.RoomNum));
  draw_text(520, 530, "Card Type = " + CardType);
  draw_text(620, 530, "Trivia No = " + string(TriviaNum));
  PlayerClicked2 = "Correct";
}
The next time you run into redundancy problems like this, don't get baited into being switch-happy. A lot of novices have these tendencies and a switch block is almost never the right way out of it. Most of the time the real solution comes from remodeling their data and/or rewriting their expressions.

Edit: Changed second draw_text() line to include global.
 
Last edited:
V

Vanavah Edwards

Guest
Thanks again for your expertise. I really appreciate all your comments and will put them into practice. I am about to try the above and will post a comment soon.
 
V

Vanavah Edwards

Guest
I have tried the above and I LOVE your suggestions. It worked so well. It saved me a great deal of coding. I must say that you have good insight. I really appreciate your knowledge, expertise, patience and kindness. Thanks. I will be posting more problems shortly in another thread.
 
Top