Is there a way to create TileMapPattern from code and add it to the patterns of the TileMap?

Godot Version

4.3.dev2

Question

I want to store the patterns created by procedural content generation algorithms in the patterns of my TileMap via code so they appear in the dock window below.

Is there a way to add a new TileMapPattern to the patterns of the TileMap?
I can’t find a method for it, but maybe there’s a workaround.
image

If there’s no way to do this, is this feature planned for the near future?

var tile_map_pattern_size := Vector2i(16, 16)
var tile_map_pattern := TileMapPattern.new()
	
tile_map_pattern.set_size(tile_map_pattern_size)
	
for used_cell in get_used_cells(0):
	if used_cell.x > map_position.x + tile_map_pattern_size.x or used_cell.y > map_position.y + tile_map_pattern_size.y:
		continue
	
	var atlas_coords := get_cell_atlas_coords(0, used_cell)
	
	tile_map_pattern.set_cell(used_cell, 0, atlas_coords)
	
## works as expected
print(tile_map_pattern.get_used_cells())
var atlas_coords := []
for used_cell in tile_map_pattern.get_used_cells():
	atlas_coords.append(tile_map_pattern.get_cell_atlas_coords(used_cell))
print(atlas_coords)
	
# add the tile_map_pattern to the patterns of this tilemap somehow... 

You’re looking at TileMap, but the patterns are stored in TileSet.
https://docs.godotengine.org/en/stable/classes/class_tileset.html#class-tileset-method-add-pattern

Just add_pattern() on the TileSet instance to store it and it should show on the dock.

1 Like

Thank you! When I add this line though, the editor crashes… (I’m using a @tool script btw.

tile_set.add_pattern(tile_map_pattern)

Then you gotta file a bug report on the github 'cause that seems like it should just work.
https://github.com/godotengine/godot/issues

Make sure to include the least amount of files that still cause the bug in your project with the bug report. That will help pinpoint the issue. Some threading race condition is my guess.

2 Likes