Attention | Topic was automatically imported from the old Question2Answer platform. | |
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