Scripting API some layer groups.layers and object layers.objects arrays are reversed

I have a custom format script and I have some layer groups with more layers groups that also contain object layers.

When I am getting the arrays from those groups (group.layers) or object layers (objectLayer.objects) some of them are in reverse order and some are not.

It’s driving me mad, for most cases the order of array items in .layers or .objects doesn’t matter to my game logic but for some unique cases, I need them to be ordered in the same order I have them in the editor (either the layer ordering as it appears in the Layers panel, or objects in object layers in the order I initially placed them i.e. lower ID’s come first in the objects arrays).

I searched a little bit for the answers but don’t see anyone having this issue so it must be something I’m not doing right when creating layers or placing objects in the editor.

Try using layerAt and objectAt instead. I haven’t looked at objectAt much, but layerAt in my experience has always been consistently ordered with the bottom layers first, both in maps and groups.

I don’t know whether the objects and layers array are in the same order as what you get with layerAt/objectAt. I’d expect them to be, but I’ve only used them when order doesn’t matter so I haven’t looked into it.

1 Like

Thanks for the fast response. I’ve only just started getting deeper into using Tiled’s scripting API.

I was doing it this way for iterating over layers within a layer group which gave me layers in reverse order for some cases.

    layerGroup.layers[i];

Changing to this has given me the same results as the previous example:

    layerGroup.layerAt(i);

It’s just puzzling me right now but I’ve been at it all day so I’ll probably realise I’ve done something silly tomorrow morning!

Edit: By “at it” I mean game dev in general, not stuck with this issue all day :sweat_smile:

Might be! The layer order is consistently bottom-up for me, are you sometimes getting top-down?

I would not be surprised if objects were in a different order, but I’d expect that order to be consistent as well.
(FWIW that order should be related to the display order since that’s the important order, whereas IDs/creation order is not.)

Edit: Something else to consider is that an already-fetched objects won’t be updated when you reorder Objects.

1 Like

Oh, actually! Now that I’ve changed every instance of square brackets array access notation to using layerAt()/objectAt(), the are now consistently bottom-up.

My layers look like this:
Screenshot_2023-10-05_164533

I’m just figuring out how to lay out my levels that will be the same for all levels in my game (beltscrolling beat 'em ups).

1 Like

Oh good! So at least layerAt()/objectAt() are consistent. I guess objects/layers should only be used when order doesn’t matter, then. That would be good to document. *nudgenudge @bjorn*

Ok, that is good to know as I was wondering this.

This is in a custom format script triggered when using the export command. Are you saying that these are cached and only updates to properties get added?

This is in a custom format script triggered when using the export command. Are you saying that these are cached and only updates to properties get added?

No, I’m saying that if you have a script that fetches objects/layers from a layer and saves that value to a variable, and then rearrange the objects/layers in Tiled (via script or manually), your saved values won’t be updated.
For scripts, such as custom export formats, that do everything in one go, this should not be a problem. It only becomes an issue if your script that fetches these things at different times and when the map may change between these times.

Blockquote Oh good! So at least layerAt()/objectAt() are consistent. I guess objects/layers should only be used when order doesn’t matter, then. That would be good to document.

Yes, if it’s consistently bottom-up ordered using the methods then that is ok for me.
In my game the order of the “screenLockXX” group layers and the order of the object layers in the “waves” group layers are important. Others are not.

Blockquote For scripts, such as custom export formats, that do everything in one go, this should not be a problem.

I haven’t dug into run time scripts or anything beyone export scripts yet.

Well thank you for your help. The mystery has been solved!