Rotating tiles in a tilemaplayer

Godot 4
Hi, I’d like to know how to put tiles with a 90, 180, -90 or 0° rotation on a tile in the TileMapLayer. Here’s the code for what I’ve started :slight_smile:

extends Node2D

# Référence aux TileMapLayer
@export var tilemap_bg: TileMapLayer
@export var tilemap_fg: TileMapLayer
@export var tilemap_spikes: TileMapLayer
@export var cursor_sprite: Sprite2D
var flip_h := TileSetAtlasSource.TRANSFORM_FLIP_H
var flip_v := TileSetAtlasSource.TRANSFORM_FLIP_V
var transpose := TileSetAtlasSource.TRANSFORM_TRANSPOSE
# Taille de la grille (188px par case)
const TILE_SIZE = 188
# ID du brush sélectionné
var chosen_brush = 1
var rotation1 = 90
# Dictionnaire des tuiles à placer selon le brush
var BLOCK_DATA = {
	1: {"bg_source": 3, "bg_x": 0, "bg_y": 0, "fg_source": 1, "fg_x": 0, "fg_y": 0},  
	2: {"bg_source": 3, "bg_x": 0, "bg_y": 1, "fg_source": 1, "fg_x": 0, "fg_y": 1},  
	3: {"bg_source": 3, "bg_x": 0, "bg_y": 2, "fg_source": 1, "fg_x": 0, "fg_y": 2, "spike_source": 0, "spike_x": 0, "spike_y": 0}  
}

var is_placing = false  # Indique si on maintient le clic gauche

func _ready():
	cursor_sprite.visible = true

func _process(delta):
	update_cursor_position()
	
	# Si le bouton est maintenu, on continue à placer les blocs
	if is_placing:
		place_block()

func _input(event):
	if event is InputEventMouseMotion:
		update_cursor_position()

	# Quand on appuie sur le bouton gauche
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT:
			is_placing = event.pressed  # Active/désactive le placement
			if event.pressed:
				place_block()  # Place un premier bloc immédiatement

	if event is InputEventKey and event.pressed:
		if event.keycode == KEY_A:
			chosen_brush = (chosen_brush % 3) + 1  # Cycle entre 1, 2 et 3
		elif event.keycode == KEY_Z:
			chosen_brush = max(1, chosen_brush - 1)
		elif event.keycode == KEY_R:
			if rotation1 > 90:
				rotation1 = -90
			else:
				rotation1 += 90
		
			

func update_cursor_position():
	""" Déplace le curseur en suivant la souris et l'aligne sur la grille """
	var mouse_pos = get_global_mouse_position()
	var grid_x = int(floor(mouse_pos.x / TILE_SIZE)) * TILE_SIZE
	var grid_y = int(floor(mouse_pos.y / TILE_SIZE)) * TILE_SIZE

	cursor_sprite.position = Vector2(grid_x, grid_y)

func place_block():
	""" Place un bloc aux coordonnées actuelles """
	var tile_pos = Vector2i(int(cursor_sprite.position.x / TILE_SIZE), int(cursor_sprite.position.y / TILE_SIZE))

	if chosen_brush in BLOCK_DATA:
		var data = BLOCK_DATA[chosen_brush]

		if "bg_source" in data:
			tilemap_bg.set_cell(tile_pos, data["bg_source"], Vector2i(data["bg_x"], data["bg_y"]))
		if "fg_source" in data:
			tilemap_fg.set_cell(tile_pos, data["fg_source"], Vector2i(data["fg_x"], data["fg_y"]))
		if "spike_source" in data:
			tilemap_spikes.set_cell(tile_pos, data["spike_source"], Vector2i(data["spike_x"], data["spike_y"]))

You need to encode it in the alternative_tile parameter of TileMapLayer.set_cell() Explanation on how to do it here TileSetAtlasSource — Godot Engine (stable) documentation in English

Sorry, I don’t understand anything, I use a tilemaplayer and name a tilemap, in this doesn’t work or maybe I’m doing it wrong, do you have a code solution?