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";
  
//]]></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>Global Variables</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 global variables"/>
  <meta name="rh-index-keywords" content="global,globalvar,Global Variables"/>
  <meta name="search-keywords" content="global,global variables,globalvar"/>
</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="Global Variables">
        <span>Global Variables</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>Global Variables</h1>
  <p>A basic description of a <strong>global </strong>variable is one that, once declared, it belongs to no instance in particular and yet can be accessed by all. Just like <a href="Local_Variables.htm">local variables</a>, global variables must be declared
    using an identifier, but unlike a local variable, a global variable remains in memory <em>until the end of the game</em>. So, you can create a global variable to keep track of (for example) the number of bullets that the player has and then just update
    this variable at different points in the game, form any instance or function and at any time. Essentially, a global variable does not belong to any specific instance and can be accessed, changed and used by all instances, and any changes made to the
    variable are &quot;global&quot;, in that all instances using the variable will be affected by the change. Let&#39;s have a look at an example:</p>
  <p class="code">global.food = 5;</p>
  <p>We declare the &quot;<span class="inline">food</span>&quot; variable by first writing &quot;<span class="inline">global</span>&quot; and then a &quot;<span class="inline">.</span>&quot; to tell GameMaker Studio 2 that this variable is now global scope.
    We will need to use this form from now on any time we are required to access or to change this variable in any way. So, we have created a new variable called &quot;<span class="inline">food</span>&quot; and we have declared it as global. Now, any
    instance or function can use and change this variable in any way and all other instances will &quot;see&quot; this. For example we could have a different food object that the player collides with and in the collision event we have:</p>
  <p class="code">global.food +=1;</p>
  <p>We also have another object that draws this value like this:</p>
  <p class="code">draw_text(32, 32, &quot;food = &quot; + string(global.food));</p>
  <p>With global variables we can change values and see those changes reflected in all instances of the objects that reference this variable. As with <i>local</i> variables you have to take care not to name your global variables the same as any instance
    variables as that may cause you problems and make bugs creep into your games due to variable overlap, which can be a difficult issue to debug sometimes. In general you should have a single object that declares all your global variables at the very
    start of the game (for example, in the <a href="../../../The_Asset_Editors/Object_Properties/Other_Events.htm">Room Start Event</a> of the first object instance placed in the first room of the game) or a single <a href="../Script_Functions.htm">script function</a>    that declares them all together, as this gives you a handy place to go back and reference everything at once should you need to check a variable name or edit a value.</p>
  <p>GameMaker Studio 2 has a collection of &quot;built in&quot; global variables too, so you should be aware of them as you may name one of your instance variables the same or wish to have your own global variable with the same name and wonder why you are
    getting errors! They are easy to spot, however, as they are shown in a different colour in the code editor and also come up in the auto-complete bar at the bottom. The majority of built in global variables are very specific and will only be used on
    rare occasions - and are listed in the appropriate sections of the manual - but there is one important one that is used frequently and isn&#39;t listed elsewhere:</p>
  <ul class="colour">
    <li><a href="Builtin_Global_Variables/async_load.htm"><tt>async_load</tt></a></li>
  </ul>
  <p>There are also three <b>deprecated</b> built in global variables which you should be aware of (these variables are only designed to support legacy projects from previous versions of GameMaker and should <b><i>not be used in new projects</i></b>):</p>
  <ul class="colour">
    <li><a href="Builtin_Global_Variables/score.htm"><tt>score</tt></a></li>
    <li><a href="Builtin_Global_Variables/health.htm"><tt>health</tt></a></li>
    <li><a href="Builtin_Global_Variables/lives.htm"><tt>lives</tt></a></li>
  </ul>
  <p>Finally, there are two variables that can be used for script functions and methods:</p>
  <ul class="colour">
    <li><a href="Builtin_Global_Variables/argument.htm">argument</a></li>
    <li><a href="Builtin_Global_Variables/argument_count.htm">argument_count</a></li>
  </ul>
  <p>The following form can also be used to declare global variables, but it is <i><b>only included for backwards compatibility</b></i>, and it is not recommended that you use this form for new projects as future versions of GameMaker may not support it.</p>
  <p>The second form for creating global variables is to declare them as such using the <tt>globalvar</tt> declaration, much as you would a local variable using the <tt>var</tt> declaration.</p>
  <p class="note"><b>IMPORTANT!</b> The <tt>globalvar</tt> declaration is <b>deprecated</b> and only supported for legacy purposes. You should <b>always</b> use the <tt>global.</tt> identifier to mark global variables.</p>
  <p>This (deprecated) declaration would be used as follows:</p>
  <p class="code">globalvar food;<br/> food = 5;</p>
  <p>Once declared in this way that variable &quot;food&quot; is now considered global and requires no <tt>global.</tt> prefix - which also means that it&#39;s a lot harder to identify global variables in your code and it&#39;s also much easier to get variable
    overlap as you use the same variable name in different objects or from extensions that you&#39;ve installed. Once declared in this way the global variable is accessed as follows:</p>
  <p class="code">food += 2;</p>
  <p>or:</p>
  <p class="code">draw_text(32, 32, &quot;food = &quot; + string(food));</p>
  <p>As you can see, with nothing to show that the variable is global in scope you are potentially setting yourself up for many subtle problems to arise in your game, which is why this declaration should be avoided.</p>
  <p>There are a few functions designed to help you when dealing with global variables, which you can find on the following page:</p>
  <ul class="colour">
    <li><a href="../../GML_Reference/Variable_Functions/Variable_Functions.htm">Variable Functions</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="../Variables_And_Variable_Scope.htm">Variables And Variables Scope</a></div>
        <div style="float:right">Next: <a href="Constants.htm">Constants</a></div>
      </div>
    </div>
    <h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
  </div>
  <!-- KEYWORDS
global
globalvar
global variables
-->
  <!-- TAGS
global_variables
-->

</body></html>