Hello everyone. How I can create and change the certain tiles of tile_set?

Godot Version

Godot version 4.3

Question

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

You need to access the tileset of the tilemap and then can use this method:

What we can do is make an image that is exactly one tile, like this:

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.

I made a little program within that script.

func _process(delta: float) -> void:
	if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
		var mouse_coord := local_to_map(get_global_mouse_position())
		var color := Color8(randi_range(0, 255), randi_range(0, 255), randi_range(0, 255), 128)
		set_cell_color(mouse_coord, color)

man, big thank you. You’re really cool, I more found, but its very rarely

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.