How do i Interact with Nodes connected to a CanvasLayer

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

I wan’t to use an Area2D on the UI of my game but i don’t now how to interact with something on the canvas layer

What do you mean by “interact”? Do you have trouble getting the node? If so, how does your scene tree look like and which node holds the script in which you’re trying to get the node?

njamster | 2020-08-01 17:55

Quick example:

if i make a custom mouse with a sprite on and then hover it
over an Area2D(with body enterred signal)connected
to the canvaslayer. The Area2D will actually not detect it

Superkrypet | 2020-08-05 06:30

Basicly the area 2D node does not work

Superkrypet | 2020-08-05 06:31

if i make a custom mouse with a sprite on

Ambiguous! Do you mean you added a Sprite-node to the scene and let it follow the mouse cursor? Or are you talking about actually changing the mouse cursor?

hover it over an Area2D(with body enterred signal)connected to the canvaslayer.

You keep mentioning the term “CanvasLayer”. Are you talking about the default CanvasLayer which forms the root-node of the scene-tree or have you actually added additional ones to the tree? Also: How is that relevant here?

The Area2D will actually not detect it

Why should it? A Sprite-node is not a body, thus no body is entering the area. The body_entered-signal will only trigger on a Kinematic-, Static- or RigidBody.

I still don’t get what’s your problem and where you need help…

njamster | 2020-08-05 21:05

you just helped me without helping me

the reason it did not work is because i used a sprite and body entered does not detect that. You just reminded me and now i know i have to change it to a different kind of node or another sort of code

Also i come from game maker where you usally have problems detecting the mouse on a UI layer.
thats the only reason i actually tried to attach a sprite to the mouse cursor as a workaround.

So if you still want to help me then how do i detect a mouse with an Area2D whitout attaching something to it because if i dont mistake the cursor is not a body

Superkrypet | 2020-08-06 18:58

:bust_in_silhouette: Reply From: Superkrypet

Quick example:

if i make a custom mouse with a sprite on and then hover it
over an Area2D(with body enterred signal)connected
to the canvaslayer. The Area2D will actually not detect it

:bust_in_silhouette: Reply From: njamster

how do i detect a mouse with an Area2D

Connect its mouse_entered- and mouse_exited-signals:

func _on_Area2D_mouse_entered():
	print("Mouse entered the Area2D")

func _on_Area2D_mouse_exited():
	print("Mouse exited the Area2D")

Note that Control-nodes provide those signals as well, so for UI you don’t have to use an Area2D (which requires a CollisionShape2D to function properly).