Home | Customize | Blog | Extras | Log In | Info
Manual | D&D icons | GML Parser | Archive | Iso City
Username: Password:  
About | Features | Directory | Banners | Contact

Designing Games with GameMaker
Designing Games with GameMaker

With constructions

As indicated before, it is possible to read and change the value of variables in other instances. But in a number of cases you want to do a lot more with other instances. For example, imagine that you want to move all balls 8 pixels down. You might think that this is achieved by the following piece of code

ball.y = ball.y + 8;

But this is not correct. The right side of the assignment gets the value of the y-coordinate of the first ball and adds 8 to it. Next this new value is set as y-coordinate of all balls. So the result is that all balls get the same y-coordinate. The statement

ball.y += 8;

will have exactly the same effect because it is simply an abbreviation of the first statement. So how do we achieve this? For this purpose there is the with statement. Its global form is

with (<expression>) <statement>

<expression> indicates one or more instances. For this you can use an instance id, the name of an object (to indicate all instances of this object) or one of the special objects (all, self, other, noone). <statement> is now executed for each of the indicated instances, as if that instance is the current (self) instance. So, to move all balls 8 pixels down, you can type.

with (ball) y += 8;

If you want to execute multiple statements, put curly brackets around them. So for example, to move all balls to a random position, you can use

with (ball)
{
  x = random(room_width);
  y = random(room_height);
}

Note that, within the statement(s), the indicated instance has become the self instance. Within the statements the original self instance has become the other instance. So for example, to move all balls to the position of the current instance, you can type

with (ball)
{
  x = other.x;
  y = other.y;
}

Use of the with statement is extremely powerful. Let me give a few more examples. To destroy all balls you type

with (ball) instance_destroy();

If a bomb explodes and you want to destroy all instances close by you can use

with (all)
{
  if (distance_to_object(other) < 50) instance_destroy();
}

Search Search


Alternative versions Alternative versions

You can also read this manual on one single long page (± 1.5 mb)

Also available in: Dutch French German

ZIP Download helpfile

Advertisement Advertisement

GameMaker Manual