FoxyOfJungle
Kazan Games
Hello!
I had to create this topic because I spent hours trying to work around the following problem: Game Maker Studio 2 draws objects with depth in the reverse order of how they were created... Basically I have the same problem as that person on this topic (in which I didn't find the right answer).
1 - I'm creating layers manually, I just want to insert instances in those layers, in which the last created ones are on top of the others.
See the following situation:
2 - This is what a have:
(Note that every new instance is created at the lowest possible level)
3 - This is what I want: For this to work, I need to remove the draw event from the object and draw it manually in the draw event of the obj_controller, however, this causes the depth of the layers to be disregarded.
4 - Code: How am I doing all this?
>> obj_controller: <<
CREATE EVENT:
STEP EVENT:
KEY PRESS: D (destroy current layer with every instance inside):
KEY PRESS: A (add a new blank layer)
DRAW GUI:
>> obj_block: <<
CREATE EVENT:
DRAW EVENT:
RIGHT PRESSED:
OBJECTIVE:
Create instances, and their depths are always closer to the camera than the ones below (the top most), respecting the depth of the leyer in which the instance is.
Could someone help me with any solution? I will be very grateful! Thank you.
I had to create this topic because I spent hours trying to work around the following problem: Game Maker Studio 2 draws objects with depth in the reverse order of how they were created... Basically I have the same problem as that person on this topic (in which I didn't find the right answer).
1 - I'm creating layers manually, I just want to insert instances in those layers, in which the last created ones are on top of the others.
See the following situation:
2 - This is what a have:
(Note that every new instance is created at the lowest possible level)
3 - This is what I want: For this to work, I need to remove the draw event from the object and draw it manually in the draw event of the obj_controller, however, this causes the depth of the layers to be disregarded.
4 - Code: How am I doing all this?
>> obj_controller: <<
CREATE EVENT:
GML:
global.Layer_Index = 0;
global.Layer_Array[0] = layer_create(0, "Default Layer");
STEP EVENT:
GML:
// create the instance in the current layer being used
if mouse_check_button_pressed(mb_left)
{
instance_create_layer(mouse_x, mouse_y, global.Layer_Array[global.Layer_Index], obj_block);
}
// change what layer is being used
if keyboard_check_pressed(vk_left) global.Layer_Index -= 1;
if keyboard_check_pressed(vk_right) global.Layer_Index += 1;
// clamp current layer index
global.Layer_Index = clamp(global.Layer_Index, 0, array_length(global.Layer_Array)-1);
// reorder layers depth (I don't know if this is necessary and how I would change it...)
if layer_exists(global.Layer_Array[global.Layer_Index])
{
for (var i = 0; i < array_length(global.Layer_Array); i+=1)
{
layer_depth(global.Layer_Array[i], -i);
}
}
KEY PRESS: D (destroy current layer with every instance inside):
GML:
layer_destroy(global.Layer_Array[global.Layer_Index]);
global.Layer_Index = array_length(global.Layer_Array);
KEY PRESS: A (add a new blank layer)
GML:
global.Layer_Array[array_length(global.Layer_Array)] = layer_create(0,"LayerName_"+string(irandom(999)));
global.Layer_Index = array_length(global.Layer_Array)-1;
DRAW GUI:
GML:
// draw layer list
for (var i = 0; i < array_length(global.Layer_Array); i+=1)
{
var _id = global.Layer_Array[i]
if layer_exists(_id)
{
var _name = layer_get_name(_id);
}
else
{
var _name = "--------";
}
draw_text(600, 10, "LAYER LIST:\n=============")
draw_text(600, 60 + i*20, _name);
}
// DEBUG
draw_set_color(c_lime);
draw_text(10, 10, fps_real);
draw_text(10, 30, fps);
draw_set_color(c_white);
var _id = global.Layer_Array[global.Layer_Index];
if layer_exists(_id) {var _name = layer_get_name(_id);} else {var _name = "---";}
draw_text(10,100,"Layers (array_lenght): "+string(array_length(global.Layer_Array))); //layers quantity
draw_text(10,120,"Layer Current (layer_array_index): "+string(global.Layer_Index) +string(" | ") +string(_name) ); //current layer index
>> obj_block: <<
CREATE EVENT:
GML:
// get the name and depth just for debug
my_layer = layer_get_name(layer)+string(" | ")+string(layer_get_depth(layer));
DRAW EVENT:
GML:
//draw object normally
draw_self();
draw_set_halign(fa_center);
draw_text(x, y-48, my_layer);
draw_set_halign(fa_left);
RIGHT PRESSED:
GML:
// destroy if the object is on the current layer
if (layer == global.Layer_Array[global.Layer_Index])
{
instance_destroy();
}
OBJECTIVE:
Create instances, and their depths are always closer to the camera than the ones below (the top most), respecting the depth of the leyer in which the instance is.
Could someone help me with any solution? I will be very grateful! Thank you.
Last edited: