Sounds like you need an Action that you’d trigger from a menu or shortcut.
You can create an Action and put it in a menu like this:
let myAction = tiled.registerAction("MyActionName", function(action) {
//do your thing
});
myAction.text = "My Action"; //display name for the action
//Add it to the Tileset menu. If you want it somewhere else, you can choose a different menu.
//If you type tiled.menus into the Tiled console, you can see a full list of modifiable menus.
tiled.extendMenu("Tileset", [
{ action: "MyActionName", before: "TilesetProperties" }
]);
The above boilerplate is how I start most of my scripts.
For your actual action code (the “do your thing” bit), you’ll need to get the current tileset and make sure it’s a tileset like this:
let oldTileset = tiled.activeAsset;
if(!oldTileset.isTileset) return; //do nothing if the current document isn't a tileset
Then, you’ll need to get the replacement tileset. You can hard-code this, or prompt the user for the file using a Dialog.
However you get the path for the replacement tileset, you can open it with tiled.open
:
let newTileset = tiled.open(newTilesetPath);
if(!newTileset.isTileset) {
tiled.warn("The replacement tileset file is not a Tileset!");
return;
}
Then, you can iterate the maps and perform the replacement using TileMap.replaceTileset:
let assets = tiled.openAssets; //Using open maps here, but you can populate assets by other means too, e.g. by making a list of all maps in the Project
let exportFormat = tiled.mapFormat("json"); //The export format we'll use. Choose whichever one you want.
//You can get a full list of format shortnames for this function by typing tiled.mapFormats into the Tiled console
for(asset of assets) {
if(!asset.isTileMap) continue;
asset.replaceTileset(oldTileset, newTileset);
//Export the map:
//First, figure out the filename we'll use for the export. Let's go with the map's current filename, with the extension changed:
//In your code, I imagine you'll want to change the directory or something as well. Check out the FileInfo docs for some useful helper functions.
let filename = map.fileName;
//Get the base name (full path, minus extension)
filename = FileInfo.baseName(filename);
filename += ".tmj"; //Add the desired format's extension. Since we're using JSON above, use the JSON map's extension here.
//You may also want to handle the case that map.fileName is empty, which will be the case if the map has not been saved.
//Finally, do the actual export using this filename:
exportFormat.write(map, filename);
//Optional: Undo the tileset replacement, if only the export is meant to have the replacement done.
asset.undo();
}
In this example, I used the current asset as the tileset to replace, but perhaps you’ll want to do something else, depending on your desired workflow. Maybe you’ll hard-code a full list of tilesets and their replacements, for example, or use custom properties. In this case, both oldTileset and newTileset would probably be opened via tiled.open
, or obtained from each map’s list of tilesets.
It’s also possible to make this not a manually-triggered action, but a function that runs whenever a map is about to be saved, by listening for the tiled.assetAboutToBeSaved
function. In this case, you wouldn’t iterate all the maps, but would just do this work on the given map.
I doubt this example will do exactly what you need, but hopefully all this commentary will help you figure out how to use the API for your task.