Reputation:
I am a beginner at Godot game development. I am having some difficulties in understanding what a signal in Godot is. I have looked at the official documentation, but could not understand well. However, I am getting a feeling that these signals are some kind of event handler.
Please, help me to understand what a signal in godot is and whether my understanding of signals as event handlers is correct or not?
Upvotes: 3
Views: 1208
Reputation: 8031
You are correct, a signal
is an event that you can emit from a Node
.
emit_signal("signalName") #emit's signal with no data
emit_signal("signalName", whateverDataToSend) #emit's signal with data
You can then register to receive notifications when the signal is fired, hence connect
to a signal.
nodeWithSignalInIt.connect( String signalName, Object target, String methodToCall)
Note: connect
needs to be called from a node that has the signal inside of it
It's also worth noting that signals can be found all over Nodes we use in Godot, for example, if you want to get notified of a collision entry on Area2D
then just connect to that Area2D's area_entered
signal.
Example:
func _ready() -> void:
#NOTE: InteractiveArea is of type Area2D
$InteractiveArea.connect("mouse_entered", self, "_on_mouse_entered")
$InteractiveArea.connect("mouse_exited", self, "_on_mouse_exited")
$InteractiveArea.connect("area_entered", self, "_on_player_entered")
$InteractiveArea.connect("area_exited", self, "_on_player_exited")
func _on_mouse_entered() -> void:
mouse_entered = true
func _on_mouse_exited() -> void:
mouse_entered = false
func _on_player_entered(area: Area2D) -> void:
print ("Player entered chest opening zone.")
interaction_zone = true;
func _on_player_exited(area: Area2D) -> void:
print ("Player exited chest opening zone.")
interaction_zone = false
Upvotes: 7