GameMaker Defining object depth by creation order

hi
I'm working on a pen&paper helper thingy that creates objects upon pressing a button (like map object;walls etc.).
It's crucial that the last created object is overlapping all previously created objects and I dont know how to get that working.
Sorting the depth by y-value makes no sense, I need to sort it by creation order / time.

thx in advance
 

SoapSud39

Member
You can do something like
GML:
//create
depth -= instance_count;

//OR

depth -= instance_number(object_index);
so for every other instance or instance of the object in the room, assuming all of the starting depths are the same, the depth is one less (one closer to the front).
 
You can do something like
GML:
//create
depth -= instance_count;

//OR

depth -= instance_number(object_index);
so for every other instance or instance of the object in the room, assuming all of the starting depths are the same, the depth is one less (one closer to the front).
i love you
thanks a bunch
 

hippyman

Member
I'm a little confused because this is how GM works by default. It draws from first to last already. Did you try simply not doing anything with depth and just creating the object? It will always be created on top of the previous instances.
 
sry i didnt entirely explain

a button creates several objects to choose from, which, if not clicked, destroy themselfves after a couple of seconds (alarm)
if they are clicked, they remain in the game and can be used as elements for a map (alarm canceled)
now it draws like 20 objects at once, defining their depth at creation
and i needed a way to change that depth, which i now solved by double tapping lmb setting depth -= instance_count
so that if i have like building and a ladder, i can change the depth of the ladder for it to overlap the building, regardless of when either of those objects were createdScreenshot 2020-11-22 000729.png
 
on a different topic:
is there a way to resize multiple sprites at once?
and is there a way to turn (multiple) sprites into an object? so i dont have to create the object and then assign a sprite?
i have like a bazillion map elements to create...

sry if these are noob questions, i'm fairly new to gamemaker
 

SoapSud39

Member
is there a way to resize multiple sprites at once?
If you mean resizing in the editor, you might have to just do them one by one. But you could draw at different x and y scales, albeit you'd have to keep track of everything you've put in your code, and potentially deal with different offsets or other issues that come from resizing.

and is there a way to turn (multiple) sprites into an object? so i dont have to create the object and then assign a sprite?
i have like a bazillion map elements to create...
If you have an object for which multiple sprites apply, you could do something like this:
GML:
//control object code
with instance_create_layer(x, y, "layer", obj_object) {
    item_index = 0;
    image_index = 0; //or sprite_index = spr_sprite;
    //and whatever else
}
    //do this for every map element, or write a for-loop using an array (or some arrays) if you care to learn how to use them
I would recommend using arrays to contain item information, so that with a for loop you could write something like
GML:
for (var i = 0; i < num_items; i++) {
    item_index = i;
    sprite_index = item_sprite[i];
        //alternatively: sprite_index = item_array[i, ITEM.SPRITE]; (constant)
    //etc.
}
Basically, instead of containing the information in a bunch of objects, you could contain the information in some arrays, or a bunch of lines of code, which would be more manageable than making a ton of objects either way.
 

FoxyOfJungle

Kazan Games
I was having the same problem recently:


GML:
// Instances draw scripts
function layer_script_instances()
{
    /// @func layer_script_instances()
    if (event_type == ev_draw)
    {
        if (event_number == 0)
        {
            var u = 0;
            repeat (array_length(global.Layer_Array))
            {
                var _array = layer_get_all_elements(global.Layer_Array[u]);
          
                var i = array_length(_array)-1;
                repeat (array_length(_array))
                {
                    if (layer_get_element_type(_array[i]) == layerelementtype_instance)
                    {
                        var L = layer_instance_get_instance(_array[i])
                        with (L)
                        {
                            // Draw Object
                            event_perform(ev_draw, 0);
                        }
                    }
                    i -= 1;
                }
                u += 1;
            }
        }
    }
}

layer_script_end(global.Layer_Array[global.Layer_Index], layer_script_instances);
 
thank u all for the helpful answers
i got my item spawn thingy working now for the inventory
now i've encountered a different problem of similar nature:

i've got this battle map for my p&p helper
the room is bigger than the screen and by pressing direction buttons i can scroll over the whole map, having the HUD following the screen
on the right side of the screen i've got a parchment-like panel on which all the map objects spawn, also part of the HUD
on creation, the map objects are in the same layer as the panel, overlapping the map and the panel
if i click on a map object, it becomes "selected" and moves to a layer below
to create the effect that objects i already planted on the map get overlapped by the panel (HUD)
now
i have a assigned a key to set the depth of a selected object and move it on top of all other objects [depth -= instance_count;]
but that only works as long as the object is in the top layer
i think i kinda need a way to get an instance count of all objects in one specific layer and then move the object to the top in that layer

@FoxyOfJungle i think you did that in your example, but i dont understand how you did it
 
Last edited:
Top