Editing singletons from the player/button script

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

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_action_just_pressed(“button”):
Global,fuel -= 10
print(fuel)

But it doesn’t seem to work

:bust_in_silhouette: Reply From: njamster

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.