Changing parts of scenes that are a part of a TileMapLayer during gameplay?

for example, if i have coordinates for a tile that is an instanced scene and i want to change the color a colorrect that is inside of the scene, how do i go about actually getting to that scene from the coordinates and changing the color?

i’ve looked into tilemap tutorials and the docs and everything, but i can’t figure out where to even start for this one. i’m using godot 4.3, if that changes anything

I had the same question too, but I came up with the same conclusion. As far as I know, there’s a straightforward but not elegant solution.

The scene tiles are child nodes of the TileMapLayer at runtime, but keep in mind that they are instantiated after the ready() function is called. You can iterate through to find one that lands on your coordinate.

1 Like

If the goal is not to access complex scene tiles but rather to create modulated tiles, I recommend manipulating textures with the Image class or creating alternative tiles with the modulate assigned.

Below is an example of making alternative tiles with modulate.

extends TileMapLayer

@onready var atlas_source: TileSetAtlasSource = tile_set.get_source(0)

func add_color_tile(color: Color) -> int:
	var alternative_id := atlas_source.create_alternative_tile(Vector2i.ZERO)
	atlas_source.get_tile_data(Vector2i.ZERO, alternative_id).modulate = color
	return alternative_id

Below is another demo about operations with the Image class.

extends Sprite2D

@export var size: Vector2i

var image: Image

func ready() -> void:
	image = Image.create_empty(size.x, size.y)
	texture = ImageTexture.create_from_image(image)

func set_pixel(coord: Vector2i, color: Color) -> void:
	image.set_pixelv(coord, color)

	# Note: This method is expensive,
	# so call it at the end if you're going to set multiple pixels at once.
	(texture as ImageTexture).update(image)