Add custom propety in a tileset

Hi,
I know how to add properties to objects, but I am having some problems to add them to a tile in a tileset. For instance, I want to add a int property called surfaceTypeId.

// Register a new action called "TemplateAction"
let surfaceTypeAction = tiled.registerAction("surfaceTypeAction", function(action) 
{
    let tilemap = tiled.activeAsset;
    
    // Ensure there's a valid tilemap
    if (!tilemap) {
        tiled.alert("No active tilemap found.");
        return;
    }
    
    const selectedTiles = tiled.tilesetEditor?.currentTiles;

    if (!selectedTiles || selectedTiles.length === 0) {
        tiled.alert("No tiles selected in the Tileset Editor.");
        return;
    }

   
    else 
    {
        selectedTiles.forEach(tile => 
        {
            // Check if the "imagePath" property exists by trying to get its value
            let surfaceTypeId = tile.property("surfaceTypeId");
            if (surfaceTypeId === undefined) 
            {
                let id = parseInt(0); // Ensure the value is treated as an int
                tile.setProperty("surfaceTypeId", id);
            }
            else 
            {
                tiled.alert("entityType already exists");
            }
        });
    }
});

surfaceTypeAction.text = "add surfaceType attr"; // Display name in the menu

tiled.extendMenu("Edit", [
	{ action: "surfaceTypeAction", before: "MapProperties" }
]);

The problem is that it keeps triggering “No tiles selected in the Tileset Editor.” even if I have them selected. I guess I am mixing up the tileset selection and the main editor selection?
I am not sure how to solve it.

Thx

There doesn’t seem to be any such property as currentTiles in the TilesetEditor class. Did you see otherwise somewhere?

Tileset has a selectedTiles property.

You’re right. I got it mixed up with currentTileset.
Got it now. I just changed this:

    const selectedTiles = tiled.mapEditor.tilesetsView.selectedTiles;

    if (selectedTiles.length === 0) {
        tiled.alert("No tiles selected in the Tileset Editor.");
        return;
    }
   

thanks for pointing that out.

1 Like