gm-docs-parser 1.0.0

A collection of typings for GameMaker Studio 2 manual pages
Documentation
<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head>

  <script type="text/javascript" language="JavaScript">
    //<![CDATA[
    function reDo() {
      if (innerWidth != origWidth || innerHeight != origHeight)
        location.reload();
    }
    if ((parseInt(navigator.appVersion) == 4) && (navigator.appName == "Netscape")) {
      origWidth = innerWidth;
      origHeight = innerHeight;
      onresize = reDo;
    }
    onerror = null;
  //]]>
  </script>
  <style type="text/css">/*<![CDATA[*/

    < !-- div.WebHelpPopupMenu {
      position: absolute;
      left: 0px;
      top: 0px;
      z-index: 4;
      visibility: hidden;
    }

    p.WebHelpNavBar {
      text-align: right;
    }

    -->
  
/*]]>*/</style>

  <script type="text/javascript">//<![CDATA[

    gRootRelPath = "../..";
    gCommonRootRelPath = "../..";
    gTopicId = "9.1.1_6";
  
//]]></script>

  <script type="text/javascript" src="../../template/scripts/rh.min.js"></script>
  <script type="text/javascript" src="../../template/scripts/common.min.js"></script>
  <script type="text/javascript" src="../../template/scripts/topic.min.js"></script>
  <script type="text/javascript" src="../../template/scripts/topicwidgets.min.js"></script>
<script type="text/javascript" src="../../whxdata/projectsettings.js"></script>
  <link rel="stylesheet" type="text/css" href="../../template/styles/topic.min.css"/>
  <link rel="stylesheet" type="text/css" href="../../template/Charcoal_Grey/topicheader.css"/>
  <meta name="topic-status" content="Draft"/>

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>Expressions And Operators</title>
  <meta name="generator" content="Adobe RoboHelp 2019"/>
  <link rel="stylesheet" href="../../assets/css/default.css" type="text/css"/>
  <meta name="rh-authors" content="Mark Alexander"/>
  <meta name="topic-comment" content="Page outlining the different GML expressions"/>
  <meta name="rh-index-keywords" content="Expressions And Operators,=,&amp;&amp;,||,^^,&lt;,&gt;,&lt;=,&gt;=,==,!=,|,&amp;,^,&lt;&lt;,&gt;&gt;,+,-,*,/,++,--,div,%,mod,!,~"/>
  <meta name="search-keywords" content="expressions,order of operation,operators,div,mod,&amp;&amp;,||,xor,^^,|,^,&amp;,++,--,&lt;&lt;,&gt;&gt;,&lt;=,&gt;=,==,=,:=,$"/>
</head>

