Files
QtGameMaker/help/files/401_06_addressing.html
T

133 lines
3.9 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Addressing variables in other instances</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body background="images/back.gif">
<!--START-->
<h3>Addressing variables in other instances</h3>
As described earlier, you can set variables in the current instance using
statements like
<p>
<blockquote><pre>x = 3;</pre></blockquote>
<p>
But in a number of cases you want to address variables in another
instance. For example, you might want to stop the motion of all balls, or
you might want to move the main character to a particular position, or, in
the case of a collision, you might want to set the sprite for the other
instance involved. This can be achieved by preceding the variable name
with the name of an object and a dot. So for example, you can write
<p>
<blockquote><pre><tt>ball</tt>.speed = 0;</pre></blockquote>
<p>
This will change the speed of all instances of object ball. There are a
number of special &quot;objects&quot;.
<ul>
<li><tt>self</tt>: The current instance for which we are executing the
action</li>
<li><tt>other</tt>: The other instance involved in a collision event
</li>
<li><tt>all</tt>: All instances</li>
<li><tt>noone</tt>: No instance at all (sounds weird probably but it
does come in handy as we will see later on)</li>
<li><tt>global</tt>: Not an instance at all, but a container that stores
global variables</li>
</ul>
So, for example, you can use the following kind of statements:
<p>
<blockquote>
<pre>
other.sprite_index = sprite5;
all.speed = 0;
global.message = 'A good result';
global.x = ball.x;
</pre>
</blockquote>
<p>
Now you might wonder what the last assignment does when there are multiple
balls. Well, the first one is taken and its x value is assigned to the
global value.
<p>
But what if you want to set the speed of one particular ball, rather than
all balls? This is slightly more difficult. Each instance has a unique id.
When you put instances in a room in the designer, this instance id is
shown when you rest the mouse on the instance. These are numbers larger
than or equal to 100000. Such a number you can also use as the left-hand
side of the dot. But be careful. The dot will get interpreted as the
decimal dot in the number. To avoid this, put brackets around it. So for
example, assuming the id of the ball is 100032, you can write:
<p>
<blockquote><pre>(100032).speed = 0;</pre></blockquote>
<p>
When you create an instance in the program, the call returns the id. So a
valid piece of program is
<p>
<blockquote>
<pre>
{
nnn = instance_create(100,100,ball);
nnn.speed = 8;
}
</pre>
</blockquote>
<p>
This creates a ball and sets its speed. Note that we assigned the instance
id to a variable and used this variable as indication in front of the dot.
This is completely valid. Let us try to make this more precise. A dot is
actually an operator. It takes a value as left operand and a variable
(address) as right operand, and returns the address of this particular
variable in the indicated object or instance. All the object names, and
the special objects indicated above simply represent values and these can
be dealt with like any value. For example, the following program is valid:
<p>
<blockquote>
<pre>
{
obj[0] = ball;
obj[1] = flag;
obj[0].alarm[4] = 12;
obj[1].id.x = 12;
}
</pre>
</blockquote>
<p>
The last statement should be read as follows. We take the id of the first
flag. For the instance with that id we set the x coordinate to 12.
<p>
Object names, the special objects, and the instance id's can also be used
in a number of functions. They are actually treated as constants in the
programs.
<!--END-->
</body>
</html>
<!-- KEYWORDS
variables, in other instances
self
other
all
global
noone
instance id
-->