GML Functions: Deactivating Instances

When you create large room, for example in platform games, with a small view, many instances lie outside the view. Such instances though are still active and will execute their events. Also when performing collision checks these instances are taken into account. This can cost a lot of time, which is often not necessary. (For example, often it is not important whether instances outside the view move.) To solve this problem Game Maker contains some functions to deactivate and activate instances. Before using them you must though clearly understand how they work.

When you deactivate instances they are in some sense removed from the game. They are not visible anymore nor are any events executed for them. So for all actions and functions they don't exist anymore. This saves a lot of time but you have to be careful. For example, when you delete all instances of a particular type, deactivated instances are not deleted (because they don't exist). So don't think that a key a player picks up can unlock a deactivated door.

The most crucial mistake you can make is to deactivate the instance that is responsible for the activation. To avoid this some of the routines below allow you to insist that the calling instance should not be deactivated itself.

Here are the available routines:


 * instance_deactivate_all(notme) Deactivates all instances in the room. If notme is true the calling instance is not deactivated (which is normally what you want).
 * instance_deactivate_object(obj) Deactivates all instances in the room of the given object. You can also use all to indicate that all instances must be deactivated or the id of an instance to deactivate an individual instance.
 * instance_deactivate_region(left,top,width,height,inside,notme) Deactivates all instances in the indicated region (that is, those whose bounding box lies partially inside the region). If inside is false the instances completely outside the region are deactivated. If notme is true the calling instance is not deactivated (which is normally what you want).
 * instance_activate_all Activates all instances in the room.
 * instance_activate_object(obj) Activates all instances in the room of the given object. You can also use all to indicate that all instances must be activated or the id of an instance to activate an individual instance.
 * instance_activate_region(left,top,width,height,inside) Activates all instances in the indicated region. If inside is false the instances completely outside the region are activated.

For example, to deactivate all instances outside the view and activate the ones inside the view, you could place the following code in the step event of the moving character:

{   instance_activate_all; instance_deactivate_region(view_xview[0],view_yview[0],view_wview[0],view_hview[0],false,true); }

In practice you might want to use a region slightly larger than the view.