I create strategy:
But for different countries, I need a lot of different tiles:
How I can create a new tiles with other color, using the one tile?
Its like to creaate the alternative tile, but using code
Make sure that this image is the only one, and its ID is 0. Then we can create alternatives from this father tile.
extends TileMapLayer
# Used to store the alternative tile id of a color
var color_dict := {}
# The source of our lovely single-white-tile image
@onready var atlas_source: TileSetAtlasSource = tile_set.get_source(0)
# A handy method to handle the data
func add_color_tile(color: Color) -> void:
# Create an alternative tile from the single tile
var alternative_id := atlas_source.create_alternative_tile(Vector2i.ZERO)
# Get the tile data of that newly created one, and change the modulate to the color
atlas_source.get_tile_data(Vector2i.ZERO, alternative_id).modulate = color
# Which alternative tile id did the color correspond to? We will store it!
color_dict[color] = alternative_id
# Another handy method to set the color cells
func set_cell_color(coord: Vector2i, color: Color) -> void:
# If we do not have the color cell, we create that cell
if not color_dict.has(color):
add_color_tile(color)
# Set the coord with that color cell
set_cell(coord, 0, Vector2.ZERO, color_dict[color])
I’m using TileMapLayer, if you’re using TileMap, the logic will still be the same, just pass in another layer with your layer.