GMS 2 How to walk behind and in front of objects with MULTIPLAYER

Alexir

Member
I have an isometric/top-down game with objects that I want the player to walk in front of and behind. When they walk in front of it, the player is visible and obscures the object behind it partially. When they walk behind it, then the object in front of it obscures the player from the camera view. This is all pretty standard stuff and I have got a good bit of code that makes this work successfully for a fence object;

STEP:

if instance_exists(obj_Player) && obj_Player.y >= self.y
{
depth = 1
}

if instance_exists(obj_Player) && obj_Player.y <= self.y
{
depth = -1
}

(Take in mind the fence's origin must be at the bottom for this to work)

Now this works perfectly fine, but when I have two players, this breaks completely. Either both players are behind the fence, or both players are in front of the fence, despite their position. This is obviously not good and I need a way to have one player be on one side of the fence, and the other player on the other side. Any help would be appreciated.
 

Slyddar

Member
Why not place instances in the room, and have each instance work out their own depth based on their own y, not based on other instances y?
 

Alexir

Member
Why not place instances in the room, and have each instance work out their own depth based on their own y, not based on other instances y?
I was thinking that, but it might conflict with other objects. But perhaps if I simply just copy and paste that code but with every object onto the player and have it as simple as 1 and -1 then it could work. I'll try it out and report back asap.
 

Alexir

Member
I was thinking that, but it might conflict with other objects. But perhaps if I simply just copy and paste that code but with every object onto the player and have it as simple as 1 and -1 then it could work. I'll try it out and report back asap.
As expected, it clashes with other objects. When I am below or above both objects it works perfectly fine, but if I am in between two objects, then it breaks and shows the player in front of both objects. The code apparently doesn't allow the player to be in front of and behind an object at the same time without bugs.
 

Alexir

Member
Only having 2 depth choices of 1 or -1 seems limiting. Have you tried setting depth = -y?
I tried writing depth = -y in every object in question's step event, and it worked perfectly for going in between the objects, but not for going above or below one individually. It pretty much just had the opposite effect as before, instead of being to be able to go above and below both but not go below one and above one, I could only go above one and below one, but not below or above both. Maybe I could combine these two to reach success?
 

Alexir

Member
I tried writing depth = -y in every object in question's step event, and it worked perfectly for going in between the objects, but not for going above or below one individually. It pretty much just had the opposite effect as before, instead of being to be able to go above and below both but not go below one and above one, I could only go above one and below one, but not below or above both. Maybe I could combine these two to reach success?
EDIT: When I put depth = -y into the player's step event it seems to work perfectly, even with multiple players! Thanks for the help, ill report back any difficulties in the future if they come by.
 
Last edited:
Top