• 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!

GML How to change layer of all instances of a specific object?

A

Airidas

Guest
I'm a beginner in Game Maker Studio 2 and I am trying to add a special feature to my game, but I just can't make it work fully, due to that the instances created by "room_instance_add" appear above every other instance in my room and I can't fix it completely with function "layer_add_instance", since it requires instance_id and cannot work with objects.

The idea (so you can get a little understanding of what I'm doing).

In my game, you kill enemies and after they die, they turn into dead bodies (separate objects).The levels (rooms) of the game can be played again. I want to do, so the dead bodies (created in the previous play) will turn into skeletons the next time you play the very same level (room) and remain there all the time.

What I have done
I have already figured out that I can easily replace dead bodies (enemy_DCU_D) with skeletons (enemy_DCU_Skeleton) and make them remain in that room through out the game using these lines of code:

Code:
while (instance_exists(enemy_DCU_D))
    {
        var furthest_enemy = instance_furthest(1,1,enemy_DCU_D);
        room_instance_add(Mountains_1,furthest_enemy.x,furthest_enemy.y,enemy_DCU_Skeleton);
        instance_destroy(furthest_enemy);
        
        
    }
This code works, but there is a one flaw. The created skeletons are above every other instance in the room (depth).

What I have tried

I have experimented and tried with various functions, and the below one made me go closest to my goal, however it is still lacking, since it is very slow and cannot change layer of instances at the same x position.

Code:
if (instance_exists(enemy_DCU_Skeleton))
{
    layer_add_instance(layer_get_id("Skeletons"),instance_nearest(1 + i,1,enemy_DCU_Skeleton));
    i = i + 5;
}
If I add more to the "i", it is more faster, but even less skeletons get moved.

I wish I knew how to tell Game Maker to move all instances of object enemy_DCU_Skeleton to layer "Skeletons".

I have experimented with instance_create_layer , but the instances I create with it do not remain when I replay the room.

Moreover, I have experimented with "Persistent" function in Object properties, but then the bodies remain in the further rooms, where they shouldn't (according to the idea of my game).

How to change layer of all instances of a specific object?
Or what is the better way to make my idea work?

I would greatly appreciate any solutions or tips!

P.S if you think my explanation is lacking, please, ask for more information regarding to where it is lacking.
 

Slyddar

Member
You should probably draw them to a surface when they die. If you keep using objects as skeletons, even if they are dead, they are taking up resources in GMS. You could save the surface to a sprite if needed and use it for the level each time you go back to it. I suggest you look into it as an option, as it's really good once you get it going.
 
E

Ephemeral

Guest
I wish I knew how to tell Game Maker to move all instances of object enemy_DCU_Skeleton to layer "Skeletons"
Code:
var lyr_id = layer_get_id("Skeletons");
with (enemy_DCU_Skeleton)
{
    layer = lyr_id;
}
 
A

Airidas

Guest
You should probably draw them to a surface when they die. If you keep using objects as skeletons, even if they are dead, they are taking up resources in GMS. You could save the surface to a sprite if needed and use it for the level each time you go back to it. I suggest you look into it as an option, as it's really good once you get it going.
That is a very good point! I understand what do you mean, yet I don't know how to make it happen. I will try to find a way how to do it!

As I understand right now, I have to create assets instead of objects on a special layer. Then, when the level finishes, I would just turn the whole group of created assets into a one sprite (asset), reducing the many amount of assets to one asset that I would place in the background in the next play. Similar to merging layers in image editing, but I'll have to figure out how to "merge" them in GMS 2.

Thank you very much!
 
A

Airidas

Guest
Code:
var lyr_id = layer_get_id("Skeletons");
with (enemy_DCU_Skeleton)
{
    layer = lyr_id;
}
OMG! Yes! Thank you so much! It works perfectly now! Didn't know there is a built in layer variable.

I'm still looking forward to making this feature optimized, as @TheSly said.
 
Top