Godot emit custom signal with arguments 3/10/2023

preview_player
Показать описание
I'm not sure why, but everything I tried before this either didn't work or was too complicated, or both.
Рекомендации по теме
Комментарии
Автор

The exact answer I'm looking for in under 20 seconds 💯

kruth
Автор

I love this, direct to the point and very useful

dakotamogi
Автор

Q: What are the functions of the scripts in the video?
A: Press E to transmit the emitter's signal to the receiver, along with the emitter's name and position.



Explanation :

On the emitter :
1.create custom signal with arguments you want (in this case name and position) → signal on_interact(node_name, node_position)
2.emit signal with arguments above → on_interact.emit(self.name, self.transform.origin)

On the receiver :
1.Get reference of the emitter (in this case via @export aka public GameObject) → @export var emitter:Node3D
2.Connect signal on ready (or whenever you want) →
3.Create function to call when received signal → func _on_interact(node_name:String, node_position:Vector3):



Emitter script:

#Emitter
extends Node3D

#Emit the signal along with node name and node position
signal on_interact(node_name, node_position)

func _input(_InputEvent) -> void:

#Check if we press E
if Input.is_key_pressed(KEY_E):

#Then emit the signal
on_interact.emit(self.name, self.transform.origin)



Receiver script:
#Receiver
extends Node3D

#Public GameObject equivalent
@export var emitter:Node3D

#Connect to signal on ready (Start equivalent).
func _ready() -> void:


#The parameters must be the same type as the one sent by the emitter.
func _on_interact(node_name:String, node_position:Vector3):
print("Node name is ", node_name, " and Node position is ", node_position)

arigatechgozaimucho
Автор

I want to know how to activate a function from one scene in another scene. I have also seen that there are only videos about variables, but not how to activate a function.

truenincillo
Автор

bro please make tutorial for 2d with more effort (in same topic)

inxeoz