Override/Extend regular tile placement tool

Hi there! Im trying to add function that let’s me mirror any tiles placed on my map onto another part of the map. But I can’t quite figure out how to connect to the tile placement action. I looked for a connect() signal, but didn’t find one.

I have a function that let’s me select an area of placed tiles and mirror those, but I also want to be able to mirror everything I place down instantly.

Is there a way to extend the regular tile placement tool? (Stamp Brush).

Thank you in advance!

I think you might be able to implement something like this using the TileMap.regionEdited signal. That one should be emitted whenever a tile layer is edited using some tool (except for scripted tools, which I think have no way of triggering this signal at the moment…).

Thank you for the quick answer! That does look like something I could use, however I cant seem to find that signal… it doesn’t recognize tiled.regionEdited. I’m quite new to this API so it would be awesome if you could poke me in the right direction :sweat_smile:

I thought I could do something like this:

// This function will be called whenever a tile is placed
function tilePlaced(region, layer 	) {
    // Logic
    console.log("Tile placed at (" + region.rect.x + "," + region.rect.y + ")");
}

// Connect the function to the "regionEdited" signal
var tiledPlaceReact = tiled.regionEdited.connect(tilePlaced);

But yeah, regionEdited is not recognized.

That’s because it is a signal on TileMap instances. If you want to hear about this signal from all opened tile maps, you can write it for example as follows:

function regionEdited(map, layer, region) {
    tiled.log(`Region ${region} of layer ${layer.name} on map ${map.fileName} has been edited`);
}

function connectRegionEdited(asset) {
    if (asset.isTileMap) {
        asset.regionEdited.connect((region, layer) => {
            regionEdited(asset, layer, region);
        });
    }
}

tiled.openAssets.forEach(connectRegionEdited)
tiled.assetOpened.connect(connectRegionEdited)

It uses both tiled.openAssets as well as tiled.assetOpened, to make sure the signals are connected also when the scripts get reloaded after the maps have been opened.

1 Like

This worked like a charm, thank you! :smiley: I just had to set a flag each time I ran it, otherwise it would run itself over and over again, as the signal fires whenever I place the mirrored tiles.

Thank you for the help, these will do wonders for my work flow :slight_smile:

That’s great to hear!

Interesting. In the documentation I wrote that this signal is not fired for changes made through scripts. So either the docs or the code will need to get fixed, heh.

Yes I read that as well, so was a bit surprised. Having it fire can be useful, but the ability to control if it should fire would also be neat. Either way it was easy to work around, for my case at least.