[SOLVED] Correct way to reorder child entities?

Hey guys,
I currently have a list of entities in a layout group that I want to reorder with script. I thought I’d be able to remove them with removeChild() and re-add them in the new order, but removeChild doesn’t seem to actually stop them from being rendered. The only way I’ve managed to get what I want is to clone all the children, destroy() the originals and then add the clones. This seems like too much work for something so simple. Is there a better way to do this?

Is this for the 2D UI canvas? Rather than destroying them, you could disable the entity instead.

1 Like

You can sort the children of the parent directly using standard Javascript Array.prototype.sort().

I made a small project to give example:
https://playcanvas.com/project/666365/overview/forum-sort-children

2 Likes

Thanks LeXXik! That’s just what I needed

Hey @LeXXik, can you confirm if this works during runtime? I couldnt get it to work, at least.

I have a layoutgroup with 30 children that will be reordered about once a second. Each of these children has a lot of children themselves, so I’d like to avoid cloning if possible.

Edit: Figured it out. A call to layoutgroup.reflow() did the trick. Found it in the typescript definition file, but should probably include it in the docs? :slight_smile: LayoutGroupComponent | PlayCanvas API Reference

1 Like

I’ve added a feature request for you: