How I've handled - Area2d mouse click (not question)

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

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 mouse_entered() and mouse_exited() to the script.

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

3- On the mouse_entered() function set “mouse_over” to true.
On the mouse_exited() function set “mouse_over” to false.

4- Then on the _input function only check if mouse_over is true, if the event.is_pressed and if the button_index 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

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

TheSecurityDev | 2019-12-03 00:51

:bust_in_silhouette: Reply From: literalcitrus

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

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.

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

I’m using Godot 3 rc2.

SARDON | 2018-01-23 06:17

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

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

literalcitrus | 2018-01-23 06:41

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

SARDON | 2018-01-23 12:04

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

Yes! That is the solution. Thank you :slight_smile:

brylie | 2018-03-28 20:22

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

TheGodotCoder | 2021-02-13 01:07

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 “input_event” signals (programatically if you have too many to do by hand, or if you generate them on the fly), and then each input_event 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.

epw | 2021-02-20 07:54

Just commenting to say that the other solution does have a good use case. input events are triggered last. even after _unhandled_input. so if you want to mark input as handled on a click event before, say, your player character starts moving, using the event as you have done will not help.

jimjamjahaa | 2023-03-09 11:38