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.0_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>Local 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 local variables"/>
  <meta name="rh-index-keywords" content="var,Local Variables"/>
  <meta name="search-keywords" content="local variables,local,var,local scope"/>
</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="Local Variables">
        <span>Local 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>Local Variables</h1>
  <p>A <strong>local</strong> variable is one that we create for a specific <strong>event</strong> or <strong>function</strong> <i>only</i> and then discard when the event or function has finished. If it is created in a custom function then the local variable
    is only available to the function and then discarded when the function has finished. Why would we need them? Well, variables take up space in memory and it may be that we are only going to use them for one operation or function in which case we only
    need to have it in memory for that short time that it&#39;s used. This keeps your code base clean and tidy as well as keeping memory space optimised for the things that really need it. To declare a local variable we use the <span class="inline"><strong><tt>var</tt></strong></span> operator
    like this:</p>
  <p class="code">var _i, _num, _str;<br/> _i = 0;<br/> _num = 24.5;<br/> _str = &quot;Hello World&quot;;<br/>
    <br/> // or<br/>
    <br/> var _i = 0, _num = 24.5, _str = &quot;Hello World&quot;;<br/>
    <br/> // or<br/>
    <br/> var _i = 0;<br/> var _num = 24.5;<br/> var _str = &quot;Hello World&quot;;</p>
  <p>All of the variables created in this way will be &quot;forgotten&quot; (ie: removed from memory) at the end of the event (or function) in which they were created. You should be careful that the name you give all <tt>var</tt> declared variables does
    not coincide with another instance variable within the object running the code, and also make sure that you have no intention of using the value stored in that variable outside of the event or function you declare it in (in the examples above, all
    the <tt>var</tt> declared variables have been defined with the underscore &quot;_&quot; preceding the variable name, but this is not required and is done simply to make it more obvious in the code that we are dealing with a local variable). That said,
    if you find yourself in a situation where you think a local variable <em>should</em> be the same as an instance variable, then you can use the <span class="inline">self</span> <a href="../Instance_Keywords.htm">keyword</a> to identify the instance
    variable as separate, for example:</p>
  <p class="code">var hp = 10;<br/> with (obj_Enemy)<br/> {
    <br/> self.hp -= hp;<br/> }
  </p>
  <p>Local variables are used a lot in programs, especially in loops for counting <a class="tooltip" title="An iteration is a single pass through a set of operations in your project code. One form of iteration in computer programming is via loops. A loop will repeat a certain segment of code until a condition is met and it can proceed further. Each time the computer runs a loop, it is known as an iteration. In simple terms, iteration is the process of repeating a particular snippet of code over and over again to perform a certain action.">iteration</a><span class="glossextra">s</span>, or when using a value several times in one operation that is not going
    to be repeated again. Here are another couple of examples:</p>
  <p class="code">var _i = 0;<br/> repeat (10)<br/>     {
    <br/>     inventory[_i] = 0;<br/>     _i += 1;<br/>     }
  </p>
  <p>The above code creates a local variable called &quot;<span class="inline">_i</span>&quot; and sets it to 0, all in the same line. Note that in previous versions of <i>GameMaker</i> you had to declare your local variables first and <i>then</i> assign
    them values, but in this version you can declare <i>and</i> assign them a value at the same time. The above code then uses this variable to initialize an <a href="../Arrays.htm">array</a>. As the variable &quot;<span class="inline">_i</span>&quot;
    is not going to be used for any further functions in the instance other than this, it can be local in scope. Here is one more example:</p>
  <p class="code">var _x, _y;<br/> _x = x - 32 + irandom(64);<br/> _y = y - 32 + irandom(64);<br/> instance_create_layer(_x, _y, &quot;Effects&quot;, obj_blood);</p>
  <p>Here we have used the local variables &quot;<span class="inline">_x</span>&quot; and &quot;<span class="inline">_y</span>&quot; to store some random coordinates that we then use to create an instance. In this example you can see that it is not strictly
    necessary that we use these variables but for the sake of readability and ease of use, we do. It is MUCH clearer and obvious what we are doing there than if we used this code:</p>
  <p class="code">instance_create_layer(x - 32 + irandom(64), y - 32 + irandom(64), &quot;Effects&quot;, obj_guts);</p>
  <p>One other thing about <tt>var</tt> declared local variables should be noted... Since they are unique to the event or function that runs them, they can be used in any other instances through code too! This means that we can use these variables to set
    and change things in other instances using the &quot;<span class="inline">with()</span>&quot; construct (see the section on <a href="../Variables_And_Variable_Scope.htm">variable scope</a> for more information). The actual code itself would look something
    like this:</p>
  <p class="code">var num = instance_number(obj_Enemy);<br/> with (obj_Enemy)<br/>     {
    <br/>     if num&gt;10 instance_destroy();<br/>     }
  </p>
  <p>The above code works because the <tt>var</tt> declared variable is local to the <i>event</i> (or function) it is contained in, not the instance, nor the game world, and so can be used in any function in any object as long as it is in the same code block.</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="Instance_Variables.htm">Instance Variables</a></div>
      </div>
    </div>
    <h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
  </div>
  <!-- KEYWORDS
local variables
var
-->
  <!-- TAGS
var
local_variables
-->

</body></html>