Dynamic Z ordering

Get help using Construct 3!

» Wed Nov 22, 2017 2:45 pm

Ashley wrote:FWIW, using "move to front" or "move to back" in a loop is very inefficient. This is why we made the System 'Sort Z order' action - it's much more efficient for ordering lots of instances.

Hi @Ashley

Ive just seen this.

I supose it is to do with z-sorting every run of a loop compared to z-sorting all at the end

So would it be correct to assume that this recommendation is also applicable to something like

For Each "ZObject" > in order of instance variable Z (ascending) > move to XY
ZObject move to bottom of layer

(as this will process in order of individual instance Z variable,
thus the objects will appear in the correct Z order naturally)

so would actually be better to do like

For Each "ZObject" move to XY
then Sort all ZObjects together according to ivarZ

So now they are moved first in any order then Z sorted all at once after the loop

?

cheers
Last edited by NetOne on Wed Nov 22, 2017 8:11 pm, edited 3 times in total.
NOTE: There is a high a probability that I am wrong.
B
53
S
27
G
7
Posts: 490
Reputation: 9,051

» Wed Nov 22, 2017 3:14 pm

Changing Z order in a loop is inefficient because it changes the Z order repeatedly. The 'sort Z order' action is efficient because it does the sorting, then changes the Z order once at the end. So you should basically only use the 'sort Z order' actions for large numbers of instances. However if you just update the instance variable it uses in a loop, that's fine, because updating an instance variable doesn't itself change Z order.
Scirra Founder
B
415
S
248
G
92
Posts: 25,295
Reputation: 200,994

» Wed Nov 22, 2017 3:21 pm

Ashley wrote:Changing Z order in a loop is inefficient because it changes the Z order repeatedly. The 'sort Z order' action is efficient because it does the sorting, then changes the Z order once at the end. So you should basically only use the 'sort Z order' actions for large numbers of instances. However if you just update the instance variable it uses in a loop, that's fine, because updating an instance variable doesn't itself change Z order.

Thanks, sorry yes I missed the "Move to Top/Bottom in my example above. (will edit)

OK then, I am processing all the enemies and environmental objects in my game (pretty much everything you see on screen) in this inefficient way, every tick!

(And here is me still being impressed with the performance !)

Cheers
NOTE: There is a high a probability that I am wrong.
B
53
S
27
G
7
Posts: 490
Reputation: 9,051

» Wed Nov 22, 2017 8:11 pm

Nice!

I was starting to see dips to 54 fps in current build when getting above 40 enemies on screen

this simple change has taken me back to 60fps

thanks !
NOTE: There is a high a probability that I am wrong.
B
53
S
27
G
7
Posts: 490
Reputation: 9,051

Previous