<body>
  <div class="topic-header rh-hide" id="rh-topic-header" onclick="rh._.goToFullLayout()">
    <div class="logo">
    </div>
    <div class="nav">
      <div class="title" title="Expressions And Operators">
        <span>Expressions And Operators</span>
      </div>
      <div class="gotohome" title="Click here to see this page in full context">
        <span>Click here to see this page in full context</span>
      </div>
    </div>
  </div>
  <div class="topic-header-shadow rh-hide" id="rh-topic-header-shadow"></div>



  <!--<div class="body-scroll" style="top: 150px;">-->
  <h1>Expressions And Operators</h1>
  <p>An expression is a mathematical phrase that can contain ordinary numbers, variables, strings, or functions as well as one or more <strong>operators </strong>(like add, subtract, multiply, etc...). The values used in an expression can be real numbers
    (e.g. <span class="inline">3.4 * 6</span>), hexadecimal numbers starting with a <span class="inline">$</span> sign (e.g. <span class="inline">$00FFAA | $88FFAA</span>), strings between double quotes (e.g. <span class="inline">&quot;hello&quot;</span>    + <span class="inline">&quot;world&quot;</span>) or more complicated expressions using multiple operators and values.</p>
  <p>For expressions, the following operators exist (in order of priority):</p>
  <p> </p>
  <p><a class="dropspot" data-rhwidget="DropSpot" data-target="drop-down2" href="#"><span data-open-text="true">Assigning (=)</span><span data-close-text="true">Assigning (=)</span></a></p>
  <div class="droptext" data-targetname="drop-down2">
    <p class="dropspot"><b>=</b> - Used to assign a value to a variable. Note that this can <i>also</i> be used for comparing variables in GameMaker Studio 2 and you may see this in examples and other peoples codes. However, this is a legacy from old <i>GameMaker</i> versions
      and you should use the <b>==</b> operators for comparing and <b>=</b> for assigning, as shown in these examples:</p>
    <p class="code">a = 12;<br/> speed = 5;<br/> val = (old_val + 5);</p>
    <p class="dropspot"> </p>
  </div>
  <p><a class="dropspot" data-rhwidget="DropSpot" data-target="drop-down3" href="#"><span data-open-text="true">Combining (&amp;&amp;, ||, ^^)</span><span data-close-text="true">Combining (&amp;&amp;, ||, ^^)</span></a></p>
  <div class="droptext" data-targetname="drop-down3">
    <p class="dropspot"><b>&amp;&amp;, ||, ^^ (and, or and xor)</b> - Combine Boolean values to give either true or false. If any of the following examples resolves to <tt>true</tt> then the code would be run:</p>
    <p class="code">if (a == b &amp;&amp; c == d) {do something...}  // and<br/> if (a == b || c ==d) {do something...}   // or<br/> if (a == b ^^ c == d) {do something...}  // xor</p>
    <p class="dropspot"> </p>
  </div>
  <p><a class="dropspot" data-rhwidget="DropSpot" data-target="drop-down4" href="#"><span data-open-text="true">Comparing (&lt;, &lt;=, ==, !=, &gt;, &gt;=)</span><span data-close-text="true">Comparing (&lt;, &lt;=, ==, !=, &gt;, &gt;=)</span></a></p>
  <div class="droptext" data-targetname="drop-down4">
    <p class="dropspot"><b>&lt;, &lt;=, ==, !=, &gt;, &gt;=</b> - These are comparisons and can only give a <tt>true</tt> or <tt>false</tt> result (where true can also be interpreted as 1, and false as 0). Examples of use:</p>
    <p class="code">if (a &lt; b) {do something...}<br/> if (a != b) {do something...}</p>
    <p class="dropspot"> </p>
  </div>
  <p><a class="dropspot" data-rhwidget="DropSpot" data-target="drop-down5" href="#"><span data-open-text="true">Bitwise (|, &amp;, ^, &lt;&lt;, &gt;&gt;)</span><span data-close-text="true">Bitwise (|, &amp;, ^, &lt;&lt;, &gt;&gt;)</span></a></p>
  <div class="droptext" data-targetname="drop-down5">
    <p class="dropspot"><b>|, &amp;, ^, &lt;&lt;, &gt;&gt;</b>: You can perform bitwise operations with these, where | = bitwise or, &amp; = bitwise and, ^ = bitwise xor, &lt;&lt; = shift left, &gt;&gt; = shift right. Examples of use:</p>
    <p class="code">x = (x &amp; $ffffffe0) + 32;<br/> if (y ^ $1f) &gt; 0 {do something...};</p>
    <p class="dropspot">You can find additional information on how to use the bitwise operators and what they do from the section: <a href="../../Additional_Information/Bitwise_Operators.htm">Bitwise Operators</a>.</p>
    <p class="dropspot"> </p>
  </div>
  <p><a class="dropspot" data-rhwidget="DropSpot" data-target="drop-down6" href="#"><span data-open-text="true">Arithmetical (+, -, *, /)</span><span data-close-text="true">Arithmetical (+, -, *, /)</span></a></p>
  <div class="droptext" data-targetname="drop-down6">
    <p class="dropspot"><b>+, -, *, /</b> - Add, subtract, multiply and divide. Examples of use:</p>
    <p class="code">c = a * b;<br/> str = a + &quot;world&quot;;</p>
    <p class="dropspot"> </p>
  </div>
  <p><a class="dropspot" data-rhwidget="DropSpot" data-target="drop-down7" href="#"><span data-open-text="true">Increment/Decrement (++, --)</span><span data-close-text="true">Increment/Decrement (++, --)</span></a></p>
  <div class="droptext" data-targetname="drop-down7">
    <p class="dropspot"><strong>++</strong>, <strong>--</strong> : Add or subtract one from a value. It is worth noting that placing this before or after the value to be added to or subtracted from will have slightly different results. For example:</p>
    <ul class="dropspotlist">
      <li class="dropspot"><span class="inline">++a</span> will increment the variable and return the incremented value.</li>
      <li class="dropspot"><span class="inline">a++</span> will increment the variable but return the value before it was incremented.</li>
    </ul>
    <p class="dropspot">Therefore, if you have something like this:</p>
    <p class="code">var a = 1;<br/> show_debug_message(string(a++));
      <br/> show_debug_message(string(++a));
    </p>
    <p class="dropspot">The debug output would be 1 and 3. Here are some examples of use:</p>
    <p class="code">for (var i = 0; i &lt; 10; i++;)<br/>     {<br/>     do something...<br/>     };</p>
    <p class="code">if (hit == true)<br/>     {<br/>     --score;<br/>     }</p>
    <p class="note"><strong>NOTE</strong>: On the YoYo Compiler target platforms (those marked (YYC)), these expressions are evaluated from left to right, while on all other target platforms they are evaluated from right to left, meaning that this:</p>
    <p class="code">val = max(num, ++num, num++);</p>
    <p class="note">will give different results depending on the platform.</p>
    <p class="dropspot"> </p>
  </div>
  <p><a class="dropspot" data-rhwidget="DropSpot" data-target="drop-down8" href="#"><span data-open-text="true">Division and Modulo (div, %, mod)</span><span data-close-text="true">Division and Modulo (div, %, mod)</span></a></p>
  <div class="droptext" data-targetname="drop-down8">
    <p class="dropspot"><strong>div, mod (%)</strong> - Division and modulo, where div gives you the amount a value can be divided into producing only an integer quotient, while mod gives you only the remainder of a division. Note that you can only div or mod using integer values.
      Examples of use:</p>
    <p class="code">secs = time mod 60;<br/> time_str = string(time div 60);</p>
    <p class="dropspot"> </p>
  </div>
  <p><a class="dropspot" data-rhwidget="DropSpot" data-target="drop-down9" href="#"><span data-open-text="true">Unary (!, -, ~)</span><span data-close-text="true">Unary (!, -, ~)</span></a></p>
  <div class="droptext" data-targetname="drop-down9">
    <p class="dropspot">Also, the following <strong>unary</strong> operators exist:</p>
    <ul class="dropspotlist">
      <li class="dropspot"><strong>!</strong> : boolean &quot;not&quot;, so <span class="inline">!true == false</span></li>
      <li class="dropspot"><strong>-</strong> : negates the next real or integer value (not valid for strings or booleans)</li>
      <li class="dropspot"><strong>~</strong> : negates the next value bitwise</li>
    </ul>
    <p class="dropspot"> </p>
  </div>
  <p> </p>
  <p><i></i>As values in all expressions you can use numbers, variables, or functions that return a value, and sub-expressions can be placed between brackets too. All operators work for real number values, but <em>comparisons </em>also work for strings and
    the &quot;<strong>+</strong>&quot; operator can be used to concatenate strings.</p>
  <p>When doing multiple operations in a single expression, it is <b>very important</b> that you use brackets <span class="inline">()</span> to separate out the order of operation, as different platforms may perform them differently if not explicitly stated
    in this way. For example, consider the following code:</p>
  <p class="code">a = b == c || d;</p>
  <p>The different target compilers will perform the operations in different orders since they are not explicitly shown, giving rise to &quot;odd&quot; results that you may not expect when you play your game. to avoid this, use the <span class="inline">()</span>    to separate out the parts, like this:</p>
  <p class="code">a = (b == c || d);   //better<br/> a = ((b == c) || d); //best</p>
  <p>Also note that when using various operations and exprsssions in a single code block, these too should be separated. For example, the following <em>looks</em> like valid code:</p>
  <p>if my_var == your_var ++their_var;</p>
  <p>However, the compiler could interpret this in one of two ways:</p>
  <p class="code">if my_var == your_var++ then their_var;<br/>
    <br/> // or<br/>
    <br/> if my_var == your_var then ++their_var;</p>
  <p>Now, you can tell looking at the code that one of those is a bit stupid, but that&#39;s because we know what we are wanting to achieve and what we want to happen, but the compiler doesn&#39;t. All it sees is two variables with the ++ operator between
    them so it has to choose which one to apply it to. Therefore, you should <strong>always explicitly bracket expressions, operations and statements</strong>. The correct version of the above code should be:</p>
  <p class="code">if (my_var == your_var)<br/> {
    <br/> ++their_var;
    <br/> }
  </p>
  <p>This may appear more verbose, but there is no ambiguity about the operations being performed and it will compile and behave consistently across all platforms. Also note that while you can chain expressions and statements without the use of brackets
    at the moment, this is a legacy feature and going forward may be deprecated and removed from GML, so using brackets appropriately now will &quot;future-proof&quot; your code (and is generally good practice anyway).</p>
  <p>Here are some final examples of the various different expressions:</p>
  <p class="code">{<br/> x = 23 div 2;<br/> colour = $FFAA00 + $00BB12;<br/> str = &quot;hello&quot; + &quot;world&quot;;<br/> y += 5;<br/> x *= y;<br/> x = y &lt;&lt; 2;<br/> x = 23 * ((2 + 4) / sin(y));<br/> b = (x &lt; 5) &amp;&amp; !((x == 2) || (x == 4));<br/> }
  </p>
  <p>One final thing to note is that there are also some expression &quot;short-cuts&quot; called <b>accessors</b> for use with certain <a href="../GML_Reference/Data_Structures/Data_Structures.htm">Data Structures</a> and <a href="Arrays.htm">Arrays</a>.
    These enable you to add, or replace data within these formats quickly and easily and without the use of any function calls. For full details, please see the following page</p>
  <ul class="colour">
    <li><a href="Accessors.htm">Accessors</a></li>
  </ul>
  <p> </p>
  <p> </p>
  <p> </p>
  <div class="footer">
    <div class="buttons">
      <div class="clear">
        <div style="float:left">Back: <a href="GML_Overview.htm">GML Overview</a></div>
        <div style="float:right">Next: <a href="Accessors.htm">Accessors</a></div>
      </div>
    </div>
    <h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
  </div>
  <!-- KEYWORDS
div
mod
bitwise operators
bitshift
and
or
xor 
&amp;&amp;
||
^^
%
|
&amp;
^
&lt;&lt;
&gt;&gt;
bitwise or
bitwise and
bitwise xor
bitshift left
bitshift right
&lt;
&lt;=
==
!=
&gt;
&gt;=
++
$
hexadecimal
-->
  <!-- TAGS
expressions_operators
-->

</body></html>