0 votes

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.

[img]https://i.ibb.co/vwZXtg6/MyIssue.png[/img]

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.

Godot version Version 3.4 stable
in Engine by (17 points)

2 Answers

+1 vote
Best answer

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

by (5,651 points)
selected by
null(added comment)

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?

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.moveandcollide() # 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

0 votes

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.

by (87 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.