How can I fill an Object up with a Terrain Set?

Hey there!

Say I’ve got an object defined as a polygon aligned to the tilegrid, is there a way to fill it (or an associated Tile Layer) up with a tileset?

I’ve tried selecting the object in the Object tab, going to Layers tab, then clicking fill on the selection, but it fills the entire map, instead of just the selected object’s polygon.

Would this be something I could write in a script? I took a look at the Tileset docs and it seemed more like manipulating the tileset itself, rather than using it, although the tile-rectangle example uses the Tilemap which looks like it could also be a tileset.

In short, I’m wondering if I can somehow fill a selection (or ideally a selected object’s custom polygon) with a tileset. If not, is there some way I can write a custom extension to implement this for me?

edit: the tile-rectangle.js gets pretty close in action. If I can figure out how to get it to use the tileset instead of the selected tiles, and then also floodfill, I’ll be lookin’ good

These are called Wangs, and I appear to be close: tiled.mapEditor.tilesetsView.currentTileset.wangSets lists one that hopefully contains what I want to use. It looks like it’s basically a ‘layer’ of values for which color each tile in the tileset is painted. Then you can call myWangSet.wangId(tileFromMyTileset) to get an array of values to figure out which color its painted.

I don’t see any way to call something to deal with wangs, so I’ll have to write that bit myself then, checking for neighboring tiles’ wang id and choose the current tile based on that.

You would have to script this, correct.

The tile-rectangle example uses your current brush (which is stored as its own little TileMap) to decide what to paint, it’s basically like an impromptu simplified terrain definition.

I’d previously tried writing a tool to make it easy to draw big rectangles filled with a given Terrain, but ran into the problem that scripting has no way to access which Terrain or TerrainSet is currently selected in the GUI. A way around it would be to require the user to select a tile and its its wangId to decide what Terrain to paint with. You’ll still need some way to decide which WangSet to use though, some tiles may be labelled in multiple sets.

The scripting API also offers no way to find a tile by wangId, so you’d need to search by iterating all the tiles, or build your own data structures sorted by wangId.

As you said, you’ll need to reimplement the Terrain logic yourself. Since you know the shape you’re building though, you can probably implement a simpler version that just places a bunch of tiles that have wangId = all corners and/or edges are of the desired colour and then adjusts the edges of the shape to match the surrounding tiles where such tiles are available.

1 Like