# How to make a command to change the probability of certain tiles?

**URL:** <https://discourse.mapeditor.org/t/how-to-make-a-command-to-change-the-probability-of-certain-tiles/5989>\
**Category:** Questions\
**Created:** [January 27, 2023, 7:28pm UTC](https://discourse.mapeditor.org/t/how-to-make-a-command-to-change-the-probability-of-certain-tiles/5989 "2023-01-27T19:28:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlosandresRG](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@carlosandresRG](https://discourse.mapeditor.org/u/carlosandresRG)\
**Post date:** [January 27, 2023, 7:28pm UTC](https://discourse.mapeditor.org/t/how-to-make-a-command-to-change-the-probability-of-certain-tiles/5989/1 "2023-01-27T19:28:34Z")

</div>

Hi, Im new to Tiled (im using Tiled 1.9.2) and I’ve been messing around the terrain. As far as I know, when two tiles have the same terrain pattern Tiled automatically randomizes those two tiles when drawing a terrain, but is there a way to change the probability of one of those two tiles to 0 and make the other one 1 (or 100), and then do that the other way around?

Im using the tileset shown in the image below

 ![TerrainTIles](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/2/22b87b75e0f6a2201df2a933755c210aab8f90a4.png)

These are the terrains im using  
 ![image](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/7/7535ded3be3b38e33f835a4a8ee279911e91285d.png)

As you can see there are tiles with the same pattern, but I want to shift between those at will, or even change their probability on the go, inside one terrain set.

I currently have two different terrain set, one for these  
 ![image](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/e/e4b76d9e80bebaeb9eaf3c1cf38c5ff0f364090c.png)

and one for these (without the 2 on the top right corner that has that black line)  
 ![image](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/e/e8ba388fca608715ef3d70229ebd01a84bdb8db3.png)

but i have some problems that i have to manually correct.

I know this is a bunch of text so thank you if you read all of this, and hope you can help 🙂

---

<div class="post-metadata">

**Author:** ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)\
**Post date:** [January 27, 2023, 7:52pm UTC](https://discourse.mapeditor.org/t/how-to-make-a-command-to-change-the-probability-of-certain-tiles/5989/2 "2023-01-27T19:52:49Z")

</div>

You can do this with a script. Iterate over the tiles in the tileset, and set their `probability` property.

I haven’t tested this, but here is some code to get you started:

```javascript
let toggleProbability = tiled.registerAction("toggleTileProbability", function(action) {
	//Get the tileset to modify:
	let tileset = tiled.activeAsset;
	//If the active document is a map, grab the tileset currently selected for Terrain use:
	if(tileset.isTileMap) {
		let terrainSet = tiled.mapEditor.currentWangSet;
		if(terrainSet) {
			tileset = terrainSet.tileset;
		} else tileset = null;
	} //otherwise, tileset is a Tileset
	if(!tileset) return; //If there's no tileset available, don't do anything
	
	//Now that we have the tileset, get toggling:
	let tiles = tileset.tiles; //save a local copy of the tile list to speed up access
	for(tile of tiles) {
		//If you don't want to toggle *every* tile's probability,
		//check for some property (e.g. terrains or a custom property) here,
		//and continue if the check fails, e.g.
		//if(!tile.property("includeInToggle")) continue;
		if(tile.probability > 0)
			tile.probability = 0;
		else
			tile.probability = 1;
	}
}
toggleProbability.text = "Toggle Tile Probability";
toggleProbability.shortcut = "Ctrl+Shift+T"; //your shortcut here

```

I don’t see how this would achieve anything that swapping terrain sets wouldn’t though xP

---

<div class="post-metadata">

**Author:** ![carlosandresRG](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@carlosandresRG](https://discourse.mapeditor.org/u/carlosandresRG)\
**Post date:** [January 27, 2023, 10:45pm UTC](https://discourse.mapeditor.org/t/how-to-make-a-command-to-change-the-probability-of-certain-tiles/5989/3 "2023-01-27T22:45:35Z")

</div>

> I don’t see how this would achieve anything that swapping terrain sets wouldn’t though xP

Well, when swapping terrain sets there are parts that doesn’t connect automatically, like in this example

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/4/4210468f1a0aaa52d49172439d7ada8fabcba4cb.png)

and yeah, This could be fixed manually, but being able to change between flat and sloped corners on the go would be easier and more time saving. This case there are only 2 options, but i imagine there could be scenarios where 3 o more different type of corners could be needed to make a map.

---

<div class="post-metadata">

**Author:** ![carlosandresRG](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@carlosandresRG](https://discourse.mapeditor.org/u/carlosandresRG)\
**Post date:** [January 27, 2023, 10:54pm UTC](https://discourse.mapeditor.org/t/how-to-make-a-command-to-change-the-probability-of-certain-tiles/5989/4 "2023-01-27T22:54:33Z")

</div>

> //If you don’t want to toggle _every_ tile’s probability,  
> //check for some property (e.g. terrains or a custom property) here,  
> //and continue if the check fails, e.g.  
> //if(!tile.property(“includeInToggle”)) continue;

Would this work with specific terrain patches? so tile A with the terrain patch would swap with tile B with the same terrain patch

---

<div class="post-metadata">

**Author:** ![carlosandresRG](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@carlosandresRG](https://discourse.mapeditor.org/u/carlosandresRG)\
**Post date:** [January 27, 2023, 11:00pm UTC](https://discourse.mapeditor.org/t/how-to-make-a-command-to-change-the-probability-of-certain-tiles/5989/5 "2023-01-27T23:00:09Z")

</div>

Also, any idea why my terrain brush default size is set to 2x2 instead of 3x3? when I press command (mac user) it grows

---

<div class="post-metadata">

**Author:** ![eishiya](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/eishiya/32/2534_2.png) [@eishiya](https://discourse.mapeditor.org/u/eishiya)\
**Post date:** [January 28, 2023, 4:20pm UTC](https://discourse.mapeditor.org/t/how-to-make-a-command-to-change-the-probability-of-certain-tiles/5989/6 "2023-01-28T16:20:08Z")

</div>

There’s no such thing as a “default terrain brush size”. The Terrain Brush will modify as many tiles as it needs to in order to make the change you request, which may much larger than 3x3.  
By default, you modify a single corner (or edge), and this usually requires updating at least a 2x2 region (more if the desired transition tiles don’t exist). When you hold command, you modify all four corners of the tile you’re hovering on, rather than the closest single corner.

Re: swapping patches: My code was meant to just give you a basic example of how to use the API (make an action, get the tileset, check custom properties, change tile probability), it’s up to you to implement the actual logic you want. You can add different custom properties to each patch of tiles and have the script look for those, or you can do it some other way, it’s up to you.
