How do I receive custom signals...?

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

This might seem like a dumb beginner question because it kind of is…
But say I made a custom signal for Godot to send to other scenes, I have loaded the scene into the script I want it to change, and messed with some variables:

example:
var chara = load.chara.tcsn
if chara.var == 1:
#and this has worked out for me so far

But I can’t quite figure out how to receive a signal on another script, or even on the same script.

example:
signal chara_signal

if var == 1:
emit_signal(chara_signal)

My problem is I can’t figure out what script to use to make something happen after the signal has been emitted. So I guess my question is, would there be some sort of
when_signal(chara_signal)_emitted
or something that would run a script after the signal is broadcast.
Any and all help is appreciated! Thank you!

:bust_in_silhouette: Reply From: Lola

In godot signals are connected to functions, that is to say that when a signal is emited (using emit_signal) the list of connected objects calls their connected methods.
This connection can be done either via the editor interface or via code and is described in the docs.

To connect a signal from another node from code you can use the connect method:
emitter.connect("signal_name", receiver, "method_to_call").
For example in an hypotetical MainMenu.gd script:
$PlayButton.connect("pressed", self, "_start_game")
or in your case:
my_node.connect("chara_signal", some_node, "_on_MyNode_chara_signal").

:bust_in_silhouette: Reply From: arbo

Hello! So there are two options (or at least two that I’ve used, if anyone has other methods, please add on).

  1. Connect the signal to a receiving node via a script. This would be something like get_node("emitting_node").connect("name_of_signal", self, "name_of_function") in the _ready() function of the receiving node. Presumably, this could also be done the other way around where you connect from the sending node but I’ve always done it from the receiver. “name_of_function” is simply the function that is called when the connected signal is emitted.

  2. When you declare signal chara_signal this signal appears under the “Node” tab (next to the “Inspector” tab). From there, you can connect the signal to any node with a script, just like any other signal (by right-clicking and selecting “Connect”). When you connect a signal from this tab, it will automatically create a function in the receiver’s script that will run when the signal is emitted.

If you’re unfamiliar with connecting scripts to signals/signals in general, I suggest reading through the Godot documentation .

I hope I was of some help, signals are a really powerful tool once you get a hang of them!

:bust_in_silhouette: Reply From: wyattb

In the subscriber to the signal you need to do the following as in this example:

extends KinematicBody2D

onready var speed_control: Control=$"../CanvasLayer/SpeedControl/Slider"

func _ready():
    _err=speed_control.connect("speed_changed",self,"speed_changed")

func speed_changed(new_speed):
    print(new_speed)
    pass

And in the publisher of the signal.

extends VSlider

signal speed_changed

func _on_SpeedControl_value_changed(value: float) -> void:
	print(value)
	$Speed.text=str(value)
	emit_signal('speed_changed',value)
	pass # Replace with function body.
:bust_in_silhouette: Reply From: DoanYılmaz

Signals are easy to use:

  1. First define the signal in the emitter node’s script (example: signal custom_signal)

  2. Write the code to emit signal in the emitter node’s script (example: emit_signal("custom_signal")) Now, you’re done with the node that
    emits signal.

  3. To connect this signal to any node; in the receiver node’s script, you should write this:emitter_node.connect("custom_signal", receiver_node, "any_function_in_the_receiver")

I created a scene for better understanding.
enter image description here

Script for EMITTER:

extends Control


signal custom_signal

func _ready():
	emit_signal("custom_signal")

Script for RECEIVER:

extends Control


func _ready():
	var EMITTER = get_parent().get_node("EMITTER")
	EMITTER.connect("custom_signal", self, "signal_function")


func signal_function():
	print("Signal test")