1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Question - Code Order Of Operations

Discussion in 'GameMaker Studio 2 Community Tech Support' started by samspade, Dec 3, 2019.

  1. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,117
    I've been looking into some of the basics of GML recently and when I was looking at operator precedence I came across this page in the manual: Expressions. It claims that: "When doing multiple operations in a single expression, it is very important that you use brackets () to separate out the order of operation, as different platforms may perform them differently if not explicitly stated in this way."

    This makes some sense, however, it goes on to add that:

    Code:
    
    a = b == c || d;
    a = (b == c || d); //better
    a = ((b == c) || d); //best
    
    
    That seems ridiculous. The only reason for the middle one being better is if this is a possible evaluation of the first line:

    Code:
    
    (a = b) == c || d;
    
    
    Is it really this undefined? I would have assumed there was at least some consistency for order across all platforms on basic stuff like assignment or comparisons. For example:

    Code:
    
    //would this ever be evaluated as
    if 5 * 2 == 10 * 1 {
    }
    
    //this
    if 5 * (2 == 10) * 1 {
    }
    
    
     
  2. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    7,044
    The example is meant to illustrate a point, not be taken as a "real world" example. :)

    Basically, the manual is saying, "don't rely on order of operations across all platforms as that may not work or may be changed behind the scenes". It's good practice to be explicit and it prevents future potential errors when you compile using YYC or on another target platform.
     
    Cpaz likes this.

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