Updating variable via signal argument

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

Hello, I am trying to use custom signals to pass variable speed from one scene into a singleton and then have player scene use this singleton for the speed.

Currently I have in CharacterSheet.gd:

signal speed_signal(speed)

connect(“speed_signal”, get_node(“/root/DataImport”), “Player_Stats”)

func _process(delta):
emit_signal(“speed_signal”, speed)

DataImport.gd (singleton)
var speed = 300

func Player_Stats(speed):

So the thing I am trying to figure out is print(speed) returns the correct speed values from the CharacterSheet.gd, but var speed stays at 300 when it gets grabbed by the Player scene. And if I dont assign a default value for the var speed it just grabs nil/0/error and not the value I have under func Played_Stats(speed)

Any help appreciated.

:bust_in_silhouette: Reply From: jgodfrey

The speed reference you have in your funct Player_Stats(speed) is a local variable. That is, it’s not the same variable you created at the top of your DataImport.gd script. Because of that, your not actually setting the singleton’s speed var in that function.

Try this instead:

func Player_Stats(new_speed):
    speed = new_speed

Additionally, if the var you’re trying to update is stored in a singleton, why not just update it directly (from wherever) rather than updating it via a signal? Maybe you have a valid reason, but it’s something to think about…

jgodfrey | 2020-09-08 20:35

It works, thank you for you help.

I am using signals because I try to control the speed from outside the main tree through instanced scene, but this is my first Godot project so I am sure there are many ways to do things more efficient.

AltoWaltz | 2020-09-09 07:25