Issue comparing strings

I'm running into an issue where an if statement comparing two strings is returning false even though the strings are the same. Here is my code:

GML:
...
if (weapon_slot != noone) {
            var w_key = weapon_slot.weapon_key;
            if (string(w_key) == string("gun")) {
...
The string() cast is from my latest attempt, but the if statement also returns false without it. I've confirmed through breakpoints and print statements that the value w_key actually is "gun", in lowercase and with no spaces, but my code still doesn't enter the "if" block. I've tried = rather than == since some things I've read say that's necessary for string comparisons, but that didn't work either. Is there some other method I should be using to compare?
 

ophelius

Member
Why are you using the string function? Just use this:
if (w_key == "gun") {
The string function converts a number to a string
 

Yal

šŸ§ *penguin noises*
GMC Elder
in lowercase and with no spaces
With "no spaces", you mean "no whitespace", right? I usually add "<" and ">" characters around any string data I want to output just so random trailing / leading spaces won't be missed, because it's surprisingly easy to accidentally add some, especially when reading parts of a string to store in another. Newlines also tend to get a bit iffy, since they technically are two characters on Windows, the behavior or one of these characters on its own isn't clearly defined, and *nix systems use one of them on its own (CR for mac and LF for Linux). I think like >80% of my whitespace problems have been newline-related over the years, and they're definitely at least as bad as spaces.
 

Yal

šŸ§ *penguin noises*
GMC Elder
Sweet, glad I could help! It usually doesn't hurt to be a bit paranoid about whether your code actually does what it's supposed to, and having the right debug message at the right place can save you a lot of frustrated troubleshooting down the line.
 
Top