GML Can a case in a switch statement be checked for "noone"?

X

XirmiX

Guest
Cases in switch statements need to be a number, be it 0, positive or a negative number, or it can be set to default... The "noone" declaration, refers to a null or empty variable. If I used a switch statement, could I have a case for if it is unset? It could allow me for some pretty efficiently-clever coding, if I can.

EDIT: I could probably test this easily to see if this works for me, but since I've a lot of other code to work with just to get certain type of feature in, I have a lot of things to keep track of and I'm bound to get a lot of errors from elsewhere, so it's best to know before I wrap my code around this. Plus, this thread may be useful to others.
 

The-any-Key

Member
Cases in switch statements need to be a number
Can be string too. If I got a lot of cases and just started to create a state machine I use strings for readability. Then when all is done I can enum them for speed.

The "noone" declaration, refers to a null or empty variable
Nope. it's the number -4.
"undefined" is the nearest to an empty variable.

Can a case in a switch statement be checked for "noone"?
Yes. Because noone = -4. So you can case for noone or just case -4.

https://docs.yoyogames.com/source/dadiospice/002_reference/001_gml language overview/data types.html
 
Last edited:

CloseRange

Member
Can be string too. If I got a lot of cases and just started to create a state machine I use strings for readability. Then when all is done I can enum them for speed.


Nope. it's the number -4.
"undefined" is the nearest to an empty variable.

Can a case in a switch statement be checked for "noone"?
Yes. Because noone = -4. So you can case for noone or just case -4.
string is a number technically. If you look up ascii chart it'll show the value for each char and a string is just a bunch of chars so just a bunch of numbers.

if you never initialized the variable you are checking for then yes it will become undefined but if you try to use a switch statement it will return an error before even getting to that point. If you want to set a variable to noone and check it that is fine if you want to not initialize the variable then check if it exists you would do something like this:
Code:
if !is_undefined(variable) {
     switch(variable) {
         case 1: //yada yada
     }
} else {
     // if undefined
}
 

rwkay

GameMaker Staff
GameMaker Dev.
The Case expression in a switch statement can be any type and not just a number i.e. they can be strings as well.

Russell
 
G

Guest

Guest
EDIT: I could probably test this easily to see if this works for me, but since I've a lot of other code to work with just to get certain type of feature in, I have a lot of things to keep track of and I'm bound to get a lot of errors from elsewhere, so it's best to know before I wrap my code around this. Plus, this thread may be useful to others.
GMS2 is nice in that it allows you to have two IDEs running at the same time with different projects. In my experience, they run well without fighting each other. I often open a second IDE with a project called Sandbox with a couple base obj_labrats so I can quickly test things exactly like in your question, before I touch the code in my real project. I imagine more experienced programmers actually develop chunks of their project separately and add them in after getting through basic testing, to do quicker iterations without having to wait for the full project to compile.
 
X

XirmiX

Guest
Well, strangely enough, it seems as if I can't have it when it comes to scripts... it expects an input for a script, but when I provide no input, it gives me an error instead of continuing to run without a value to check if it doesn't have a value:

Error
Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Async Event: Networking
for object obj_connection:

illegal access of argument, argument is not provided to script
 at gml_Script_hullFive (line 1) - determineData = argument[0];
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Script_hullFive (line 1)
called from - gml_Script_hullIdentity (line 18) -         returnScriptType = hullFive();
called from - gml_Object_obj_connection_NetworkingEvent_1 (line 110) -                     hullIdentity(buffer_hull_receive);
Script code that executes hullFive script (hullIdentity)
Code:
returnScriptType = hullFive();
Script where the argument can't be noone (hullFive)
Code:
determineData = argument[0];

switch(determineData)
{
    case noone:
        //code that sets stuff, not relevant
    default:
        //code that sets stuff, not relevant
    break;
}
P.S and before anyone tells me yes, there is only supposed to be one break and not two.
 

FrostyCat

Redemption Seeker
noone DOES NOT mean "no value" for an argument passed to a script. What you should be doing is checking argument_count before accessing arguments, not expecting some sort of placeholder value already there for you.
Code:
if (argument_count == 0) {
  determineData = noone;
} else {
  determineData = argument[0];
}

switch(determineData) {
  case noone:
    //...
  default:
    //...
  break;
}
 
Top