Area2D's CollisionShape2D region not detecting mouse clicks

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Godont

Hello.

I’m new to Godot (just downloaded the software today and followed some beginner tutorials).

After working through everything up to the SceneTree section I thought I’d try to create something simple on my own as a test of what I’d learned so far. I thought Tic-Tac-Toe should be simple enough, but I’ve hit an issue pretty quickly: I cannot seem to get an Area2D to detect mouse clicks.

Scene Tree:
Board
A TextureRect whose Texture is an image of a 3x3 grid, with ample white
space padding around the edges. This is the root node.

ClickableArea
An Area2D with Pickable set to On and one Collision Layer set. This is a child of Board

CollisionShape2D
This is a child of ClickableArea with a RectangleShape2D shape and has its Extents set so that it covers just the grid portion of Board (i.e. not the bounding whitespace).

Message
A Label which is a child of Board and is just used to display messages onscreen when certain events occur.

Signals:
On ClickableArea2D under Node → CollisionObject2D I’ve connected the input_event to itself, generating the method _on_ClickableArea_input_event which has the following definition (code updated based on answer from user kidscancode):

func _on_ClickableArea_input_event(viewport, event, shape_idx):
    get_node("../Message").text = "The message is not updated with this text"
	if event is InputEventMouseButton \
	and event.button_index == BUTTON_LEFT \
	and event.pressed:
		get_node("../Message").text = "CollisionShape2D Clicked"

As mentioned, clicks on the area defined by the Area2D’s CollisionShape2D’s extents do not register (even without checking that the event is a mouse button, that it is the left mouse button, or that the button is pressed).

Changing to a mouse entry event doesn’t work, and I can detect mouse clicks on the root TextureRect, so I believe the problem may be with the the area itself but I’m not sure.

Any help would really be appreciated.

Thanks!

:bust_in_silhouette: Reply From: kidscancode

Your code for testing input is incorrect. In GDScript you check type with is, and there’s no such thing as InputEvent.MOUSE_BUTTON.

func _on_ClickableArea_input_event(viewport, event, shape_idx):
    if event is InputEventMouseButton \
    and event.button_index == BUTTON_LEFT \
    and event.pressed:
        get_node("../Message").text = "CollisionShape2D Clicked"

See here for more examples on how to properly process inputs:

Oh sorry about that; it looks like I’m using old syntax which I copy pasted from an answer on the web somewhere. InputEvent.MOUSE_BUTTON was used in previous versions of Godot (see the answer to this question, and its comments), but I’ve changed the code to use the syntax you specified here and am still experiencing the same issue.

In fact, even removing the if condition within _on_ClickableArea_input_event and just changing the Message node upon entering the function still produces no result, meaning _on_ClickableArea_input_event isn’t even getting called.

I’ll update the question with the more up-to-date syntax.

Godont | 2020-04-06 23:03

If the function isn’t getting called, it’s likely one of the following:

  1. The _input_event signal is not connected. Do you see the green icon next to the function definition indicating the connection?

  2. Something else is consuming the mouse event before it reaches the area. Do you have any Control/GUI nodes in the scene? If so, you may need to set the Mouse/Filter to “Ignore”.

Also, is another collision object (one higher in the tree) overlapping this one?

  1. “Pickable” is disabled on the area.

kidscancode | 2020-04-07 00:15

Hello. Yeah, the icon next to the connection is green. And the “Pickable” property of ClickableArea is also set to On.

I don’t have any other nodes in the scene than the ones listed, so no control nodes. But I just checked and TextureRect did have Mouse/Filter set to “Pass”, so I set it to “Ignore” and that worked!

Thanks very much for helping me with this, and sorry if the question was foolish. I have to read up on the Mouse/Filter property.

Thanks again!

Godont | 2020-04-07 02:25

TextureRect is a Control node. All the UI nodes (green icons) inherit from Control.

kidscancode | 2020-04-07 04:02