How to send a signal with a variable?

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

Hi godot-ers, thanks for clicking. I am new to the engine coming from Roblox Lua coding, and a thing you can do in there is send a signal with a variable (i.e if a drop-down with a selected value, you could send a signal with that value attached to be received by the other scripts). Is there a way to do this type of thing in Godot with emit_signal()? Like I mentioned I am extremely new to the editor and would like to get help. For reference I am making a stamina bar where the player node has the script for the stamina value, I want to send that value to another script in a canvas layer to change the value of a progress bar onscreen.

Thanks so much for the help, I’ll be checking back every day and likely asking questions to the posters.

:bust_in_silhouette: Reply From: godot_dev_

Here is a webpage on signal emission in Godot.

If signals haven’t changed from Godot 3 to Godot 4, then it’s as simple as emit_signal("your_signal_name",arg1,arg2), where arg1, and arg2 are example variable that you are providing with the signal. For example, emit_signal("signal_name",52,"cat") would send 52 and "cat" to any function that is connected to "signal_name".

You can connect a function to the signal as follows:

yournode.connect("your_signal_name",self,"your_func")

Where your_func is the name of the function implemented in the script of yournode. So for example, with your_func implemented as follows:

func your_func(arg1,arg2)
    print("arg1: "+str(arg1))
    print("arg2: "+str(arg2))

Thenemit_signal("your_signal_name","Hello","World!) would print
arg1: Hello
arg2: World!

Thank you so much! I was hoping it was like this, as it’s very similar to Lua.

RBbooms | 2023-04-14 21:19