# How do I automate "Z Order = Y Position?"

Get help using Construct 2

### » Fri Nov 04, 2016 6:02 am

I feel like I'm overlooking a basic feature of Construct 2, but I can't seem to find the info anywhere on this.

Is there a way to set the Z Order of sprites on a layer relative to their Y position? I'm trying to have an isometric game where humanoid sprites can walk in front of and behind each other
B
11
S
3
Posts: 10
Reputation: 695

### » Fri Nov 04, 2016 8:04 am

Yesterday I pulled this off, by using some information from the forums here.
I am doing this from work, so I might make a mistake, and someone please correct me if I am wrong.

I first gave the sprite an instance variable. (this is going to be the y-value of the sprite)
I made an event with condition: order by (that instance variable)
And the action was: sent to top?

I also just found this -->
Ashley wrote:'For each ordered' is a good way - it's not CPU intensive if it's not nested with any other loops.

Sort by Y co-ordinate:

+ For each Sprite order by Sprite.Y ascending
-> Sprite: send to front

Sort by any old number:

+ For each Sprite order by Sprite('some_variable_with_z_number')
-> Sprite: send to front

Then you can set any old number to the 'some_variable_with_z_number' private variable, and it'll sort the Z order from that.
One of my recent games: Plants
B
32
S
7
G
9
Posts: 732
Reputation: 6,775

### » Fri Nov 04, 2016 8:35 am

That is the 'old' method.

Now days you have a system action: Sort Z order
Sort the Z order of instances of a given object according to an instance variable. This effectively removes all instances from their Z order, sorts them, then inserts the sorted sequence back in to the holes left behind. This means if the instances are spread across a range of layers, they will be sorted in the same Z order locations, maintaining the same order relative to other instances on those layers. Note this action is much faster than using an ordered For each with an action like Send to front/back, so this action should be the preferred way to sort the Z order of large numbers of instances.

Basically you store the y in an instance variable (in a loop) and sort with that variable.

But. I found (for automation) the @rexrainbow plugin much faster:
http://c2rexplugins.weebly.com/rex_zsorter.html
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

### » Fri Nov 04, 2016 9:23 am

Thanks for the quick responses. I hope this can help me efficiently make something like the classic RPG towns of old.
B
11
S
3
Posts: 10
Reputation: 695

### » Fri Nov 04, 2016 9:53 am

@99Instances2Go

I did not compare the performance of my z-sort plugin with "system action: sort Z order", sorry I could not promise which one is better.
Goodbye
B
114
S
31
G
295
Posts: 4,549
Reputation: 164,608

### » Fri Nov 11, 2016 3:01 pm

Hmm, if "Sort Z Order" only works for instances of one particular sprite, then I guess instead of using a Family of NPCs, monsters, and town objects, I'll have to turn all characters, buildings, and other such things into a single sprite object with different animations, or frames of animation to identify them.

Is this really the easiest solution?
B
11
S
3
Posts: 10
Reputation: 695

### » Fri Nov 11, 2016 3:09 pm

Families have variables.
B
178
S
50
G
206
Posts: 8,692
Reputation: 127,727

### » Sat Nov 12, 2016 10:52 am

That's a relief. It'd seem like a pretty major oversight if we couldn't make a game from an isometric perspective where sprites could be drawn going in front of and behind each other dynamically.
B
11
S
3
Posts: 10
Reputation: 695

### » Thu Apr 27, 2017 3:09 am

ShinjiDammit wrote:I feel like I'm overlooking a basic feature of Construct 2, but I can't seem to find the info anywhere on this.

Is there a way to set the Z Order of sprites on a layer relative to their Y position? I'm trying to have an isometric game where humanoid sprites can walk in front of and behind each other

This is my solution using a loop method already in Construct 2:

Code: Select all
`+ For each Family order by Family.Y ascending-> Family: send to front`

Make a family "foo" and add sprites into that family. Sprites in those family will have to be in the same layer in order to pass in front and behind one another.

I'm sure there're other methods but this is the easiest one. Maybe use this one as a prototype and see if it works for what you have. Then as you move on and your sprite movement get more complex, you can play around with some other looping method.

If your sprites have simulated depth, you'll just have to offset the origin point for that sprite. For example, if you have a bridge, and since there is no Z in 2D, technically the bridge section is just placed Y+100 from its origin.
B
4
S
1
Posts: 6
Reputation: 312