• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Question about 'Object depth'...

T

thegabba

Guest
Hi guys, I'm trying to order the objects in my scene and I was wondering how does the 'depth' work.
I know I can 'force' the depth of an object by changing it through code and that's the only way to make it work in my project.
I prefer to organize my layer 'visually' in the room editor and I've read the way to do that is by changing the objects order in the 'instance creation order' window.
I know it 'reads' from top down, but even if I put the object I want the more far away from my camera as the first object, it remains the closest to the camera.
I hope you can help me, thanks in advance.

Gabba
 

Slyddar

Member
Don't rely on the instance creation order to sort your instances. If you are using Gamemaker 2 you should use layers in the room editor to control the sort order.
 
T

thegabba

Guest
Don't rely on the instance creation order to sort your instances. If you are using Gamemaker 2 you should use layers in the room editor to control the sort order.
Ok, thank you Slyddar! But I was wondering why changing the objects order in the 'instance creation order' window does not change anything...
Furthermore, Is there an easy way to put all my objects in a new layer by copying them or dragging them into it or I have to manually put them one by one in the new layer?
Thanks again
 

Slyddar

Member
The instance creation order is just that, it's the order that the instances are created. Their depth is still set by the layer they are in. If they are all in the same layer, then the depth you see them may be determined by the instance creation order, but that is not to be relied on, and specially says in the manual that may be changed, and don't code based on that.
You can move instances between layers, as long as they are instance layers. To select multiple instances you can hold shift and draw a rectangle around them, you can also hold shift + ctrl and draw a rectangle to add to an existing selection. Then just cut/copy/paste to move them to another layer.
 
T

thegabba

Guest
Ok, thank you so much Slyddar for your detailed explanation!
 
This is a very easy problem to solve.
I order my depth in object top to bottom, and left to right.
I do this by having my layers, including tile and instance layers, separated by large numbers.
Instead of having layers have a dpeth of 0, 100, 200,...
I give them 0, 1000000, 2000000, 3000000,... (millions)

When the object is static (it will never ever move), I place this in the create event of that object:

GML:
depthspecific    = 0;
depth            = layer_get_depth("Instance_Player") - (((y-depthspecific)*room_width) + x);
When the object is dynamic in movement, i place that both in the create event, AND in the step event to update its depth.

The "Instance Player" part is just the NAME of the instance layer the player object is on.
(around which instance layer do we want to do depth sorting?)

The variable depthspecific is a tiny adjustment, which depends on the sprite of the object.
Certain sprites in your game might have different center points, given in tthe image editor.
By tweaking that number a bit you can still cleanly sort depth even with objects who have a sprite with
different sprite origin coordinates.

The way this works, is it creates a single number for an object, a number that stores all
the information of its y depth AND x depth.

for example, 2 objects are sitting at these coordinates in a room 1000 pixels wide, at the player instance layer (which has a depth of 5000000 (5 million)):
object 1: x = 100, y = 104 (higher in room, same x, must appear behind object 2)
object 2: x = 100, y = 105 (lower in room, same x, must appear in front of object 2)

for object 1, the depth becomes:
5 000 000 - (((104-0)*1000)+100) = 5 000 000 - 104100 = 4895900
for object 2, the depth becomes:
5 000 000 - (((105-0)*1000)+100) = 5 000 000 - 105100 = 4894900

Notice something? Object 2 gets a lower depth value than object 1, meaning it will appear in front of object 1.
The same applies to x sorting in this way.

The reason this works is because that single number stores integer data for y, and integer data for x,
in the same number,
but separated.

It's using math to create an integer to store data.

I do this all the time, especially with powers of 10. It's very quick and doesn't really produce any performance hits.
 
Just want to raise the warning from the manual regarding depth.
Very strange... Everything is drawn for me...

Edit: layer depths can go all the way up into the millions, even above 10 million,
so it would be weird if GMS didn't allow anything to be drawn on such layers
(plus everything draws for me just fine)
 
Top