My problem is this: I am trying to detect a click on a 2D object. Basically, make a unit selected with a click.

Now. I am pretty sure this problem had already arisen a zillion times before. And I have even seen some answers concerning Godot 3. But I can't find anything related close enough. If you know the answer just give me a link pls

I followed the best advice I could find (which was about godot 3.5) and did this:

I created Area2D with CollisionShape2D
I attached a script to Area2D and added this code into _input() function (which seemingly replaced _input_event).

func _input(event: InputEvent) -> void:
	if Input.is_action_just_released("mouseLeft"):
		doSomething()

But doSomething() always happens, wherever I click. And I do not understand how to limit InputEvent to the boundaries of CollisionShape2D. It seems I don't understand how exactly this input works.

Could you tell me please what I am doing wrong? Or direct me where I could read about it. I can't understand it from Godot help. Thank you.

    xyz Ah thank you very much! I totally forgot about in-built signals!

    So let me clarify the solution for the future dummies like me:

    1. connect signal input_event() of the Area2d to a function via Node properties
    2. add a condition to check that it was the right kind of input, for example:
    func _on_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
    	if Input.is_action_just_released("mouseLeft"):
    		doSomething()

    I think I should update this further by adding another solution, which seems better for RTS-style item click selection.

    In scene root:

    signal primary_action
    
    func _process(delta: float) -> void:
    	if Input.is_action_just_released("mouseLeft"):
    		emit_signal("primary_action")

    signal connected to a function select_unit() in the selectable item:

    clickArea = $ClickArea.shape.get_rect() # get Rect2 from child collision shape
    
    func select_unit():
    	var endPoint = $ClickArea.get_local_mouse_position()
    	if clickArea.has_point(endPoint):
    		doSomething()
    	else: 
    		doTheOpposite()

    Item you click gets selected, all other items deselected