# Tiled API scripting: extras

**URL:** <https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863>\
**Category:** Questions\
**Created:** [July 18, 2024, 12:02am UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863 "2024-07-18T00:02:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)\
**Post date:** [July 18, 2024, 12:02am UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/1 "2024-07-18T00:02:36Z")

</div>

I have implemented a custom tile inserter, but I was wondering if the script had access to the floating, transparent image of the currently selected tile.

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

I am also attempting to change an object’s name based on a somewhat static property.

For example. When I select my item’s name, I want to somehow change the name of the object. Is this possible?

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

It did seem like `Interface Signal` would be my answer, but there is nothing I see that shows `Property Changed`, etc. ☹

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [July 18, 2024, 5:54am UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/2 "2024-07-18T05:54:42Z")

</div>

> [@Bongo](#):
>
> I was wondering if the script had access to the floating, transparent image of the currently selected tile

Do you mean [`tiled.mapEditor.currentBrush`](https://www.mapeditor.org/docs/scripting/interfaces/MapEditor.html#currentBrush)? When you mean how to control it for a custom tool, see [`Tool.preview`](https://www.mapeditor.org/docs/scripting/interfaces/Tool.html#preview).

> [@Bongo](#):
>
> When I select my item’s name, I want to somehow change the name of the object. Is this possible?

Unfortunately there is currently no signal being emitted to the script when you change the property. There is also no way yet for the script to set a different text on the object’s label, the only way to change it is to actually assign to `object.name`.

What you could do is to register a custom action that you manually trigger, which will update the object names as appropriate. You could also attach such an update to [`tiled.assetAboutToBeSaved`](https://www.mapeditor.org/docs/scripting/modules/tiled.html#assetAboutToBeSaved).

---

<div class="post-metadata">

**Author:** ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)\
**Post date:** [July 18, 2024, 11:41am UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/3 "2024-07-18T11:41:59Z")

</div>

> [@bjorn](#):
>
> Do you mean [`tiled.mapEditor.currentBrush`](https://www.mapeditor.org/docs/scripting/interfaces/MapEditor.html#currentBrush)? When you mean how to control it for a custom tool, see [`Tool.preview`](https://www.mapeditor.org/docs/scripting/interfaces/Tool.html#preview).

I can’t seem to get this to work. I am sure I am doing something wrong as its use is not too clear to me.

> [@bjorn](#):
>
> Unfortunately there is currently no signal being emitted to the script when you change the property. There is also no way yet for the script to set a different text on the object’s label, the only way to change it is to actually assign to `object.name`.
> 
> What you could do is to register a custom action that you manually trigger, which will update the object names as appropriate. You could also attach such an update to [`tiled.assetAboutToBeSaved`](https://www.mapeditor.org/docs/scripting/modules/tiled.html#assetAboutToBeSaved).

This is actually what I ended up doing last night. Funny you should post this option. Thanks. 🙂

---

<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:** [July 18, 2024, 11:52am UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/4 "2024-07-18T11:52:23Z")

</div>

> [@Bongo](#):
>
> > [@bjorn](#):
> >
> > Do you mean [`tiled.mapEditor.currentBrush`](https://www.mapeditor.org/docs/scripting/interfaces/MapEditor.html#currentBrush)? When you mean how to control it for a custom tool, see [`Tool.preview`](https://www.mapeditor.org/docs/scripting/interfaces/Tool.html#preview).
> 
> I can’t seem to get this to work. I am sure I am doing something wrong as its use is not too clear to me.

Which part? Are you writing a Tool, or something else? Perhaps you should share your code, or explain exactly what you’re doing, since it’s hard to help with no idea of what you’re doing.

---

<div class="post-metadata">

**Author:** ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)\
**Post date:** [July 18, 2024, 11:55am UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/5 "2024-07-18T11:55:48Z")

</div>

Hello, I am actually reviewing some of your tool sources now to better understand how `Tool.preview` works. I thought it was much simpler and straight forward to show a preview of the currently selected object, but looking at your tool sources, it seems much more involved.

All I want to do is show a preview of the currently selected tile as a preview.

---

<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:** [July 18, 2024, 12:07pm UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/6 "2024-07-18T12:07:06Z")

</div>

What do you mean by “your tool sources”? If you mean the scripts I’ve written: Mine are more complicated than they strictly need to be.

You need to assign a TileMap to `preview`, this map containing the modified tile at the correct location. The easiest way to do this consistently is to, any time you want to replace the preview (e.g. on `tilePositionChanged`):

```js
//Create a new TileMap. You could reuse the old one if it exists, but then you'll need to clear any existing preview.
let preview = new TileMap();
preview.width = this.map.width; //"this" being the Tool
preview.height = this.map.height;
let layer = new TileLayer();
preview.addLayer(layer);

//Set up the actual tile(s) you want to preview
let layerEdit = layer.edit();
layerEdit.setTile(x, y, newTile, newFlags); //the tile you want to display. flags are optional.
layerEdit.apply();

this.preview = preview; //the preview is only actually updated when you assign to Tool.preview
}
```

---

<div class="post-metadata">

**Author:** ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)\
**Post date:** [July 18, 2024, 12:19pm UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/7 "2024-07-18T12:19:24Z")

</div>

> [@eishiya](#):
>
> ```auto
> let layerEdit = layer.edit();
> layerEdit.setTile(x, y, newTile, newFlags); //the tile you want to display. flags are optional.
> layerEdit.apply();
> 
> this.preview = preview;
> 
> ```

Ah, go it. Yes, your scripts were doing more than I needed. Thanks for the help. My tool is strictly for `ObjectGroup`s so I made the necessary adjustments.

---

<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:** [July 18, 2024, 12:23pm UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/8 "2024-07-18T12:23:31Z")

</div>

AFAIK objects cannot be displayed in previews currently, Object layers are ignored. So you’d still have to use a tile layer to preview Tile Objects if you want to preview at all. There’s an issue open about this: [Scripting: Allow MapObjects in Tool previews · Issue #3383 · mapeditor/tiled · GitHub](https://github.com/mapeditor/tiled/issues/3383)

---

<div class="post-metadata">

**Author:** ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)\
**Post date:** [July 18, 2024, 12:26pm UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/9 "2024-07-18T12:26:38Z")

</div>

Lol. Yes, I just realized this when all errors popped up in my console. Going to switch it back to TileLayer and try it again.

---

<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:** [July 18, 2024, 12:31pm UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/10 "2024-07-18T12:31:14Z")

</div>

Object layers are ignored silently, they don’t create errors. The errors you saw were probably because there’s no edit() for Object layers, you can add objects directly. Only Tile Layers have TileLayerEdit, this is required by the API to enable it to group together edits and thus make modifying tile layers more performant.

---

<div class="post-metadata">

**Author:** ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)\
**Post date:** [July 18, 2024, 12:34pm UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/11 "2024-07-18T12:34:15Z")

</div>

> [@eishiya](#):
>
> ```auto
> let layerEdit = layer.edit();
> layerEdit.setTile(x, y, newTile, newFlags); //the tile you want to display. flags are optional.
> layerEdit.apply();
> 
> this.preview = preview;
> 
> ```

I notice that you create a new instance of TileMap() each time the mouse moves to a new cell and `showpreview` is called. Is that necessary?

I assumed you still required preparepreview / showpreview though your above example did not indicate it as much.

---

<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:** [July 18, 2024, 12:37pm UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/12 "2024-07-18T12:37:09Z")

</div>

As I wrote in the comment at the top of the example, no, this is not necessary. But you would need to either clear or delete and recreate the existing layer to keep the preview from showing a trail of tiles. Creating a new one each time is simpler (no need to check for an existing preview map, layer, adjust size when the current document changes, etc), and at least in my experience hasn’t caused performance issues.

---

<div class="post-metadata">

**Author:** ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)\
**Post date:** [July 18, 2024, 12:40pm UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/13 "2024-07-18T12:40:08Z")

</div>

All I had to do was scroll over a bit to see that. Lol.  
Cool. Working on implementation now. Will let you know how it turns out. Thanks again.

---

<div class="post-metadata">

**Author:** ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)\
**Post date:** [July 18, 2024, 1:36pm UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/14 "2024-07-18T13:36:27Z")

</div>

> [@eishiya](#):
>
> As I wrote in the comment at the top of the example, no, this is not necessary. But you would need to either clear or delete and recreate the existing layer to keep the preview from showing a trail of tiles. Creating a new one each time is simpler (no need to check for an existing preview map, layer, adjust size when the current document changes, etc), and at least in my experience hasn’t caused performance issues.

Hey again.

Okay, something is still wrong. ☹  
The tile is not showing and I have no errors. I commented out some things and tried to display all in `preparepreview` but still nothing. Maybe you can help me figure it out? Thanks.

```auto
// X, Y = this.tilePosition.x/y
preparePreview(x, y) {
		let preview = new TileMap();
		preview.width = this.map.width; //"this" being the Tool
		preview.height = this.map.height;
		let layer = new TileLayer();
		preview.addLayer(layer);

		const brush = tiled.mapEditor.currentBrush.layerAt(0);
		const tile = brush.tileAt(0, 0);
		tiled.log("TILE: " + tile );

		let layerEdit = layer.edit();
		layerEdit.setTile(x, y, tile, 0); //the tile you want to display. flags are optional.
		layerEdit.apply();

		// if (this.map) {
		// preview.setSize(this.map.width, this.map.height);
		// preview.setTileSize(this.map.tileWidth, this.map.tileHeight);
		// }
		this.preview = preview;
	},

	showPreview(x, y) {
		this.preparePreview(x, y);

		// if (!this.map || !this.map.currentLayer) return;

		// let currentLayer = this.map.currentLayer;
		// if (!currentLayer.isObjectLayer) return;
		
		// const brush = tiled.mapEditor.currentBrush.layerAt(0);
		// const tile = brush.tileAt(0, 0);
		// tiled.log("TILE: " + tile );

		// let layer = this.preview.layerAt(0);
		// let layerEdit = layer.edit();
		// layerEdit.setTile(x, y, tile, 0); //the tile you want to display. flags are optional.
		// layerEdit.apply();

		// this.preview = preview;
	}

```

---

<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:** [July 18, 2024, 1:38pm UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/15 "2024-07-18T13:38:52Z")

</div>

Are you calling `showPreview()` anywhere?

---

<div class="post-metadata">

**Author:** ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)\
**Post date:** [July 18, 2024, 1:39pm UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/16 "2024-07-18T13:39:40Z")

</div>

Calling it here:

```auto
mouseMoved(x, y, /*, modifiers */) {
		let positionChanged = (this.lastModifiedTile.x != this.tilePosition.x || this.lastModifiedTile.y != this.tilePosition.y);

		if (positionChanged){
// tiled.log("MOVED: " + this.tilePosition.x + ", " + this.tilePosition.y)
			this.showPreview(this.tilePosition.x, this.tilePosition.y);
			this.lastModifiedTile.x = this.tilePosition.x;
			this.lastModifiedTile.y = this.tilePosition.y;
		}
	},

```

---

<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:** [July 18, 2024, 1:45pm UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/17 "2024-07-18T13:45:44Z")

</div>

Do any of your `tiled.log`s run, or are these methods just not getting to run at all? Nothing jumps out at me.

If the methods are running but the preview’s not working, try setting the tile size. I don’t remember whether that’s required or not. It might be, and in my code I do set it, e.g.

```js
let preview = new TileMap();
preview.setSize(this.map.width, this.map.height);
preview.setTileSize(this.map.tileWidth, this.map.tileHeight);
preview.infinite = this.map.infinite;

```

By the way, you shouldn’t be using `mouseMoved` if you only want to do things when the mouse changes tile location. There’s `tilePositionChanged` for that instead, and that should also remove the need to store/check against the tile position. `mouseMoved` is for when you actually care about changing position within a cell.

---

<div class="post-metadata">

**Author:** ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)\
**Post date:** [July 18, 2024, 3:13pm UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/18 "2024-07-18T15:13:31Z")

</div>

All logs run, and `tile` does contain an item. Nothing is being displayed though.

---

<div class="post-metadata">

**Author:** ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)\
**Post date:** [July 18, 2024, 4:01pm UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/19 "2024-07-18T16:01:02Z")

</div>

> [@eishiya](#):
>
> Do any of your `tiled.log`s run, or are these methods just not getting to run at all? Nothing jumps out at me.
> 
> If the methods are running but the preview’s not working, try setting the tile size. I don’t remember whether that’s required or not. It might be, and in my code I do set it, e.g.

Okay, I think I got it now. I had to use `this.selectedTile` in order to see what I needed.

It seems to not work with ObjectGroups. TileLayers are working fine, but I only need it for ObjectGroups. Oh well. Thanks again.

---

<div class="post-metadata">

**Author:** ![Bongo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bongo/32/4246_2.png) [@Bongo](https://discourse.mapeditor.org/u/Bongo)\
**Post date:** [July 21, 2024, 11:07pm UTC](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863/20 "2024-07-21T23:07:48Z")

</div>

@bjorn @eishiya Hey folks. My apologies for tagging you, but I have a custom tools question and did not want to start a new thread.

When adding custom tools, is it possible to get access to the flipping tools or not?

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

[Next page](https://discourse.mapeditor.org/t/tiled-api-scripting-extras/6863.md?page=2)
