• 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!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Discussion Creating depth within a single layer

H

Hurg

Guest
Hi all, hopefully I'm asking this in the correct place. My question relates to using depth to draw sprites under the new layer system of GM2 (only been tinkering with this version for a couple of weeks).

Basically, I want to create a player character that has upgrade-able components that follow the x/y position of the player's torso per step. So for example, a head that would look in the direction of the mouse/change sprite depending on whether player is wearing a helmet or whatever.

Now I know that we're supposed to use the layer system in GM2 for handling depth, but I don't really want to separate the player into 5 or 6 layers as that seems cumbersome, especially if I need to call the layer that the player is on.

I had a look at the manual regarding depth in GM2 and came across this: "it may be that you want to change the depth of an instance using this value, in which case a "temporary layer" will be created specifically for the instance at the given depth." This doesn't really sound like it solves the issue anyway as it would be creating separate layers for each object at the set depth.

Is there a way to keep this all on a single layer while determining the draw order of sprites?

Cheers
 

Slyddar

Member
The order of draw on a layer cannot be relied on. Here's a few methods you could try.

1. You could store the draw events you need in an array, ds_list, or even ds_grid, and then have a single controller object, or even the player object, draw them as sprites in the order you require.

2. Alternatively you could control the depth of each object based on the depth of the object under it. Say you have a player, head and helmet. Give o_player a depth, then the head has "depth = o_player.depth-1;", and helmet has "depth = o_head.depth - 1;" You might get away with setting these in the create event of each object, but depending on what you are doing, probably best to set in the step event if updates are required.
 
H

Hurg

Guest
2. Alternatively you could control the depth of each object based on the depth of the object under it. Say you have a player, head and helmet. Give o_player a depth, then the head has "depth = o_player.depth-1;", and helmet has "depth = o_head.depth - 1;" You might get away with setting these in the create event of each object, but depending on what you are doing, probably best to set in the step event if updates are required.
Thanks, but If I'm reading the manual correctly, this method would create separate layers for each object anyway, just that they would be temporary which would be even less useful than assigning the objects manually to separate layers. What I want is a way to set the draw order of sprites within a specific layer.
 
H

Hurg

Guest
Playing around with this then and found that layers can have sublayers (was looking for a grouping option). Created an empty playerGroup object and added the separate parts as sublayers. Draw order looks good, can change things just by dragging the sub groups around.

Unfortunately, anything applied to the parent group isn't passed down to the subgroups. Just tried an experiment then when I deleted the parent layer in game hoping that the sub layers would also be destroyed. However, this only destroyed the top layer. Same thing happens when applying a shader to the parent layer, it's not inherited by the sub layers which is what I was hoping for.

***edit***

As I suspected, changing depth in an object's create code means that those layer functions will no longer apply to that object. Not really surprising as that's what the manual said would happen, but I just tried adding three objects to a single layer then and changed their depth in the create code, no layer effects were applied to those objects.
 
Last edited by a moderator:

Slyddar

Member
Sub-layer inherit the properties of the layer above, and not code/properties from the objects on the layers.

Option 1 would allow you to control the order, and the components wouldn't even need to be objects, just drawn sprites. Even the weapon could be a sprite, that creates a hitbox object when needed. That way everything is drawn from the player object and what happens to the player happens to the objects i.e. moved/destroyed
 
H

Hurg

Guest
So... I think I found a simple solution to this. Found out you can change the draw order of sprites in a layer by dragging the objects in the room list and they will draw in the order they're placed in, with the last item being at top. I attached an image that demonstrates this.

Is there any reason not to use this?
 

Attachments

Top