0 votes

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

in Engine by (91 points)
recategorized by

1 Answer

+2 votes
Best answer

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)
by (10,521 points)
selected by

It work perfectly thank you very much !

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.

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.

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.