+7 votes

After countless (insert time) searching how to handle an mouse click on an Area2d node, I've came up up with something that works just fine and it's very easy to implement.

1- On the Area2d node connect the signals mouseentered() and mouseexited() to the script.

2- Create an "mouse_over" variable and set it to false.

3- On the mouseentered() function set "mouseover" to true.
On the mouseexited() function set "mouseover" to false.

4- Then on the input function only check if mouseover is true, if the event.ispressed and if the buttonindex is the "BUTTON_LEFT". (left button of the mouse in my case, feel free to change it).

5- Be happy.

PS: "castle" is the name of my node. Godot 3 rc2

enter image description here

in Engine by (36 points)
edited by

This is exactly the way I've been doing it also.

1 Answer

+9 votes
Best answer

Not to burst your bubble, but there is a more efficient way of doing this :P

You can use the input_event signal on the Area2D, and then in the connected function use:

func _on_Area2D_input_event( viewport, event, shape_idx ):
    if (event.type == InputEvent.MOUSE_BUTTON && event.pressed):
        print("Clicked")

To check for whether the input event was a click.

by (524 points)
selected by

If i use your solution, i get an "Invalid get index 'type' (on base: 'InputEventMouseMotion') "

I'm using Godot 3 rc2.

Ah right, for Godot 3 it is slightly different. The if statement changes to:

if (event is InputEventMouseButton && event.pressed):

Thanks.

func onArea2Dinputevent( viewport, event, shapeidx ):
    if (event.type == InputEvent.MOUSEBUTTON && event.pressed and event.button_index == BUTTONLEFT):
    print("Clicked")

added the left mouse button so it only triggers when using left mouse button

if (event is InputEventMouseButton && event.pressed):

Yes! That is the solution. Thank you :-)

How do you know which object is clicked?
Can you please answer? I can't find an answer to the question for days.

As best as I can understand it, this means that the Area2D was clicked.

So, it should be whatever object is having the function be called.

If you have a group of objects, all children of a larger Area2D, you could make them all be of type Area2D, and connect their "inputevent" signals (programatically if you have too many to do by hand, or if you generate them on the fly), and then each inputevent function can just assume that it is the object being clicked.

I'm not sure what to do if you have overlapping objects, if that's your problem.

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.