0 votes

I have a singleton that contains the fuel data for my spaceship.I don't know how to change singleton's value from the player script

Extends node
var fuel = 1000

In my test character i try to edit it like that
var fuel = Global.fuel
func process(delta):
if Input.is
Global,fuel -= 10

But it doesn't seem to work

in Engine by (20 points)

1 Answer

+1 vote
Best answer

You're first changing the value of the fuel-variable in the singleton. And then print the value of the localfuel-variable in your character script. The latter is a simple copy of the Singleton's value, not a reference! So it won't change along with the other.

var fuel = Global.fuel
func _process(delta):
    if Input.is_action_just_pressed("button"):
    Global.fuel -= 10
    print("Global: ", Global.fuel)
    print("Local: ", self.fuel)  # explicitly local
    print("Local: ", fuel)         # implicitly local

Also if you provide any code, please make sure, it's formatted properly and doesn't contain any typos (like the comma instead of a dot in Global,fuel). That will make it a lot easier for others to read through your code and help you faster.

by (10,523 points)
selected 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 Frequently asked questions and 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 [email protected] with your username.