How to link the same string variable count to all scripts on different nodes/scenes?

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

Hi all. I am trying to link the real-time score variable count to all scenes/nodes but putting the variable in the global script alone doesn’t seem to work.

In the global script:

var score = 0

In the menu/score-keeping/leaderboard script (this works fine):

func _on_Player_scored():
global.score += 1

In main scene script: Spawn mobs when the player is still below or above a certain score. This does not work as the global.score on the main script stays 0 even when the player is scoring.

Vice versa. I tried the opposite way; connect the player_scored signal to the main script and its mob-spawning function works but the score count label on the menu script stays 0 instead.

The only way I can get the score to count on both sides is to connect the Player_scored signal on both scripts but I’m sure there’s a better way to do this.

Appreciate it if anyone could advise. Thanks!

Have you added the script as a Singleton and enabled Global Variable?
Project Settings > AutoLoad

godscock | 2022-04-29 23:11

Yes that has been done.
Is it not working because one script’s node is the child of another?

tehgaming | 2022-04-30 04:31

Sorry for the late reply, did you try incrementing the global score variable on process then going to the Remote part in Scene tab while the game is running and checking if singleton’s score variable is getting incremented?
It should be the first node after root.

godscock | 2022-05-13 18:36