can't access singleton variable

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

Hello everybody,

I have this tree:

  • main
    –|-player (player.gd) → RigidBody2D
    –|-CanvasLayer
    ----|gui (gui.gd) → Node2D

player.gd is set to AutoLoad in Project settings so that I can access player’s variables everywhere (I just need to read, not modify) as described here.
When I try to access player.linear_velocity from gui.gd, something isn’t right : the x value is reduced to a float like 0.000001 where the y value is correct.
I placed two print() in each script to show you the issue.

gui.gd

extends Node2D

func _ready():
	pass

func _process(_delta):
	print("in gui script: ", player.linear_velocity)

player.gd

extends RigidBody2D

var thrust = Vector2(0, 50)
var torque = 1000

func _ready():
	pass

func _process(_delta):
	print("in player script: ", linear_velocity)

func _integrate_forces(_state):
	if Input.is_action_pressed("ui_up"):
		applied_force = -thrust.rotated(rotation)
	else:
		applied_force = Vector2()
	var rotation_dir = 0
	if Input.is_action_pressed("ui_right"):
		rotation_dir += 1
	if Input.is_action_pressed("ui_left"):
		rotation_dir -= 1
	applied_torque = rotation_dir * torque

Here is the Output when player is moving:

in player script: (0.000001, -14.953201)
in player script: (-6.814495, -13.309379)
in gui script: (0.000001, -14.953201)
in player script: (0.000001, -14.928279)
in player script: (-6.803138, -13.287196)
in gui script: (0.000001, -14.928279)
in player script: (0.000001, -14.903399)
in player script: (-6.791799, -13.265051)

I don’t know why there is two value printed for the player.
Also I know that I can access my player variable from his path, and it work, but it’s a pain to modify it every time I move the object in the scene.
I hope that you can help me with this issue.

Thanks for your time

:bust_in_silhouette: Reply From: njamster

player.gd is set to AutoLoad in Project settings so that I can access player’s variables everywhere

Changing a property in the player node (the one inside your main-scene) will not change that property in the Singleton based on the same script! They are independent instances in different places that merely share the same script.

Save the path to your player-node in the Singleton instead:

global.gd (add this as an AutoLoad named “Global”)

extends Node

var player_node = null

player.gd

extends RigidBody2D

func _ready():
    Global.player_node = self

gui.gd

extends Node

func _process(_delta):
    if Global.player_node: # make sure it's not null
        print("in gui script: ", Global.player_node.linear_velocity)

It work perfectly thank you very much !

leo-pnt | 2020-04-18 19:18

I have the same problem now.

if Global.player_node: # make sure it’s not null

What do I do if I do get “null” here?
I checkes the grammer and names of everything.

drorya | 2020-05-28 09:07

If Global.player_node is null then there either is no node called Global (add it as an AutoLoad in the Project Settings!) or there is no variable called player_node. If you copied my code from above, that won’t be the case though.

njamster | 2020-05-28 12:04