Area2D on_input_event is not registering mouse clicks, only mouse motion

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

I have been trying to detect mouse clicks on a node. The node is an Area2D with a sprite and CollisionArea2D as children.
I have followed the answer from this question: https://forum.godotengine.org/22606/how-ive-handled-area2d-mouse-click-not-question, but I cannot get the input_event function to run on mouse clicks, only on mouse motion in and out of the collision area.
What should I do to correctly detect mouse clicks on the node?

My code:

extends Area2D

func _ready():
    # Called when the node is added to the scene for the first time.
    pass
func _on_book_input_event(viewport, event, shape_idx):
    print("Something happened! ", event)

The output:

** Debug Process Started **
OpenGL ES 3.0 Renderer: Intel(R) HD Graphics 620
Something happened! [InputEventMouseMotion:1117]
Something happened! [InputEventMouseMotion:1168]
Something happened! [InputEventMouseMotion:1225]
Something happened! [InputEventMouseMotion:1276]
Something happened! [InputEventMouseMotion:1387]
Something happened! [InputEventMouseMotion:1390]
Something happened! [InputEventMouseMotion:1393]
Something happened! [InputEventMouseMotion:1429]
** Debug Process Stopped **

Here I moved the mouse onto and off of the node a few times, and clicked on the node each time. Only the movement of the mouse triggered the function to run.

I know that I can use _on_mouse_entered and on_mouse_exited in combination with the general input function to do what I want, but I would prefer to get the simpler solution working, or at least understand why it is not.

Thank you!

:bust_in_silhouette: Reply From: jgodfrey

This works for me…

func _on_Area2D_input_event(viewport, event, shape_idx ):
	if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
		print("Clicked")

Though, that doesn’t explain why you don’t see some sporadic InputEventMouseButton messages in your debug output. Are you sure they’re not just lost in a sea of InputEventMouseMotion messages?

Are there any setting you had to apply to the area to make it work?
Mine is set to pickable.

If I use something like your code to detect clicks both inside the area and anywhere on the screen only the function to detect clicks anywhere picks anything up.
Code:

func _on_book_input_event(viewport, event, shape_idx ):
    print('Something happened!')
    if event is InputEventMouseButton && event.button_index == BUTTON_LEFT && event.pressed:
	    print("Click Detected in Area")

func _input(event):
    if event is InputEventMouseButton && event.button_index == BUTTON_LEFT && event.pressed:
	    print("Click Detected")

I get the following output:

** Debug Process Started **
OpenGL ES 3.0 Renderer: Intel(R) HD Graphics 620
Something happened!
Click Detected
Something happened!
Something happened!
Click Detected
** Debug Process Stopped **

The "Something happened!"s show that it recognizes when I move the mouse into the area, and the “Click Detected” that it recognizes the clicks using the general function, but it doesn’t recognize the clicks inside the area.

Thank you for your help!

woodml | 2020-05-06 01:53

I didn’t do anything special. I simply added an Area2D, with a CollisionShape2D and a Sprite as its only children. Then, I just connect the input_event signal of the Area2D to the posted script.

That’s all I did. Using Godot 3.2.1 stable, Win 10…

jgodfrey | 2020-05-06 02:09

Maybe try creating a new, simple scene as a test (if you haven’t already)?

jgodfrey | 2020-05-06 02:11