• 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!

GameMaker How to use != as an or statement

N

Nathan

Guest
Here is the code I need to alter :

if (variable01 != "fire")

{
sprite_index = default_sprite;
}

I need this to be come if variable01 is 'fire' OR 'water' then change the sprite index. How do you make it into an 'or' statement? I couldn't find an example of this. Thanks!
 

Evanski

Raccoon Lord
Forum Staff
Moderator
if (variable01) == ( "fire" ^^ "water" )

{
sprite_index = default_sprite;
}


^^ is and.or
 
Last edited:

Simon Gust

Member
I am Little confused. You want sprite index to be Default sprite when variable01 is "fire" or "water" or do you want it to be Default sprite when variable01 isn't "fire" nor "water"?
That said, @EvanSki, do you really want to glue together strings as if they were booleans?

Code:
if (variable01 == "water" or variable01 == "fire") // or
if (variable01 != "water" and variable01 != "fire") // nor
Which one do you want?
 

CloseRange

Member
I've never seen this before so i tried it out:
Code:
var variable01 = "water"
if (variable01) == ( "fire" ^^ "water" )

{
    show_message("Hi")
}
my exact code. I got an error. I thought it was fishy.... ^ is bitwise xor but ^^ wouldn't work I don't think
@Nathan just do this:
Code:
if(variable01 == "fire" || variable01 == "water") {

}
|| is actually how you do an or statment
 

FrostyCat

Redemption Seeker
See: How NOT to use && and || (#1)

Either "fire" or "water":
Code:
if (variable01 == "fire" || variable01 == "water")
{
    sprite_index = default_sprite;
}
Neither "fire" nor "water":
Code:
if (variable01 != "fire" && variable01 != "water")
{
    sprite_index = default_sprite;
}
 

Evanski

Raccoon Lord
Forum Staff
Moderator
&&, ||, ^^ (and, or and xor) - Combine Boolean values to give either true or false. If any of the following examples resolves to true then the code would be run:


if (a == b && c == d) {do something...}// and
if (a == b || c ==d) {do something...}// or
if (a == b ^^ c == d) {do something...} // xor

https://docs2.yoyogames.com/source/_build/3_scripting/3_gml_overview/12_expressions.html

if var = "fire" meaning is var "fire"

if var = "water" is var "water"

^^ is it one or both

works for me
 
N

Nathan

Guest
Got it working thanks! What worked was the ^^ As Catan mentioned the || I think didn't work because of the 'both' check as well. I may have missed the 'not' for != as well, in any case I got the result I was after with ^^ :) Thank you all for your help!
 

FrostyCat

Redemption Seeker
if var = "fire" meaning is var "fire"

if var = "water" is var "water"

^^ is it one or both

works for me
Look again at what you posted on post #2. This won't work properly:
Code:
if (variable01) == ( "fire" ^^ "water" )
{
    sprite_index = default_sprite;
}
This is what it should have been like:
Code:
if (variable01 == "fire") ^^ (variable01 == "water")
{
    sprite_index = default_sprite;
}
I hope @Nathan re-reads this and actually tries out every value of variable01 (i.e. "fire", "water" and one other string that is neither), and fixes the code if it happens to be the "fire" ^^ "water" nonsense. Otherwise he'll be burying a landmine in his code.
 
N

Nathan

Guest
UPDATED : Frosty Cat - Here is what worked for me :

if (variable01 == "fire" ^^ variable01 != "medusa")

I had to play around a bit with it to give me the proper result. It also works how I need when variable01 is set to other strings. Not quite sure what the translation of this would be (I'm not a programmer) but somehow it's working as intended.
 
H

Homunculus

Guest
That condition makes no sense. You are using ^^, therefore if both statements are true, the condition is false.

In words, this condition is true only if variable01 is not “fire” and is not “medusa”. and if that is really what you need, you definitely wrote it in a very convoluted way.

Can we also stop using xor please until the request is more clear? I started using game maker in 2003 with version 5, and in all these years I’ve never had any use for that, throwing it into the mix is just making things way more confusing than they probably need to be.
 
N

Nathan

Guest
Yea I don't understand it either, but it works exactly how I need. I tried a bunch of combinations of the == and != as well as ^^ and || and it only worked this way. Since the string can only be one thing at a time the way it is switched it can never be both. Mysteries abound!
 
H

Homunculus

Guest
Programming is not about trying stuff at random, if you don't understand what you are doing, you will run into trouble sooner or later. The prerequisite to avoiding this kind of situations is that you understand what you are trying to achieve before starting to code or even thinking about the code. If you can't even express it in words, how can you hope to program something that works?

I'm not telling you this because I like being pedantic, it's a really important part of a programmer skillset, and a basic one.
 
Code:
if (variable01 != "fire" && variable01 != "medusa")
Should result in identical output. Xor is completely superfluous since it is physically impossible for a variable to be in two states at once unless you're using some kind of quantum computer.

I recommend studying basic boolean algebra. It will help your understanding of boolean (I.E. True or False/binary) logic immensely. That seems to me to be what you're struggling with the most here.

EDIT: https://ryanstutorials.net/boolean-algebra-tutorial/
 
N

Nathan

Guest
Catan : I understand what you are saying. I never got beyond basic programming but can mess around with the basics. My brother is the one doing 95% of the programming for our game, but every once in awhile when new small things need implementing and I get tired of waiting for him I take a stab at it by going off things he's previously already put in the game. I handle the design, graphics, concepts, sound, music, etc. Just about everything except the programming, but occasionally I have to play around.

Nacho : Thanks for the link. You are correct, I tried your code format and it works exactly the same as the funky combo I created. I'm sure now I won't get scolded by my brother when he reviews the code haha. I guess my biggest question is what does != represent? How is it different from = and == ?Is it the negation of equals or another form of equals?
 
Nacho : Thanks for the link. You are correct, I tried your code format and it works exactly the same as the funky combo I created. I'm sure now I won't get scolded by my brother when he reviews the code haha. I guess my biggest question is what does != represent? How is it different from = and == ?Is it the negation of equals or another form of equals?
You're welcome! The exclamation point "!" is used to represent negation. So yes, "!=" is essentially saying "not equals". It checks two statements for inequality. The line of code can be written in English as:
Code:
if variable01 is not equal to "fire" AND variable01 is not equal to "medusa"
The only time both these statements will be true is when variable01 is not set to either "fire" or "medusa".
 
N

Nathan

Guest
Ah I see, so I was reading what he did backwards from the beginning thus the confusion for everyone. My mistake. Now all is working perfectly, I appreciate everyones help on this, it's the best way for me to learn :)
 
Top