• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Cannot use string in switch statement?

obscene

Member
Going through an extremely annoying problem.

Code:
switch (something) 
  {
  case "some_string":
  do stuff
  break;
  }
Sure. String comparisons are not efficient. But when you're doing something quick and dirty that only gets called one time, why care. It's easier than making enums for every damn little thing. But, when using the YYC and only on Linux, these result in errors like this....

Game/gml_Script_scr_hint_get.gml.cpp:187:103: warning: comparison of array 'g_pString7287_C8D69341' not equal to a null pointer is always true [-Wtautological-pointer-compare]
if (( (___sw7286___.GetString() == g_pString7287_C8D69341) || ((___sw7286___.GetString() != NULL) && (g_pString7287_C8D69341 != NULL) && (strcmp( ___sw7286___.GetString(), g_pString7287_C8D69341 ) == 0)))) {
^~~~~~~~~~~~~~~~~~~~~~ ~~~~

I'm 100% sure the only problem is that it's a string. I've been going through these and either changing them to enums or just using if/else statements and knocking these errors out. But it's annoying. I have alot of them. And I can't help but think there's a checkbox somewhere I can click to tell the compiler "stfu this works on windows and mac."
 
Z

Zep--

Guest
Works fine here...

Looks like you don't have "something" defined as a string anywhere.

//room create code
global.something="No";

///object step event
switch (global.something)
{
case "Yes":
show_message("something Equals Yes") //this will print when something="Yes"
break;
case "No":
show_message("something Equals No") //this will print when something="No"
break;

}
 

obscene

Member
I definitely do. Typically it's a variable defined in the create event ( something=""; ) and then changed elsewhere.

Here's an example using a GM function that returns a string. Using Spine sprites and skeleton_animation_get() will return the animation name.

Code:
switch skeleton_animation_get()
    {
    case "Scrub":
        switch (image_index)
            {
            case 0: scrubup=true; break;
            case 22: scrubdown=true; break;
            }
        break;
    
    case "Scrub Hard":
        switch (image_index)
            {
            case 0: scrubup=true; break;
            case 16: scrubdown=true; break;
            }
        break;
    
    case "Scrub Harder":
        switch (image_index)
            {
            case 0: scrubup=true; break;
            case 12: scrubdown=true; break;
            }
        break;
    
    case "Soap":
        switch (image_index)
            {
            case 10:
                //audio_emitter_pitch(audio_emitter,random_range(1,1.1))
                var s=audio_play_sound_on(audio_emitter,
                    choose(snd_mom_soap_dish,snd_mom_soap_dish2,snd_mom_soap_dish3,snd_mom_soap_dish4)
                    ,false,1);
                audio_sound_gain(s,.25,0);
                break;
            }
                
    case "Load Dish 1":
    case "Load Dish 2":
        switch (image_index)
            {
            case 46:
                audio_play_sound_on(audio_emitter,choose(snd_mom_place_dish1,snd_mom_place_dish2),false,1);
                break;
            
            case 114:
                audio_play_sound_on(audio_emitter,snd_mom_get_dish,false,1);
                break;
            
            case 170:
                audio_play_sound_on(audio_emitter,choose(snd_mom_soap_dish,snd_mom_soap_dish2,snd_mom_soap_dish3,snd_mom_soap_dish4),false,1);
                break;            
            
            }
    }
Windows, that's fine. Mac, that's fine. Linux in VM is fine. Linux in YYC give me this crap...

Code:
Game/gml_Object_obj_mother_Other_7.gml.cpp:65:103: warning: comparison of array 'g_pString9720_E92917D4' not equal to a null pointer is always true [-Wtautological-pointer-compare]
if (( (___sw9719___.GetString() == g_pString9720_E92917D4) || ((___sw9719___.GetString() != NULL) && (g_pString9720_E92917D4 != NULL) && (strcmp( ___sw9719___.GetString(), g_pString9720_E92917D4 ) == 0)))) {
                                                                                                      ^~~~~~~~~~~~~~~~~~~~~~    ~~~~
Game/gml_Object_obj_mother_Other_7.gml.cpp:68:108: warning: comparison of array 'g_pString9721_E92917D4' not equal to a null pointer is always true [-Wtautological-pointer-compare]
else if (( (___sw9719___.GetString() == g_pString9721_E92917D4) || ((___sw9719___.GetString() != NULL) && (g_pString9721_E92917D4 != NULL) && (strcmp( ___sw9719___.GetString(), g_pString9721_E92917D4 ) == 0)))) {
                                                                                                           ^~~~~~~~~~~~~~~~~~~~~~    ~~~~
Game/gml_Object_obj_mother_Other_7.gml.cpp:71:108: warning: comparison of array 'g_pString9722_E92917D4' not equal to a null pointer is always true [-Wtautological-pointer-compare]
else if (( (___sw9719___.GetString() == g_pString9722_E92917D4) || ((___sw9719___.GetString() != NULL) && (g_pString9722_E92917D4 != NULL) && (strcmp( ___sw9719___.GetString(), g_pString9722_E92917D4 ) == 0)))) {
                                                                                                           ^~~~~~~~~~~~~~~~~~~~~~    ~~~~
3 warnings generated.
Game/gml_Object_obj_mom_Step_0.gml.cpp:222:106: warning: comparison of array 'g_pString12882_E78682B9' not equal to a null pointer is always true [-Wtautological-pointer-compare]
if (( (___sw12885___.GetString() == g_pString12882_E78682B9) || ((___sw12885___.GetString() != NULL) && (g_pString12882_E78682B9 != NULL) && (strcmp( ___sw12885___.GetString(), g_pString12882_E78682B9 ) == 0)))) {
                                                                                                         ^~~~~~~~~~~~~~~~~~~~~~~    ~~~~
Game/gml_Object_obj_mom_Step_0.gml.cpp:225:111: warning: comparison of array 'g_pString12886_E78682B9' not equal to a null pointer is always true [-Wtautological-pointer-compare]
else if (( (___sw12885___.GetString() == g_pString12886_E78682B9) || ((___sw12885___.GetString() != NULL) && (g_pString12886_E78682B9 != NULL) && (strcmp( ___sw12885___.GetString(), g_pString12886_E78682B9 ) == 0)))) {
                                                                                                              ^~~~~~~~~~~~~~~~~~~~~~~    ~~~~
Game/gml_Object_obj_mom_Step_0.gml.cpp:228:111: warning: comparison of array 'g_pString12887_E78682B9' not equal to a null pointer is always true [-Wtautological-pointer-compare]
else if (( (___sw12885___.GetString() == g_pString12887_E78682B9) || ((___sw12885___.GetString() != NULL) && (g_pString12887_E78682B9 != NULL) && (strcmp( ___sw12885___.GetString(), g_pString12887_E78682B9 ) == 0)))) {
                                                                                                              ^~~~~~~~~~~~~~~~~~~~~~~    ~~~~
Game/gml_Object_obj_mom_Step_0.gml.cpp:231:111: warning: comparison of array 'g_pString12888_E78682B9' not equal to a null pointer is always true [-Wtautological-pointer-compare]
else if (( (___sw12885___.GetString() == g_pString12888_E78682B9) || ((___sw12885___.GetString() != NULL) && (g_pString12888_E78682B9 != NULL) && (strcmp( ___sw12885___.GetString(), g_pString12888_E78682B9 ) == 0)))) {
                                                                                                              ^~~~~~~~~~~~~~~~~~~~~~~    ~~~~
Game/gml_Object_obj_mom_Step_0.gml.cpp:234:111: warning: comparison of array 'g_pString12889_E78682B9' not equal to a null pointer is always true [-Wtautological-pointer-compare]
else if (( (___sw12885___.GetString() == g_pString12889_E78682B9) || ((___sw12885___.GetString() != NULL) && (g_pString12889_E78682B9 != NULL) && (strcmp( ___sw12885___.GetString(), g_pString12889_E78682B9 ) == 0))) || ( (___sw12885___.GetString() == g_pString12890_E78682B9) || ((___sw12885___.GetString() != NULL) && (g_pString12890_E78682B9 != NULL) && (strcmp( ___sw12885___.GetString(), g_pString12890_E78682B9 ) == 0)))) {
                                                                                                              ^~~~~~~~~~~~~~~~~~~~~~~    ~~~~
Game/gml_Object_obj_mom_Step_0.gml.cpp:234:321: warning: comparison of array 'g_pString12890_E78682B9' not equal to a null pointer is always true [-Wtautological-pointer-compare]
else if (( (___sw12885___.GetString() == g_pString12889_E78682B9) || ((___sw12885___.GetString() != NULL) && (g_pString12889_E78682B9 != NULL) && (strcmp( ___sw12885___.GetString(), g_pString12889_E78682B9 ) == 0))) || ( (___sw12885___.GetString() == g_pString12890_E78682B9) || ((___sw12885___.GetString() != NULL) && (g_pString12890_E78682B9 != NULL) && (strcmp( ___sw12885___.GetString(), g_pString12890_E78682B9 ) == 0)))) {
                                                                                                                                                                                                                                                                                                                                ^~~~~~~~~~~~~~~~~~~~~~~    ~~~~
6 warnings generated.
So now I get to redo all that code using if/else.
 
Z

Zep--

Guest
Have you tried using macros or maybe an enum for var "something" instead?
 

obscene

Member
Yeah. You're a bad reader, man. That's what I said in the first post. :p I'm doing that to fix them. But it sucks, I have a lot left to replace and I was hoping someone would know why this crap is happening.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
File a bug please! That definitely doesn't look like an issue in your code, but rather a runner error.
 

obscene

Member
File a bug please! That definitely doesn't look like an issue in your code, but rather a runner error.
Will do. Will make a test project for it once I've got my head around it 100%. There are actually a few different things like this happening relating to strings. Also finding that special characters seem to trick it up. In the meantime though I'm just changing all my code as I don't want to wait for a fix. Been on this thing tooo long. :p
 

obscene

Member
File a bug please! That definitely doesn't look like an issue in your code, but rather a runner error.
Tried to make a test project. It compiled. Of course lol. However I'm leaning towards the conclusion that this occurs if there is another string elsewhere in the game containing a special character, example "Türkçe." So I think I just spent a day replacing switch statements and making enums for nothing.
 

rIKmAN

Member
Tried to make a test project. It compiled. Of course lol. However I'm leaning towards the conclusion that this occurs if there is another string elsewhere in the game containing a special character, example "Türkçe." So I think I just spent a day replacing switch statements and making enums for nothing.
But you levelled up your "enum making" and "switch replacing" skills, so it's not for nothing! :p

As Noc said it sounds like a runner error, so it might be best to continue as you are with your Windows build and get in touch with YYG with regards to fixing the Linux errors so you don't have to rewrite a lot of your code and instead wait for the Linux fixes.

I know you said you didn't want to wait, but if you are that close to release that you can't wait YYG have been known to provide fixed builds to users long before they release the official updates.

Just a thought.
 

obscene

Member
But you levelled up your "enum making" and "switch replacing" skills, so it's not for nothing! :p
That's a good point. :)

Well, if my above suspicion about special characters is correct, I'll know in about 20 minutes because I commented out everything that contained special characters and it's compiling as we speak. I'll just store that text externally and this problem is solved. (But there is definitely a bug).
 
Top