# How do I z-order of same object type [new problem]

0 Favourites
• 9 posts
• Hi,

i ran into a problem im not sure how to solve. For my isometric tower defense game i have managed the z-order of people and buildings with these events:

for placing new towers i need something similar but

doesnt work at all, i guess because it cant figure out which tower is which?

is there a way that the logic differs between two instances of the same object?

or do i have to make a workaround that relies on other values? (like both x and y position/tile that the towers stand on)

• I'm also curious about how you manage picking in this situation....

Since the towers don't move, you can set their Z order once at their creation or at the start of the level. Because you don't do it too often, you can afford to use an ordered ForEach to loop over them all by z order and send them to the top, one by one. This is, of course, the most horrible sorting algorithm ever, but I figure it's better than no solution at all.

• You really shouldn't have any issues sorting them all a few times a second only if you're moving. Just "for each object" (ordered) by y ascending, send to front whenever you're moving, but limit it to like 30 or so milliseconds so it only runs a few times a second. There seems to be some strange opinion on these forums that this is something crazy cpu-intensive and mostly be people who've never done it. It works fine, it's how tons of things are sorted in my game. Just put everything sortable into a family (they have to be the same object type) and then just run the operation on that family whenever necessary. If your character moves slower, you can probably decrease how often it happens. You don't need to run it every tick.

• I'm also curious about how you manage picking in this situation....

Since the towers don't move, you can set their Z order once at their creation or at the start of the level. Because you don't do it too often, you can afford to use an ordered ForEach to loop over them all by z order and send them to the top, one by one. This is, of course, the most horrible sorting algorithm ever, but I figure it's better than no solution at all.

Thank you both!

it works really fine that way

and youre wright, since its only necessary when a new tower is placed it shouldnt make any performance impact.

• now i got another problem i thought ive already solved :/

sometimes my code from above for setting the peoples z-order doesnt work

i figured is has something to do in which order the towers are sorted when a walker overlaps more then one tower

i made another demo, the towers are drag&drop so you can play around with different situations

if you change the place of the problematic tower it will make correct z order

i guess it has to do with the objects uid?

tryed to make something with for each(ordered) but it doesnt really work

http://fldr.de/tower7

• im now trying for quite some hours everything i thought might work, but nothing.

Several different event combinations with "for each sprite","for each tower", "for each (sorted) tower by y" descending, ascending, call a function to sort everything...

because i thought it has something to do with the UID i even implemented that every tower gets rebuild sorted by its y value, descending, ascending when a key is pressed, doesnt work.

descending, ascending does change the buggy behaviour, that means its not the same towers that are affected but i didnt find a solution where every tower behaved the way i wanted.

the strange thing about it is that only some towers are affected and i cant find the missing link.

the code works perfect when the walkersprite is only overlapping one tower but with two or more it SOMETIMES doesnt work..

i did build a tower on every field thats near the street, turns out you can fix the situation by rearanging the towers, but i dont see the pattern here, all i know its not solved by rebuilding all towers by ascending or descending y values.

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• ok, heres a solution i found on the forum that works, it just works, im happy

towers and people are both in this family

now i can delete over 50 events ^^

have to figure out how i can make this more performant (already tryed sprite"on collision"with tower instead of "every..", wasnt good) but for the moment, im just happy

Thanks for everybody who helped me !

• At some point somebody should make a plugin that implements bubble sort, if RexRainbow's plugin doesn't already....

• Here's my solution which works pretty fine. I think it performs quite well, but probably can be optimized a bit.

Objects can be replaced with families i guess.

I'm not sure if i got it right exactly the way i wanted, but what I was aiming for is to call the sorting function only when something moves to a new layer (Above or Behind), which it does depending on the Y position.

And here's a link to the capx file.

https://dl.dropboxusercontent.com/u/20560446/z_order_isometric.capx