# How to automatically generate a layer with automap rules?

**URL:** https://discourse.mapeditor.org/t/how-to-automatically-generate-a-layer-with-automap-rules/6485
**Category:** Questions
**Created:** [January 2, 2024, 9:51am UTC](https://discourse.mapeditor.org/t/how-to-automatically-generate-a-layer-with-automap-rules/6485 "2024-01-02T09:51:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![lilianzz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/lilianzz/32/4389_2.png) [@lilianzz](https://discourse.mapeditor.org/u/lilianzz)
#### Post date: [January 2, 2024, 9:51am UTC](https://discourse.mapeditor.org/t/how-to-automatically-generate-a-layer-with-automap-rules/6485/1 "2024-01-02T09:51:47Z")

</div>

Is there a script that creates a layer with output\_\* based on the automapping rules from Tiled?  
(unfortunately as a novice I can’t attach files for example)  
I automatically create a map image \*.png  
and Tiles map - \*.json  
with links to the base tiles from the file \*.png  
and there is a map with rules /Rules/ \*.json  
and tails for which /Rules/ \*.png  
in accordance with the rules, replacements must be made.

At the moment, after each map generation, you have to go into Tiled each time and press Ctrl + M to update the layer ids tails.

---

<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 2, 2024, 7:40pm UTC](https://discourse.mapeditor.org/t/how-to-automatically-generate-a-layer-with-automap-rules/6485/2 "2024-01-02T19:40:56Z")

</div>

I do not understand what you’re asking, your post is worded confusingly. Are you looking to create rules based on some template? To automatically apply rules you have already created? Something else? How do you “generate” maps?

If you’re generating maps via Tiled script, you could add `tiled.trigger("AutoMap")` to your script to apply all your existing Automapping rules (this will only work if the Tiled GUI is open though, it won’t work if you’re doing this via CLI).  
If you’re drawing maps manually, you can potentially use Automap While Drawing.

---

<div class="post-metadata">

### Author: ![lilianzz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/lilianzz/32/4389_2.png) [@lilianzz](https://discourse.mapeditor.org/u/lilianzz)
#### Post date: [January 4, 2024, 9:28am UTC](https://discourse.mapeditor.org/t/how-to-automatically-generate-a-layer-with-automap-rules/6485/3 "2024-01-04T09:28:53Z")

</div>

I apologize for the vague question, I’ll try to explain the problem I’m trying to solve again. Perhaps you will have ideas on how it would be better.

I use simplex noise for procedural map generation  
 ![gen_output_map](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/b/b76b94fd38583f99b0898197d1ebb3a342d372e8.png)  
and create a json file that is read in Tiled and contains layers with the id of each tile and a tileset in the form of all the colors of the map  
 ![gen_tileset](https://canada1.discourse-cdn.com/flex035/uploads/mapeditor/original/2X/c/c3f4fc7ba6568139acb8413bd5ade0d97a795b4c.png)  
As you can see, the attached map is just a set of colors - so I’m looking for a way to quickly use the automap rules in accordance with the rules.  
(as I said above, now the visual part is created only through Tiled by pressing ctrl-M)  
At the same time, I don’t want to give up the Tiled format due to its support by my game engine (Python Arcade) and the convenience of the GUI in some moments

---

<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 4, 2024, 1:23pm UTC](https://discourse.mapeditor.org/t/how-to-automatically-generate-a-layer-with-automap-rules/6485/4 "2024-01-04T13:23:50Z")

</div>

Thanks for the context, but I still don’t understand exactly what you’re trying to do. Are you looking to apply Automapping automatically to these flat tiles (do you already have the Automapping rules made, or do you need to create those)? Are you looking to replace the external generator with one inside Tiled which will also do Automapping? Are you even talking about Tiled Automapping or something else?

If you want to apply Automapping automatically: You can do this with a Tiled script when the map opens. The easiest way to do this is to add a custom property such as “autoMapMe” to the Tiled map your generator creates, and in Tiled, have a script something like this:

```js
tiled.assetOpened.connect( function(asset) {
   if(!asset.isTileMap) return;
   if(asset.property("autoMapMe") == true) {
      asset.autoMap("path/to/rules");
      asset.removeProperty("autoMapMe");
   }
});

```

This script checks if any map newly opened in Tiled has the “autoMapMe” property, and if it does, it applies automapping to it, and then removes that property. This script requires a path to the rules, but if you want to use the `rules.txt` at the map’s location, you can replace `asset.autoMap()` with

```js
tiled.activeAsset = asset;
tiled.trigger("autoMap");

```

The assignment to `activeAsset` makes sure the Automapping is applied to that asset and not to some other document, just in case the recently opened map isn’t the active tab.
