Hi everyone. I'd like to apologize in advance for the poor wording of the title since I really have no clues how to put this properly with as few words as a title requires.
I am working on a 2D project, and I would like some help on making a system that allows me to move items/objects by first clicking on them, then clicking somewhere else, and have the item be moved to that somewhere else place. A bit like on lichess, for instance, where if you click on a piece once and immediately release the mouse button, you then get to click again on the square where you want to move the piece. There are plenty of tutorials on the internet about making drag and drop systems, but I haven't found any that helps with what I'm trying to do so far.
I have messed around a little trying to accomplish this idea and the closest I've come to actually having it working properly is this:
extends Node2D
var clicked = false
func _process(delta: float) -> void:
if clicked == true:
yield(get_tree().create_timer(0.1), "timeout")
if Input.is_action_just_released("left_click"):
global_position = get_viewport().get_mouse_position()
clicked = false
func _on_Area2D_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed:
clicked = true
This sort of works, but since it uses a timer that has to be calibrated with the right value, I sense it's probably breakable. I would like to know if anyone here would proceed differently, and if there's a better way than mine to implement this idea.
Thanks a lot in advance!