How do I detect when a mouse button is released?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Fupicat

I’m creating a drag-and-drop script for 2d sprites. This is the code I’m using to detect left mouse button presses:

func _input(event):
    if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:

The code above detects if the mouse button was either pressed OR released, which is really annoying. I want to add a timer that the player has to hold the button pressed before the sprite actually begins dragging, but I haven’t found a solution. Having a way to detect if the button has been released would be way easier.

Here’s my entire code:

extends Sprite

var mouse_on = false #Mouse over sprite
var dragging = false #Able to drag

func _process(delta):
    print(str($"Hold Timer".time_left))
    if dragging: #If the sprite is allowed to drag
        position = get_global_mouse_position() #Set the sprite's position to the mouses position

func _input(event):
    if event is InputEventMouseButton and event.button_index == BUTTON_LEFT: #This script detects if the left mouse button was either pressed or released
    if !dragging and mouse_on: #If dragging isn't allowed and the mouse is over the sprite
        dragging = true #Allow dragging
    elif dragging: #Else if the sprite is already dragging
        dragging = false #Stop dragging

func _on_Area2D_mouse_entered():
    mouse_on = true


func _on_Area2D_mouse_exited():
    mouse_on = false
:bust_in_silhouette: Reply From: Fupicat

Ok, I’m dumb. All I needed to do was add a left mouse button action to the input map and use is_action_released().

Idk what I was thinking.

Thank you for your dumbness, I was having the same problem

Nairazak | 2020-05-26 03:49