Trying to rotate my tiles, but it's not working smoothly

Godot Version

4.4.1.stable

Question

I’m trying to add a feature where you can rotate a tile when you press a keyboard key. However, it doesn’t seem to work smoothly. The tile sometimes rotates, but sometimes it doesn’t. The idea is to have a separate tile for each orientation, then change the x-coordinate of the atlas coordinate, to ‘rotate’ the tile. Below is how I have my tiles organized:

Code below:

extends TileMapLayer

@onready var h_box_container: HBoxContainer = $"../HUD/Panel/HBoxContainer"

var mouse_position: Vector2i
var old_mouse_position: Vector2i
var cargo_selected = Vector2i(11, 0)

var rotational_adder: int

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float):
	mouse_position = local_to_map(get_local_mouse_position())
	if old_mouse_position != mouse_position:
		set_cell(mouse_position, 0, cargo_selected)
		erase_cell(old_mouse_position)
		old_mouse_position = mouse_position
	if Input.is_action_pressed("ROTATE_RIGHT"):
		cargo_selected.x += rotational_adder
		if rotational_adder == 1 and cargo_selected.x > 3:
			cargo_selected.x = 0
		if rotational_adder == 2 and cargo_selected.x > 6:
			cargo_selected.x = 0
		if rotational_adder == 3 and cargo_selected.x > 9:
			cargo_selected.x = 0
	
func button_pressed():
	if h_box_container.btn.text == "1 X 1":
		cargo_selected = Vector2i(0, 0)
		rotational_adder = 1
	elif h_box_container.btn.text == "2 X 2":
		cargo_selected = Vector2i(0, 1)
		rotational_adder = 2
	elif h_box_container.btn.text == "3 X 3":
		cargo_selected = Vector2i(0, 3)
		rotational_adder = 3

Yeah, I’m not sure what the issue might be tbh, but what if you try and simplify things a little just so there’s less noise?
Can you try and stick just with one image size? leave no if’s, just assume that there’s only one type of image you can select. So, the button_pressed function won’t be necessary, you could, in the _ready fuinction assign the cargo_selected value like what you do in the first if of the button_pressed function.

func _ready():
	cargo_selected = Vector2i(0, 0)

After that, in your_process function, change the ROTATE_RIGHT if to something like this:

if Input.is_action_pressed("ROTATE_RIGHT"):
	cargo_selected.x += 1  #instead of use of rotational_adder, hardcode the 1
	if cargo_selected.x > 3:
		cargo_selected.x = 0

This is just temporary for you to test, maybe this way it’ll be easier to get what’s the issue. If you happen to fix it, please add a comment here, I’m kinda curious

What’s the issue with having one tile and rotating and resizing it?

This video should show what I am experiencing. I press the button multiple times and the tile doesn’t always rotate.

I found out what the problem is. As it turns out, the input() method wasn’t being triggered. I updated the code accordingly and the tile does rotate now:

extends TileMapLayer

@onready var h_box_container: HBoxContainer = $"../HUD/Panel/HBoxContainer"

var mouse_position: Vector2i
var old_mouse_position: Vector2i

var x: int = 11
var y: int
var rotational_adder: int

func button_pressed():
	if h_box_container.btn.text == "1 X 1":
		x = 0
		y = 0
		rotational_adder = 1
	elif h_box_container.btn.text == "2 X 2":
		x = 0
		y = 1
		rotational_adder = 2
	elif h_box_container.btn.text == "3 X 3":
		x = 0
		y = 3
		rotational_adder = 3

func _input(event):
	if event.is_action_pressed("ROTATE_RIGHT"):
		x += rotational_adder
		if rotational_adder == 1 and x == 4:
			x = 0
		if rotational_adder == 2 and x == 8:
			x = 0
		if rotational_adder == 3 and x == 12:
			x = 0

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float):
	set_cell(mouse_position, 0, Vector2i(x, y))
	mouse_position = local_to_map(get_local_mouse_position())
	if old_mouse_position != mouse_position:
		erase_cell(old_mouse_position)
		old_mouse_position = mouse_position
1 Like