My Godot Global variable is not updating

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

Hi there, thanks for checking out my question.

In my game I am using a global script. It currently has 2 variables.
1st var Keeps track of the player’s healthpoints. (This works correctly)
2nd var uses bool to keep track of whether or not the player is facing right.

My player’s direction variable(inside the player script) updates correctly when I press left or right (while not pressing melee).
But the global Facingright variable’s true/false state does not change.
As the console keeps printing that the player is facing right. Never left.

I’m just confused as I can’t find the reason why the global script won’t update.

:bust_in_silhouette: Reply From: Inces

mistake of reference. You created var gfacingright and assigned it a value of Global.Facingright. But they are two different variables. Changing gfacingright does not change Global.facingright. You have to reference directly Global.facingright

Thank you so much. For some reason I was not understanding how to reference it properly. I think I understand now.

For simple example, my coins.

I had my Coins amount in GameManager(Which is set as my only global script).
Within GameManager I had var Coins = 0

Separately, in my actual Coin script, I had
var gCoins = GameManager.Coins

When a body enters the area2d collision. The coin script plays an animation, followed by the next line which is gCoins = gCoins + 1
But the amount in the GameManager did not update.

To correctly reference the Coins variable within my GameManager script from the coin script…That means I have to refer to it directly as GameManager.Coins rather than trying to shortcut by assigning a variable such as gCoins?

TheGodotMan | 2022-04-29 07:33

To correctly reference the Coins variable within my GameManager script from the coin script…That means I have to refer to it directly as GameManager.Coins rather than trying to shortcut by assigning a variable such as gCoins?

exactly. Shortcuts like these only work for referencing nodes, arrays, dictionaries, and some miscellanous types. So :

var a = get_node("something")
var b = a

b.move_and_collide() # will work as shortcut, it will affect the real “something” node

var a = {"apple" : 2, "banana" : 4 }
var b = a

b[“pineapple”] = 4 # this works as a shortcut too, it also will affect real dictionary var a

var a = 9
var b = a

b += 10 # this will not work as shortcut, var a will stay 9 and var b will become 19

Editor help often proveides information, whether type is passed as a value or just a reference. Being passed as a reference means it works as a shortcut

Inces | 2022-04-29 12:55

:bust_in_silhouette: Reply From: Fenisto

Basically when you type var gFacingright = GameManager.Facingright you assign a copy of GameManager.Facingright value to a new variable gFacingright. So now you have 2 variables with 2 values.

You don’t need gFacingright. In your input instead of gFacingright = true it should be GameManager.Facingright = true.