|
|
|
 |
Reply From: |
literalcitrus |
Not to burst your bubble, but there is a more efficient way of doing this 
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 
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.
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