GameMaker What is the good way to handle tile & object depth?

R

RicardoVL

Guest
Hello all,

I have some trouble in finding out what the correct way is of depth in GMS2
The fences are tiles in 2 different layers, de upper half is above the NPC's layer
and the bottom tiles beneath them.

I want the characters to be able to walk to the fence and stand really close to it, just like in real life.
I know there are some tricks to do with dept = -y but i only know this trick with objects
I could make the fences into objects, but i think that's inefficient.

And how to properly use depth between npc's, in the step event i do: depth = -y
But when i do that the depth is overall and i can for example walk over trees. (i need to change that depth to??)

 
Last edited by a moderator:

NicoDT

Member
Hey! I'm not using GM2 yet, so the solution I have for GM1 may not work.
What I do is having all the tiles I want to apply depth in a same depth level (in this case 900000), and in the room start event I change all those tiles to a -y depth.

Code:
var tiles

var i
tiles = tile_get_ids_at_depth(900000);

for ( i =0 ; i< array_length_1d(tiles); i+=1)
{
 if (tile_exists(tiles[i]))
 { tile_set_depth( tiles[i], -tile_get_y(tiles[i])-tile_get_height(tiles[i])+32)}
}
I don't know if those functions still work in GM2, or there is a work around, but since I will use GM2 for my next project I'd too like to know the solution for your problem.
 

mar_cuz

Member
Hey! I'm not using GM2 yet, so the solution I have for GM1 may not work.
What I do is having all the tiles I want to apply depth in a same depth level (in this case 900000), and in the room start event I change all those tiles to a -y depth.

Code:
var tiles

var i
tiles = tile_get_ids_at_depth(900000);

for ( i =0 ; i< array_length_1d(tiles); i+=1)
{
 if (tile_exists(tiles[i]))
 { tile_set_depth( tiles[i], -tile_get_y(tiles[i])-tile_get_height(tiles[i])+32)}
}
I don't know if those functions still work in GM2, or there is a work around, but since I will use GM2 for my next project I'd too like to know the solution for your problem.
Hi Nicodt I'm interested in your way of doing this. What is the array_length_1d in your for loop? How would I set that and would this work for isometric tiles?

Thanks
 

NicoDT

Member
Hi Nicodt I'm interested in your way of doing this. What is the array_length_1d in your for loop? How would I set that and would this work for isometric tiles?

Thanks
Hi!
What I do is store only the tiles at depth of 900000 in the variable "tiles" (these are the tiles I want to have a different depth depending on the Y coordinate.). What I do with array_length_1d is loop through all the tiles that were store in that variable, and then set a new depth.
I have never done isometric tiles, so I wouldn't know for sure. Do your tiles have height? (like a Final Fantasy tactics map), if they don't have height then I guess you could use it the same way as I do. If they do have height, I'm not sure (maybe you can do it several times for different heights/depths? I have really no idea, sorry :confused:)
 

mar_cuz

Member
Hi!
What I do is store only the tiles at depth of 900000 in the variable "tiles" (these are the tiles I want to have a different depth depending on the Y coordinate.). What I do with array_length_1d is loop through all the tiles that were store in that variable, and then set a new depth.
I have never done isometric tiles, so I wouldn't know for sure. Do your tiles have height? (like a Final Fantasy tactics map), if they don't have height then I guess you could use it the same way as I do. If they do have height, I'm not sure (maybe you can do it several times for different heights/depths? I have really no idea, sorry :confused:)
I don't have height. Just flat ground. The attached image is my game atm. The walls are objects but I want to use the walls as tiles.
 

Attachments

NicoDT

Member
I don't have height. Just flat ground. The attached image is my game atm. The walls are objects but I want to use the walls as tiles.
Yeah, doing it the same way as I do should work just fine then.
Just remember I do it for 1.4 , I have no idea if the same functions are available for GM2 or not.
Let me know if you need any help when you do it.
 

mar_cuz

Member
Yeah, doing it the same way as I do should work just fine then.
Just remember I do it for 1.4 , I have no idea if the same functions are available for GM2 or not.
Let me know if you need any help when you do it.
Thanks for that. Yes I'm on 1.4 as well. Well take your offer of help if I get stuck.

