In GameMaker, the "z index" or depth of an instance determines its drawing order on the screen. Instances with lower depths are drawn on top of those with higher depths. Here's how you can manipulate it:
You can set the depth of an instance directly using the
depth variable in the object's Create Event or at any point within its events like this:
At runtime, you might want to change an instance's depth based on certain conditions. You can assign a new value to
depth whenever needed:
If you have multiple instances that need to be sorted dynamically, you might use
instance_create_layer() function and specify the layer, which inherently controls depth:
Layers are the preferred method for managing depth since GameMaker Studio 2. Layers have their own depth values and instances placed on a given layer will be rendered according to the layer's depth.
To adjust the depth of an entire layer and thus affect all instances on that layer, you can use:
This will move the whole layer "InstancesLayer" above other layers with a higher depth value.
depth directly on instances is simple, but working with layers provides a more powerful and organized way to manage rendering order in your game. Use layers when possible for better control over what gets drawn first.