How to I emit a mouse_entered signal in code?

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

I have a bunch of area2d nodes with collision shapes. And I want for them to emit a signal when they are entered. How do I do that with code?
I understand how to connect a signal or how to pass data by a custom signal. But how do I emit build_in signals?

:bust_in_silhouette: Reply From: timothybrentwood

For built in signals the signal is automatically emitted when the event occurs and the function that you assigned to be the callback via connect() (or in editor) will be called. If you want to emit your own custom signal when a mouse enters your Area2D simply emit it in the callback:

extends Area2D

signal my_custom_signal

func _ready() -> void:
	self.connect("mouse_entered", self, "_on_Area2D_mouse_entered")

func _on_Area2D_mouse_entered() -> void:
    print("I'm automatically called when a mouse enters my Area2D.")
	emit_signal("my_custom_signal")

Thanks. That is what I assumed at first. But it turned out my debug console wasn’t working properly because I changed its remote port number. So I couldn’t see that what I tried actually worked…

Fenisto | 2021-12-06 20:02

1 Like