depth


Description

When you create an object you can assign it an initial depth which defines how the instances of that object will be drawn in the room when the game is being played and this variable can be used to get and to change that depth value while the game is running. You would normally not need to use this variable as you should be setting instances to be added to discreet layers, which in turn are set to a specific depth, but it may be that you want to change the depth of an instance using this value, in which case a "temporary layer" will be created specifically for the instance at the given depth. Note that when no instances are on the same depth then this temporary layer will be removed from memory (unlike regular layers which will remain even if they have nothing on them).

IMPORTANT! When you modify the depth variable and GameMaker STudio 2 manages the layers, the built in layer variable will return -1 and not the layer ID, since managed layers cannot be manipulated through code.


In GameMaker Studio 2 the lower the depth value for an instance, the "closer to the camera" that instance will be drawn, while a higher depth value means that the instance will be drawn "further away from the camera", i.e: -1000 is drawn on top of -100, which is drawn on top of 0, which is drawn on top of 100, which is drawn on top of 1000.

Note that instances of an object that have the same depth can be drawn above or below each other even when they appear to be placed one on top of the other in the room editor. If you want to guarantee that something is drawn over or under everything else, you should always set the depth (or layer) explicitly.

NOTE: You cannot set the depth of an instance in its draw event (all other events are fine).


Syntax:

depth;


Returns:

Real (single precision floating point value)


Example:

if y != yprevious
    {
    depth = -y;
    }

The above code will check to see if the y position has changed and if it has then the depth will also be set to correspond to it.