How do I send a variable from one script to another via a signal?

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

I have a main node script and a child node script.
And I want to generate a value on mouse movement, store it in a variable Type and send this variable to a child node where it will be used as a reference to a dictionary.

On the main node I have this:

var Type
signal Send_signal
func _ready():
	connect("Send_signal", get_node("child"), "Set_type", [Type])
func _on_Area_mouse_entered(): 
	Type = *Generated by a function value*
	emit_signal("Send_signal")

And on the child node I have

func Set_type(Type):
    Class = Type["name"]
var *Generated by a function value* = {name = "bob"}

Clearly a missed something in the process of understanding of how to create a custom signal. Can you tell me what?

the syntax for connect is connect("signal_name", signal_target_node, "signal_function") Then emit should go like emit_signal("signal_name", [argument]). Inside those brackets put the arguments of the function. Then in signal_target_node, for you it’d be your child node, put func signal_function(argument1): The parameters to your signal function should be in emit_signal not connect

Michael Antkiewicz | 2020-01-19 03:32

emit_signal("signal_name", [argument])

It doesn’t looks like you need brackets there. Or it will send it just like that, as “[argument]”
But thank you anyway.

Fenisto | 2020-01-19 10:36

:bust_in_silhouette: Reply From: Zylann

There are two ways to send values through signals: arguments, and bindings.

  1. Arguments are values sent at the moment the signal is emitted, pretty much like function calls.

  2. Bindings are values coming from the moment you connected the signal (and won’t change after). Those are rarely needed, but sometimes they come in handy.

At the moment the signal is emitted, they are both put together: arguments first, bindings next, and are sent to the handling functions.

It looks like you need the first, but you used the second instead, which means the signal will always emit with the value Type had at the moment you connected the signal. So it’s always null.
To make the signal emit with the current value of Type, declare your signal like this:

signal Send_signal(type)

And emit it like this:

emit_signal("Send_signal", Type)

I would have linked the doc: Signals — Godot Engine (3.1) documentation in English
Unfortunately, it seems signal parameters are not explained.

Thank you. Now it works.

Unfortunately, it seems signal parameters are not explained.

Indeed. That is what led me to googling other people’s explanations. But your is as clear and simple as it should be.

Fenisto | 2020-01-19 10:31