Just quickly though, the array length 1d does not need to be pre set yeah? The for loop should still work without redefining the array elsewhere first?
 

NicoDT

Member
Thanks for that. Yes I'm on 1.4 as well. Well take your offer of help if I get stuck.

Just quickly though, the array length 1d does not need to be pre set yeah? The for loop should still work without redefining the array elsewhere first?
That's right, that's the only code needed.
 
R

RicardoVL

Guest

NicoDT

Member
So you are saying that this solution is faster then tile_set_depth ? because you don't use loops?
And do you use this method in gms2?
This way doesn´t loop through all the tiles, only those that need the depth changed (so for example floor tiles won´t be considered).
I never had a problem with it, but I have no more than 40 tiles that need its depth changed to value -Y , I´d recommend you to try placing many tiles to see if you have any problem.
 
R

RicardoVL

Guest
tile_get_ids_at_depth(900000)
:'( this is an unknown function, its obslete in 2.0

This way doesn´t loop through all the tiles, only those that need the depth changed (so for example floor tiles won´t be considered).
I never had a problem with it, but I have no more than 40 tiles that need its depth changed to value -Y , I´d recommend you to try placing many tiles to see if you have any problem.
:'( this solution is only whe code created the tiles, but i do this in my room editor, ad perhaps it's obslete 2..
 
I have addressed this in my game with a collision tile layer that stops the player at just the right point so that they don't overlap the "walk-behind" tile. So I would have the top half of the fence in the "walk-behind", the bottom half of the fence on the "ground" layer (which is below the player/instance layer in depth), and then a collision layer with my collision tiles running along the top edge of the bottom half of the fence. My movement/collision checking then blocks movement based on the player bounding box and the collision tiles top keep the player from overlapping either part of the fence.

The downside is that it does keep the player away from smaller objects that you might want to be closer to.

I could post some screenshots when I get home tonight if the above doesn't make much sense. :)
 
R

RicardoVL

Guest
I have addressed this in my game with a collision tile layer that stops the player at just the right point so that they don't overlap the "walk-behind" tile. So I would have the top half of the fence in the "walk-behind", the bottom half of the fence on the "ground" layer (which is below the player/instance layer in depth), and then a collision layer with my collision tiles running along the top edge of the bottom half of the fence. My movement/collision checking then blocks movement based on the player bounding box and the collision tiles top keep the player from overlapping either part of the fence.

The downside is that it does keep the player away from smaller objects that you might want to be closer to.

I could post some screenshots when I get home tonight if the above doesn't make much sense. :)
Yes and i would like that the player can walk all the way to the fence just like in real life. It's not a majer issue but it would be way nicer, thats why i started this thread.
 
R

RicardoVL

Guest
Nothing good found yet. is it a "good" solution to make objects of things that have an advaced depth.
I also want to make all trees objects so you can chop them.

Aything against that, in speed or something?
 
K

kevins_office

Guest
Here it is 2018 and i have yet to find a solution for depth sorting tiles or sprites in GMS2. It seams like the depth sorting method only works on objects.
Has anyone found a way for sprites drawn on an asset layer, or tiles in a tilemap, to be included in the depth sorting method?
The method where you sort instances and control which has its draw_self() executed first to last without changing the depth variable.
 
L

laekwondo

Guest
I used this tutorial for object depth, seems to work well:

Edit: Here is an update to the Object Depth code, more efficient:
 
Last edited by a moderator:

JackTurbo

Member
You guys tried z-tilting? There's a great write up on it on the game maker tech blog, not tried it myself yet.
 

NightFrost

Member
Isn't z just a function of y?
Not when a shader does some 3D faking in the background. With z-tilting, you still have your 2D room plane as usual, and you place your stuff there normally. Every sprite however has to have their origin at the bottom. A shader then works on the sprites by grabbing their upper corners and tearing them along the z-axis towards the screen, in a manner that puts them all at the same angle (ie, 45 degrees elevated from the room plane). Since this doesn't alter the x/y positions of the upper corners, the sprites are technically getting stretched, but that would only be noticable from some other angle than directly from above, where your camera is anyway as you're having a 2D view.

This picture from the blog illustrates it well, though it talks about the z-fighting problem:



The blog post mentioned is over here and goes into greater detail.
 
Top