0 votes

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://godotengine.org/qa/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 onmouseentered and onmouse_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!

asked May 6 in Engine by woodml (12 points)

1 Answer

0 votes

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?

answered May 6 by jgodfrey (5,574 points)
edited May 6 by jgodfrey

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!

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...

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.