GM:S 1.4 Using &/or in if statments

Discussion in 'Programming' started by Cpt_Fantabulous, Aug 8, 2016.

Tags:
  1. So I want to create a If for when the player dies. It seems stupid to have to copy the code in to multiple else if statements when I could just use "or" for the same one.

    I have tried to google it but I can't seem to find what symbol gamemaker uses for this or even if it supports it.

    I am prob just being an idiot and misunderstanding something I have read but could someone help me out with this, please?

    Can you even do the kind of thing I am looking for in GML?
     
  2. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,114
  3. I knew I was misreading something. Think I need to take a break.

    Thanks
     
  4. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,114
    something the manual doesn't make exactly clear is that you can also use the keywords, "or, and, xor" interchangably with "||, &&, ^^"

    Oh, by the way, "xor" means if one is true and the other is false, doesn't matter which one.
     
  5. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,788
    There are a couple things to understand about the conjunctions/disjunctions.

    The bitwise conjunctions (&, |, ^) can be used in place of boolean conjunctions (&&, and, ||, or, ^^, xor) most of the time in case you're a sloppy programmer, but be aware the two sets of conjunctions are not the same and will not necessarily yield the same results.

    Consider the expressions if A & B and if A && B for example. The first example will be evaluated on a bitwise level, whereas the second will evaluate the truth of A and B. When GM evaluates the truth of an expression, any value less than 0.5 is evaluated to FALSE while anything higher is TRUE. It is important to understand that GM treats everything as numbers, so the expression A==B will return either a 0 or 1. What this means is something like A = B==C is valid syntax in GM (in which case A will be set to either 0 or 1). This is important to understand how a conditional of the expression if A & (B==C) actually works.

    Consider the conditional if mouse_check_button_pressed(mb_left) & instance_position(mouse_x, mouse_y, obj_button). The first half will evaluate to either TRUE or FALSE, which as I said above is the set of possibilities {0,1}; the second half will evaluate to the set of possibilities {1000000,n} where n is however many instances have been created. Suppose the values you get are 1 and 1000000 respectively. In a conditional of the format if A && B, the result would be TRUE, since both expressions would evaluate to TRUE and the conditional is therefore if TRUE && TRUE. However, in a conditional of the format if A & B, the result would be FALSE: at the bitwise level 1 & 1000000 = 0, which is FALSE.

    In short, be aware that if you do have a conjunction in a conditional and aren't getting the results you expected, make sure you didn't use a bitwise conjunction where you meant to use a boolean conjunction, and vice-versa. The two are nearly identical, but slightly different.

    The two types of conjunction classes are also handled with different arithmetic priority as well. The bitwise conjunctions have higher priority than boolean conjunctions, meaning bitwise conjunctions will be processed before boolean conjunctions. So in the conditional if A && B & C, GM will first test the truthfulness of the bitwise expression B & C and then test the conjunction of the result with A. Also be aware it reads from left to right. Consider the conditional if A && B ^^ C and the conditional if A ^^ B && C. If A is TRUE, B is TRUE, and C is FALSE, then the first conditional will be TRUE (TRUE && TRUE is TRUE, TRUE ^^ FALSE is TRUE) but the second conditional will be FALSE (TRUE ^^ TRUE is FALSE, FALSE && FALSE is FALSE).


    Anyway, in case you don't know your conjunctions, here is how they would work in a conditional:

    & (bitwise AND)
    Compares the bits of two values, returning TRUE if any set bits match
    | (bitwise OR)
    Combines the bits of two values, only returning FALSE if both values are 0
    ^ (bitwise XOR)
    Compares the bits of two values, returning TRUE if any bits do not match
    && (boolean AND)
    Compares the truthfulness of two values, returning TRUE if both values are TRUE
    || (boolean OR)
    Compares the truthfulness of two values, returning TRUE if either values are TRUE
    ^^ (boolean XOR)
    Compares the truthfulness of two values, returning TRUE if only one of the values is TRUE

    For bitwise OR, I intentionally used "combines" instead of "compares", because I think it's a good way to treat bitwise OR. I didn't do the same for boolean OR because that makes it sound like contradictions are true (which may or may not be true).
     
    Mike Dolphin likes this.
  6. Neo

    Neo Member

    Joined:
    Jul 10, 2016
    Posts:
    56
    If you use the word and instead of the symbol && is it the bitwise or boolean comparision same question with or is it equivalent to | or ||
     
  7. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,788
    Words are only for the booleans (true/false)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice