In my experience you will face issues with this method if you are using the built in depth variable in GMS 2
Assigning a value to the depth variable will put the object on a managed layer ( not one assigned to the room by yourself ). As layers have integer depth values, the number you are assigning in the formula above will be rounded to the nearest integer value, and you will end up with instances still occupying the same layer, even though you've given them slightly different depth values.
As they are on the same layer, they will not be rendered in a guaranteed order that you want.
You would need to use your own custom depth variable, and then render the instances yourself from back to front in order to achieve what you want.
I have a parent object for all rendered instances, and I use:
End Step Event:
Code:
with ( parent_object )
{
// [ Add all objects to a sorted ds_list based on their custom depth value ]
}
Then in the Draw Event, I draw all the objects in the list in order.
You could use the code you listed above, but also bear in mind if you are constantly shifting the depth value every End Step, you will be shifting all the instances depths by a small amount every frame.
There is a good discussion about depth sorting in GMS 2 here:
https://forum.yoyogames.com/index.p...rity-list-nested-list-grid-binary-list.13425/
All my instances have a variable called z_3D, which is their custom depth.
The code I use in my persistent object for managing this is :
// End Step Event
Code:
with ( o3D ) // All instances with depth have the object o3D as their parent.
{
var _sort_depth = id | z_3D
ds_list_add(o3DCamera.draw_list, _sort_depth)
}
ds_list_sort(draw_list, false);
Then in the Draw Event:
Code:
var _list_size = ds_list_size(draw_list)
for ( var i = 0; i < _list_size; ++i )
{
var oID = draw_list[| i] & $ffffffff;
with ( oID )
{
event_user(0) // Objects draw code is contained in user event 0
}
}