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_4";
  
//]]></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>Addressing Variables In Other Instances</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 hoe to address variables in other instances"/>
  <meta name="rh-index-keywords" content="Addressing Variables In Other Instances"/>
  <meta name="search-keywords" content=""/>
</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="Addressing Variables In Other Instances">
        <span>Addressing Variables In Other Instances</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>Addressing Variables In Other Instances</h1>
  <p>In the sections dedicated to <a href="Variables_And_Variable_Scope.htm">variables</a> you found out how to create and use variables within a single instance, or on a global scope, but what happens if you want one instance to access a variable in another,
    different instance? There are many cases when you may want to do this, for example in a collision with a bullet object, you may want to find out how much damage the bullet does by accessing a variable in the bullet, or you might want to stop the motion
    of all the balls in a puzzle, or you might want to move the main character to a particular position, or any number of other situations you typically come across in any game. Thankfully the GameMaker Language comes equipped with mechanisms to achieve
    this.</p>
  <p>One of the most common methods for accessing or changing a variable in another instance is to use its object name as an identifier and then use a point &quot;<span class="inline">.</span>&quot; to tell GameMaker Studio 2 that the variable used after
    is to be assigned or changed in that object. The syntax for this is:</p>
  <p class="code">&lt;object_id&gt;.&lt;<em>variable</em>&gt; = &lt;value&gt;; </p>
  <p>In practice it would look like this:</p>
  <p class="code">obj_ball.speed = 0;</p>
  <p>With the above code you are setting the speed of an instance of &quot;<tt>obj_ball</tt>&quot;. However if you have more than one instance of the given object in the room, <em>then it will apply to ALL of them equally</em> - unless you are using <b>HTML5</b>,
    in which case it will affect only <em>one</em>, but you have no way of knowing which one it will affect - so if you need to access all instances of an object, you should be using <a href="Language_Features/with.htm"><tt>with()</tt></a>, as that is
    100% cross platform compatible. In general, this format should only be used when you have a single instance of the object in the room, or (as you will see in the next part) when you have a specific <i>instance <b>ID</b></i>.</p>
  <p>You can also access a single instance of an object when there are multiple instances within the room using the unique <b>instance name</b> to tell GameMaker Studio 2 exactly which instance we wish to address. The <b>instance name constant</b> is the
    unique identifying constant that is given to each and every instance added to a room in your game. You can find this constant by double clicking on an instance in the <a href="../../The_Asset_Editors/Rooms.htm">room editor</a>:</p>
  <p><img alt="The instance constant in the room editor" class="center" src="../../assets/Images/Scripting_Reference/GML/Overview/Instance_Constant.png"/>Note that this name can be be edited and given a more descriptive name - although the name <em>must</em> be
    unique to the entire game - and it can be used as the left-hand side of the point:</p>
  <p class="code">inst_4DB70D2.speed = 0;</p>
  <p>By far the most common and practical method, however, is to use a <i>variable</i> on the left of the point, as long as the variable in question<i> has stored a valid <b>instance id</b></i>. The following examples illustrate this.</p>
  <p class="code">// Example 1<br/> var _inst = instance_position(mouse_x, mouse_y, all);<br/> if instance_exists(_inst)<br/>     {
    <br/>     _inst.speed = 0;<br/>     }
    <br/>
    <br/> // Example 2<br/> var _inst = instance_create_layer(mouse_x, mouse_y, &quot;Enemies&quot;, obj_E_Parent);<br/> _inst.direction = point_direction(_inst.x, _inst.y, x, y);<br/> _inst.target = id;</p>
  <p>In the above code for Example 1, there is an <a href="../GML_Reference/Asset_Management/Instances/instance_exists.htm"><tt>instance_exists()</tt></a> call in the code block. This is because using the point method to access or change another instances
    value will give an error and crash the game if the instance does not exist, and there is the possibility that this is the case in that example. We don&#39;t need the check however in Examples 2 and 3 because we <i>know</i> that the instance is there
    since in Example 2 we created it, and in Example 3, its the other instance in a collision event. However, if there is <i>any</i> possibility that the instance could be destroyed, deactivated, or otherwise removed from the room while using this method,
    you should <i>always</i> check beforehand using the <tt>instance_exists()</tt> function or the <a href="../GML_Reference/Asset_Management/Instances/instance_number.htm"><tt>instance_number()</tt></a> function.</p>
  <p>Note that you cannot use the special <a href="Instance_Keywords.htm">keyword</a> &quot;<tt>all</tt>&quot; with this method to target all instances(eg: <span class="inline">all.val = 10</span> would give an error), but you <i>can</i> use the keywords
    &quot;<tt>other</tt>&quot; and &quot;<tt>self</tt>&quot; without issues. For example, using <tt>other</tt> in a <a href="../../The_Asset_Editors/Object_Properties/Object_Events.htm">collision event</a>:</p>
  <p class="code">// Example 3 other.hp -= 10;<br/> if other.hp &lt;= 0<br/>     {
    <br/>     other.sprite_index = spr_E_Dead;<br/>     }
  </p>
  <p>It is also important to note that you cannot use any function by itself as the left-hand side of an assignment. For example, the following code would give you an error:</p>
  <p class="code">instance_nearest(x, y, obj).speed = 0;</p>
  <p>The return value for the expression in that code example is an integer number (the unique ID value for the nearest instance) and so it must be enclosed in brackets <span class="inline">()</span> to be used in this way and properly address the instance
    required. The above code would be correctly written as:</p>
  <p class="code">(instance_nearest(x, y, obj)).speed = 0;<br/>
    <br/> //or
    <br/>
    <br/> var inst = instance_nearest(x, y, obj);<br/> inst.speed = 0;</p>
  <p>These are all perfectly valid ways of reading, changing and setting variables in other instances, and work because the point is actually an <i>operator</i>. It takes a value as the left <a class="tooltip" title="An operand is a term used to denote the values which can be manipulated using different operators. In the expression A + B + C, A, B and C are the operands.">operand</a>    and a variable as the right operand, and returns the address of this particular variable in the indicated object or instance. All the object names, constants, IDs etc... simply represent values and these can be dealt with like any other value.</p>
  <p>The other way to reference variables in another instance is to use the GameMaker Language function <span class="inline">with()</span>, which is discussed in detail <a href="Language_Features/with.htm">here</a>:</p>
  <p class="code">// This will affect all instances of the object &quot;obj_Enemy&quot;<br/> with (obj_Enemy)<br/>     {<br/>     target = other.parent;<br/>     }<br/>
    <br/> // This will affect one instance of the object &quot;obj_Enemy&quot;<br/> var _enemy = instance_nearest(x, y, obj_Enemy);<br/> if instance_exists(_enemy)<br/>     {<br/>     with (_enemy)<br/>         {<br/>         target_x = mouse_x;<br/>            target.y = mouse_y;<br/>         }<br/>     }</p>
  <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="Evaluation_Order.htm">Evaluation Order</a></div>
      </div>
    </div>
    <h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
  </div>
  <!-- KEYWORDS
variables - addressing
addressing variables
-->
  <!-- TAGS
addressing_variables
-->

</body></html>