0 votes

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:
emitsignal(charasignal)

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
whensignal(charasignal)_emitted
or something that would run a script after the signal is broadcast.
Any and all help is appreciated! Thank you!

in Engine by (24 points)

3 Answers

0 votes

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. "nameoffunction" 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!

by (25 points)
0 votes

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").

by (728 points)
0 votes

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.
by (719 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.