Automatically create/order/group batches of tile layers

Hey there Tiled peeps!

I’m working on a project where each map consists of a gameplay layer and 15-20 art layers. So far for the few maps with art, we have created and organized all these art layers manually. However, we have 100+ maps that will eventually need art, and certain biomes will need additional unique art layers. We are looking for a way to automatically create and order layers, and if possible, group them and modify their properties.

One solution is to create a base map that has the layers configured correctly, then copy that file when creating new maps. This would work, but feels extremely clunky and error prone. Especially when working in a team with different artists/designers that all work slightly differently. This is an exceedingly manual solution to a problem that seems highly automatable.

Another solution is to do some automapper or tile stamp shenanigans but this seems not ideal. We already use a couple large rulemaps and complex tile stamps to handle basic art like walls, floors, and layered decals. Automapping and stamping does create the required layers if they did not exist already. However, the layers are created in a seemingly uncontrollable order, are unsorted, do not carry over properties or attributes like offset, and are only created if art is actually being placed in them. I tried creating an extra rulemap that adds and subsequently deletes one tile from each layer, which does create the necessary layers, but then I still have to go through afterwards and manually order and modify the layers.

The obvious solution seems to be scripting, but I have no idea where to start. I’m honestly surprised I haven’t been able to find a premade script that does something similar to this. Even hard coding which layers are created would be a feasible solution. I’ve attempted to create a script myself but as a novice coder, going back and forth through the API documentation is taking me longer than I care to admit. I would hate to spend a week teaching myself tiled scripting just to find out there was already a simple way to do this.

What would the best solution to this problem of batch creating layers and modifying layer properties be? If it really is scripting, can anyone provide a basic template or a link to a tutorial outside of the dense scripting API documentation? Thank you! <3

The usual recommended solution is to start with a template map that has all the layers and properties you want, make it a read-only file so you don’t accidentally overwrite it, and then start new maps by opening that template map and Save As to create your new map.

If that’s not suitable, scripting is indeed the way to go. No public script for this exists because this is much easier to set up for a specific scenario than as a generic solution. So, most people’s implementations of such scripts just aren’t worth sharing because they’re designed for their specific set(s) of layers. One of my scripts creates layers en masse, but it’s designed specifically to aid in creating large numbers of Automapping output/input layers, for example.
To get started, the simplest option is probably to create an Action that creates a bunch of new layers (e.g. let newLayer = new TileLayer("New Layer Name") and adds them to the active map (e.g. let map = tiled.activeAsset; /*...*/ map.addLayer(newLayer), see also TileMap.insertLayerAt).
If you want to give yourself a GUI for selecting which set(s)/layer(s) to create, take a look at the Dialog API.
For adding/modifying properties via scripting, see the methods on TiledObject.

Layer ordering can be done with removeLayer and insertLayerAt, I think. Not something I’ve ever had to do with scripting, I order things manually xP Groups can help.

Thank you so much for the speedy reply!

We already have most maps designed and awaiting art, so unfortunately a template map isn’t a super good solution for us. We would have to create new map files and move everything over which would be version control nightmare. If we had some foresight we could have used templates from the start but alas…

It seems like scripting is the play then. I was actually looking at that exact automap helper script of yours and getting so deeply lost in the sauce I thought it might be better to ask :sweat_smile: Looks like I’ve got a few days of scripting ahead of me. Thanks for the tips on getting started! Once I put something together I’ll post a snippet of it to help others who come across this post in the future.

I don’t understand your situation so I don’t know if it’s relevant, but among my scripts is also this one that copy+pastes entire layers, could this be of use? It copies and pastes the names, contents (which may be empty) and custom properties of layers, and can be used to get additional layers into existing maps.

If you need to copy properties onto existing layers, then I think you’ll have to write something of your own.

And just because I’d be remiss not to mention it: if you haven’t already, look into classes, which can be used to define default properties for your layers, and make them easier to set when different values are needed.

Yeah I was thinking about using that because what we need is not super complex. And copying some premade layers is basically exactly what I’m trying to do. I’m just worried since we have multiple people helping with art that introducing extra steps to the process is going to create more chances for things to go wrong.

To be honest since our project does not have the largest scale a more “bandaid” solution like copy and pasting layers might work just fine for us. Developing and testing a custom script is kinda overkill for this problem. I just love learning new things, and if I got decently knowledgeable we could automate a lot of other things about mapping too.

We only need a couple layers to have any properties, and they each only have one or two so I don’t think we need to use classes, but that’s a super cool tool that we might use on a future project! Thanks for mentioning it!