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.1_1";
  
//]]></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>Constants</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 explaining the different GML constants"/>
  <meta name="rh-index-keywords" content="Constants,macro,enum"/>
  <meta name="search-keywords" content="constants,macros,enums,macro,enum"/>
</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="Constants">
        <span>Constants</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>Constants</h1>
  <p>A constant is a type of variable that is set once at the start of the game and then never changes. In fact, constant values <em>cannot be changed after they have been declared</em>. This makes them ideal for holding values that are used throughout the
    game to identify special data.</p>
  <p>In the GameMaker Language there are two types of user-defined constant: <strong>macros</strong> and <strong>enums</strong>, both of which are explained below. Also note that any value that is always the same is classed as a constant, regardless of the
    <a href="../Data_Types.htm">data type</a>, for example, a string or the number 3.</p>
  <p class="note"><strong>NOTE</strong>: The GameMaker Language also has a number of built-in constant values that are used to identify specific things. These are outlined on the appropriate pages for the runtime functions that require them in the <a href="../../GML_Reference/GML_Reference.htm">GML Reference</a>    section.</p>
  <p> </p>
  <h2>Macros</h2>
  <p>While not exactly variables, macros are similar to them in how they are used, ie: they are named values that you can use throughout your code to replace <a class="tooltip" title="In computer programming, the term hard-coded is used to describe code that is considered fixed and not likely to change. Hardcoded features are built into hardware or software in such a way that they cannot be modified later on. For example, if you are making a game and &#39;hard-code&#39; the player health to 10, then you would be using the value 10 throughout the game code rather than using a variable.">hard-coded</a> values. Basically, a macro
    is a named variable that holds a constant single value of any <a href="../Data_Types.htm">data type</a>. You can define your own macros using the <a href="../Script_Functions.htm">Script Editor</a> and then use them in your code and DnD™ as if they
    were regular variables, with the one difference being that they <i>can&#39;t be changed in the game</i>.</p>
  <p>The syntax structure for a macro is as follows:</p>
  <p class="code">#macro <i>&lt;variable&gt;</i> <i>&lt;value&gt;</i></p>
  <p>For example say you define the following macro &quot;<span class="inline">total_weapons</span>&quot; (note the preceding &quot;<span class="inline">#</span>&quot; and the lack of a colon &quot;<span class="inline">;</span>&quot; at the end):</p>
  <p class="code">#macro total_weapons 10</p>
  <p>You would then call this in your code like this:</p>
  <p class="code">if ++pos == total_weapons<br/>     {
    <br/>     pos = 0;<br/>     }
  </p>
  <p>Note that you would not be able to change the constant value, so code like this will cause the game to crash:</p>
  <p class="code">total_weapons = 11;</p>
  <p>You can define a macro anywhere in your code or in a script and it will be <i>pre-compiled</i> and included in your game as if it was there from the start, but we recommend that you create a dedicated script asset and define all your macros in there.
    It will be easier to organise and debug later!</p>
  <p>If you need the value of a macro to change at run-time then you should probably make it a <a href="Global_Variables.htm">global variable</a>, since these can be changed from code during a game, unless you set the macro to be a<span> <a href="../Runtime_Functions.htm">runtime </a></span>    <a href="../Runtime_Functions.htm">function</a>. By setting the macro to a function it means that this function will be called every time you use the macro. For example:</p>
  <p class="code">#macro col make_colour_hsv(irandom(255), 255, 255)</p>
  <p>You would then call this macro something like this:</p>
  <p class="code">image_blend = col;</p>
  <p>Using this code will make the image blend a different colour every time the macro is used. It is worth noting that you can also split macros over multiple lines using the <span class="inline"><tt>\</tt></span> character to show where the line breaks.
    An example would be something like:</p>
  <p class="code">#macro hello show_debug_message(&quot;Hello&quot; + \<br/> string(player_name) + \<br/> &quot;, how are you today?&quot;);</p>
  <p>This is purely cosmetic, in that splitting a macro like this will have no effect over the result of the final macro when used, and is simply to provide support for multiline text on macros that have longer lines of code.</p>
  <p>One very important feature of macros is that they can be defined for use with specific <a href="../../../Settings/Configurations.htm">Configurations</a> (configs), meaning you can have the same macro name but give it different values based on the currently
    selected config. For example, say you have a configuration for Android Ads and another for iOS Ads, then you could define a single macro to hold the required app ID value:</p>
  <p class="code">#macro ad_id &quot;&quot;;<br/> #macro Android:ad_id &quot;com.yoyogames.googlegame&quot;<br/> #macro iOS:ad_id &quot;com.yoyogames.appstoregame&quot;</p>
  <p>As you can see, you give the config name first then a colon <span class="inline">:</span> and then the macro name and value. Note that you cannot have any whitespace between the colon <span class="inline">:</span> and either the config name nor the
    macro name otherwise you will get an error.</p>
  <p> </p>
  <h2>Enums</h2>
  <p>An enum is an &quot;enumerator&quot;, and it essentially permits you to create your own limited data type with a list of constant values, and they have the following structure:</p>
  <p class="code">enum <i>&lt;variable&gt; </i>{<br/>
    <i>&lt;constant&gt;</i> [= <i>&lt;value&gt;</i>],<br/>
    <i>&lt;constant&gt;</i> [= <i>&lt;value&gt;</i>],<br/> // etc...<br/> }
  </p>
  <p>In the following example, we create an enum for the colours of the rainbow and assign it various constants and default values:</p>
  <p class="code">enum rainbowcolours {<br/>     red,<br/>     orange,<br/>     yellow,<br/>     green,<br/>     blue,<br/>     indigo,<br/>     violet<br/>     }</p>
  <p>The enum entries can only be <b>integer numbers</b> or <b><a class="tooltip" title="An expression is a combination of one or more constants, variables, operators, and/or functions that are interpreted according to particular rules of precedence and association to return another value. A simple expression would be (5 + 5), which returns 10.">expression</a><span class="glossextra">s</span> with previous enums that evaluate to an integer number</b>, and by default are numbered
    from 0 upwards, so our example given above would default to <tt>red = 0</tt>, <tt>orange = 1</tt>, <tt>yellow = 2</tt>, etc...</p>
  <p>You can also assign values to the enum variables at the time of creation:</p>
  <p class="code">enum enum_test {<br/> val = 10;<br/> }
    <br/>
    <br/> enum rainbowcolours {<br/>     red = 5,<br/>     orange = 5 * 2,<br/>     yellow = 15,<br/>     green = 20,<br/>     blue = 25,<br/>     indigo = 30,<br/>     violet = 35 * enum_test.val<br/>     }</p>
  <p>Notice in the above example we use another enum to create an expression for &quot;violet&quot;. This only works if the enum being referenced was created <i>before</i> the enum that is using it in an expression, but it will not work for variables or
    functions, since the enum value must be able to be evaluated as a constant when the project is <a class="tooltip" title="Compiling is when GameMaker takes your code and collects it together in such a way as to create an executable package that will run on a device as a game.">Compiling</a>. Also note that all enum values evaluate to <b>integer</b> values, and when you create
    your own you should be aware that <i>only integer values are permitted</i> for enums to work. This value can be any integer number that a floating point double precision number can represent, including negative values.</p>
  <p>To later access the value within a given enum type, you can use the point <tt>&quot;.&quot;</tt> method, like this:</p>
  <p class="code">variable = &lt;enum_name&gt;.&lt;<em>enum_variable</em>&gt;;</p>
  <p>As an example, let&#39;s use the &quot;<span class="inline">rainbowcolours</span>&quot; enum that we created in the code above:</p>
  <p class="code">colour_value = rainbowcolours.green * rainbowcolours.red;</p>
  <p>The <span class="inline">colour_value</span> variable would now hold the value 100 (20 * 5).</p>
  <p>Note that you <em>cannot </em>modify the values for any enum constant after it has been created, much the same as you can&#39;t modify macros after they have been created.</p>
  <p> </p>
  <p> </p>
  <p> </p>
  <div class="footer">
    <div class="buttons">
      <div class="clear">
        <div style="float:left">Back: <a href="../Variables_And_Variable_Scope.htm">Variables And Variables Scope</a></div>
        <div style="float:right">Next: <a href="Local_Variables.htm">Local Variables</a></div>
      </div>
    </div>
    <h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
  </div>
  <!-- KEYWORDS
constants
macros
enums
macro
enum
-->
  <!-- TAGS
constants
macro
enum
-->

</body></html>