Check to see if mouse button is down when it enters an object

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JymWythawhy
:warning: Old Version Published before Godot 3 was released.

I have the following code, but it isn’t working like I would expect.

func _on_Tile_mouse_enter():
    if Input.is_mouse_button_pressed(BUTTON_LEFT):
        print("Left mouse dragged over Tile: ", gridPosition)
    elif Input.is_mouse_button_pressed(BUTTON_RIGHT):
        print("Right mouse dragged over Tile: ", gridPosition)

This is on a Control node with a sprite child node. I would like the tile to do some action if I drag a clicked mouse over the control node, but the above code is not working. If I put a print command outside of the checks for mouse buttons being pressed, that fires every time the mouse enters the tiles, but these ones inside don’t fire, usually. If I attempt to click and drag many times in a row, occasionally one of my attempts will work, and every tile I mouse over will fire the message. I can’t figure out why it works sometimes but not others.

I have different code that fires on an actual mouse click on the tile, and that works everytime. It’s just this code that isn’t working.

Is there a different way I should be doing this?

is your problem that “these ones inside don’t fire”?
then see this answer.
Input event not propagating past Control? - Archive - Godot Forum

volzhs | 2016-11-18 05:57

:bust_in_silhouette: Reply From: avencherus

One method is keeping track of the state with a flag, and using the input events. Something like this inside a control with a a mouse enter signal (The extra branching is just for making it easier to read, you can just put event.pressed right into the flag):

func _ready():
	set_process_input(true)
	
var dragging = false
	
func _input(event):
	if  (event.type == InputEvent.MOUSE_BUTTON):
		if  (event.button_index == BUTTON_LEFT and event.pressed):     dragging = true
		elif(event.button_index == BUTTON_LEFT and not event.pressed): dragging = false

func _on_Control_mouse_enter():
	if(dragging): print("entered while mouse down")

Another version if you want to deal with either right or left dragging might look like this:

func _ready():
	set_process_input(true)

var left_dragging = false
var right_dragging = false

func _input(event):
	if(event.type == InputEvent.MOUSE_BUTTON):
		if(event.button_index == BUTTON_LEFT):
			left_dragging = event.pressed
		if(event.button_index == BUTTON_RIGHT):
			right_dragging = event.pressed

func _on_Control_mouse_enter():
	if(left_dragging): print("entered while left mouse button down")
	if(right_dragging): print("entered while right mouse button down.")

That makes sense, and is what I am trying to do- I just thought Godot already had a way to check if the mouse was pressed without my having to cache that info somewhere. I will just handle it myself, like you suggest. Thanks!

JymWythawhy | 2016-11-18 14:04

You’re welcome.

I don’t think Godot has built-in events for things like dragging or the timing of input events.

There was this discussion at one time about just_pressed functionality: built in input state tracking · Issue #1766 · godotengine/godot · GitHub

avencherus | 2016-11-18 17:15

It looks like Input.is_mouse_button_pressed(button ID) works for what I was looking for. The problem was coming from some other issue, but Godot apparently does have a method to check the pressed condition of mouse buttons. Neat!

JymWythawhy | 2016-11-21 17:27