<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<script type="text/javascript" language="JavaScript">
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">
< !-- div.WebHelpPopupMenu {
position: absolute;
left: 0px;
top: 0px;
z-index: 4;
visibility: hidden;
}
p.WebHelpNavBar {
text-align: right;
}
-->
</style>
<script type="text/javascript">
gRootRelPath = "../..";
gCommonRootRelPath = "../..";
gTopicId = "9.1.1_3";
<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>Instance Keywords</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 instance "keywords""/>
<meta name="rh-index-keywords" content="Instance Keywords,noone,self,other,all"/>
<meta name="search-keywords" content="self,all,noone,other,instance keywords"/>
</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="Instance Keywords">
<span>Instance Keywords</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>
<h1>Instance Keywords</h1>
<p>To make certain things easier in GameMaker Studio 2, you can use one of several <b>instance keywords</b> in your code (whether GML or Drag and Drop™). These keywords are used primarily to identify instances under different situations and each one is
explained in the text below. Note that all keywords are represented by a <strong>negative integer value</strong> internally, so care must be taken when assigning or checking variable values against or with these keywords, as you may get unexpected
results later as GameMaker Studio 2 interprets the value you have used as something else. You should also note that using the values instead of the keywords in your code is <b>not at all recommended</b> and could cause issues later on.</p>
<p> </p>
<p><a class="dropspot" data-rhwidget="DropSpot" data-target="drop-down" href="#"><span data-open-text="true">self</span><span data-close-text="true">self</span></a></p>
<div class="droptext" data-targetname="drop-down">
<table>
<tbody>
<tr>
<th>Keyword</th>
<th>Description</th>
<th>value</th>
</tr>
<tr>
<td>self</td>
<td>The instance which is executing the current block of code.</td>
<td> -1</td>
</tr>
</tbody>
</table>
<p class="dropspot"> </p>
<p class="dropspot"><tt>self</tt> can be used to identify the calling instance of the current block of code, as it always references the unique ID for the instance currently in scope. For example:</p>
<p class="code">var val = 100;<br/> with (instance_create_layer(x, y, "Instances", obj_Fire))<br/> {
<br/> self.val = val;<br/> }
</p>
<p class="dropspot">In this example you can see that we have a <i>local</i> variable called <tt>val</tt> and we want it to set the <i>instance</i> variable with the same name in the newly created object instance. To identify the instance variable correctly and tell GameMaker
Studio 2 to set it in the instance calling the code block, we use the <tt>self</tt> keyword. In most cases you can also use the <tt>id</tt><span> built-in </span>instance variable instead of <tt>self</tt>, but <tt>self</tt> offers certain benefits.
To start with, it is faster for the compiler to retrieve the instance ID value using <tt>self</tt> rather than <tt>id</tt>, as the <tt>id</tt> value goes through the instance lookup table. Secondly, for those people making extensions, it is very
useful to ensure the correct scoping of variables, since it is possible that a project which uses an extension may have a global scope variable or something with the same name as a variable in the extension.</p>
<p class="dropspot">It is also worth noting that <tt>self</tt> can also be used within <a href="Structs.htm">structs</a> - under very specific circumstances - to reference member variables for the struct.</p>
<p class="dropspot"> </p>
</div>
<p><a class="dropspot" data-rhwidget="DropSpot" data-target="drop-down1" href="#"><span data-open-text="true">other</span><span data-close-text="true">other</span></a></p>
<div class="droptext" data-targetname="drop-down1">
<table>
<tbody>
<tr>
<th>Keyword</th>
<th>Description</th>
<th>value</th>
</tr>
<tr>
<td>other</td>
<td>The other instance involved in a collision event, or the other instance from a <tt>with</tt> function.</td>
<td> -2</td>
</tr>
</tbody>
</table>
<p> </p>
<p class="dropspot">The special <i>keyword</i> <b>other</b> has two different ways that it can be used to reference a specific instance: when used in a <tt><b>with</b></tt> function (explained <a href="Language_Features/with.htm">here</a>) or when used in a <a href="../../The_Asset_Editors/Object_Properties/Object_Events.htm">collision event</a>,
which is what this section is going to explain.</p>
<p class="dropspot">A collision event can only happen between two instances. You <i>can</i> have multiple collisions between multiple instances, but they are all resolved by GameMaker Studio 2 on a 1-on-1 basis, with the instance that has the collision event and the
"other" instance that is involved. Imagine you have a player object, multiple enemy objects and multiple bullet objects that the enemy can fire at you. You can assign each enemy a single bullet instance but with a different damage variable
randomly assigned to it when created, for example:</p>
<p class="code">var nnn;<br/> nnn = instance_create_layer(x, y, "Bullets", obj_Bullet);<br/> nnn.damage = 5 + irandom(5);<br/> nnn.speed = 8;<br/> nnn.direction = point_direction(x, y, obj_Player.x, obj_Player.y);</p>
<p class="dropspot">You can see how we set its variables using the "point method" outlined in the section on <a href="Addressing_Variables_In_Other_Instances.htm">Addressing Variables In Other Instances</a>. This will give the bullet objects a different damage
value. But what about the player object? How will it detect the damage that it has to take? By using <b><tt>other</tt></b> in the collision event:</p>
<p class="code">hp -= other.damage;<br/> if hp <= 0 instance_destroy();</p>
<p class="dropspot">The above code will deduct the amount stored in the <i>other</i> instance in the collisions "damage" variable from the player "hp" variable, then it will check to see if the "hp" is lower than or equal to 0. If it is
then it will destroy the player object. Please note that <b>other</b> used in this way <i>only works in the collision event</i> and that the other instance must have the variable being checked or else an error will be thrown. However you can assign
values to variables, or even create new ones, using <b>other</b> in the collision event too, like this:</p>
<p class="code">//add ten to the other instance "mana" variable<br/> other.mana += 10;<br/> //set the other instance variable "hit" to true, creating it if the variable doesn't already exist<br/> other.hit = true;</p>
<p class="dropspot"> </p>
</div>
<p><a class="dropspot" data-rhwidget="DropSpot" data-target="drop-down2" href="#"><span data-open-text="true">all</span><span data-close-text="true">all</span></a></p>
<div class="droptext" data-targetname="drop-down2">
<table>
<tbody>
<tr>
<th>Keyword</th>
<th>Description</th>
<th>value</th>
</tr>
<tr>
<td>all</td>
<td>All instances currently active in the room.</td>
<td> -3</td>
</tr>
</tbody>
</table>
<p> </p>
<p class="dropspot">This keyword is used to tell GameMaker Studio 2 that a function is to be applied, or to check, all active instances within a room (deactivated instances will not be checked or accessed). You <b>cannot</b> use <tt>all</tt> to access or set variables
in other instances using the point method (see <a href="Addressing_Variables_In_Other_Instances.htm">here</a>), but you <strong>can </strong>use it when calling <a href="Language_Features/with.htm"><tt>with()</tt></a>, for example:</p>
<p class="code">with (all)<br/> {
<br/> speed = 0;<br/> }
</p>
<p class="dropspot">The above code will set the speed of all instances in the room to 0. You can also use <tt>all</tt> within functions to target or check all instances in the room for example:</p>
<p class="code">//Check a point for any active instance in the room<br/> inst = instance_position(mouse_x, mouse_y, all); <br/>
<br/> //Check all instances for a collision along a line<br/> if collision_line(x, y, mouse_x, mouse_y, all, false, true) {}<br/>
<br/> //Add all instances in the room into a motion planning grid<br/> mp_grid_add_instances(grid, all, false);</p>
<p class="dropspot"><tt>all</tt> is a very useful keyword and can be used in numerous situations within your code and actions, often cutting down on the amount of code you need to write to achieve a desired effect.</p>
<p class="dropspot"> </p>
</div>
<p><a class="dropspot" data-rhwidget="DropSpot" data-target="drop-down3" href="#"><span data-open-text="true">noone</span><span data-close-text="true">noone</span></a></p>
<div class="droptext" data-targetname="drop-down3">
<table class=" cke_show_border">
<tbody>
<tr>
<th>Keyword</th>
<th>Description</th>
<th>value</th>
</tr>
<tr>
<td>noone</td>
<td>No instance at all.</td>
<td> -4</td>
</tr>
</tbody>
</table>
<p> </p>
<p class="dropspot">It may seem odd, but many times while programming your games will you find the need to check if there are no instances found at a location, or in a collision etc... In those cases you would use this keyword to check for nothing, something like this:</p>
<p class="code">if instance_nearest(x, y, obj_enemy) != noone<br/> {
<br/> //do something as there is an enemy instance near<br/> }
</p>
<p class="dropspot">In this example, the function <tt>instance_nearest()</tt> will return either <tt>noone</tt> or the unique ID of the nearest found instance. Basically, any time that you need to check for an instance, you can expect to get either <tt>noone</tt> or
a unique instance ID returned.</p>
<p class="dropspot"> </p>
</div>
<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="Addressing_Variables_In_Other_Instances.htm">Addressing Variables In Other Instances</a></div>
</div>
</div>
<h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
</div>
</body></html>