1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

GMS 2 How to use != as an or statement

Discussion in 'Programming' started by Nathan, Sep 26, 2019.

Tags:
  1. Nathan

    Nathan Member

    Joined:
    Jan 19, 2017
    Posts:
    53
    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!
     
  2. EvanSki

    EvanSki King of Raccoons

    Joined:
    Apr 17, 2018
    Posts:
    581
    if (variable01) == ( "fire" ^^ "water" )

    {
    sprite_index = default_sprite;
    }


    ^^ is and.or
     
    Last edited: Sep 26, 2019
  3. Homunculus

    Homunculus Member

    Joined:
    Jun 20, 2016
    Posts:
    856
    Are you serious?
     
  4. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,262
    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?
     
  5. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    1,008
    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
     
  6. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,881
    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;
    }
     
  7. EvanSki

    EvanSki King of Raccoons

    Joined:
    Apr 17, 2018
    Posts:
    581
    &&, ||, ^^ (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
     
  8. curato

    curato Member

    Joined:
    Jun 30, 2016
    Posts:
    438
  9. Homunculus

    Homunculus Member

    Joined:
    Jun 20, 2016
    Posts:
    856
    Your code is totally wrong, FrostyCat link explains why. ^^ (xor) is one but not both, || (or) is one or both.

    But in this case, why would you even consider the “both” case, a string can’t be two things at the same time...
     
    CloseRange and EvanSki like this.
  10. Nathan

    Nathan Member

    Joined:
    Jan 19, 2017
    Posts:
    53
    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!
     
    EvanSki likes this.
  11. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,881
    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.
     
    EvanSki and CloseRange like this.
  12. Nathan

    Nathan Member

    Joined:
    Jan 19, 2017
    Posts:
    53
    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.
     
  13. Homunculus

    Homunculus Member

    Joined:
    Jun 20, 2016
    Posts:
    856
    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.
     
  14. Nathan

    Nathan Member

    Joined:
    Jan 19, 2017
    Posts:
    53
    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!
     
  15. Homunculus

    Homunculus Member

    Joined:
    Jun 20, 2016
    Posts:
    856
    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.
     
    Simon Gust likes this.
  16. nacho_chicken

    nacho_chicken Member

    Joined:
    Jun 21, 2016
    Posts:
    463
    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/
     
  17. Nathan

    Nathan Member

    Joined:
    Jan 19, 2017
    Posts:
    53
    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?
     
  18. nacho_chicken

    nacho_chicken Member

    Joined:
    Jun 21, 2016
    Posts:
    463
    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".
     
  19. Nathan

    Nathan Member

    Joined:
    Jan 19, 2017
    Posts:
    53
    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 :)
     
    nacho_chicken likes this.

Share This Page