So the problem is that i want to get player_position in UI label3 scene but it is always nill. If i try to print player_position in Globals _process it returns nill and actual player_position, but i dont know why. I tried using await player_entered signal in _process but it didnt work too. In that screenshots player_entered signal does nothing.
Player code does not affect player_position variable
Is there a way to solve the problem? Why player_position prints nill and actual player position in Globals _procces function? Also is there a better way accesing that variable in UI scene? Maybe i need to update the variable in the UI scene somehow?
Thank you
Sooo I think it might be something to do with your “player” variable. It’s currently just var player which I think might result in null.
I can see you have a function to assign that variable though. It might work if you move that code to your func _ready().
Alternatively, you can create @onready variables for anything that references a node in order to assign the node to a variable right away. You can do this easily by holding Ctrl, then click and dragging the node into the script.
Edit* you could also call _on_player_ready() in the ready function.
This isn’t a direct answer for your exact problem, but it might help you. I tend to use code like this in my global singleton file:
func get_player() -> Player:
return _get_player_recursive(get_tree().get_root())
func _get_player_recursive(node: Node) -> Player:
for child in node.get_children():
if child is Player:
return child
var found = _get_player_recursive(child)
if found:
return found
return null
This works as long as your player scene has a script with class_name Player.
Once you have this setup, your other script could simply be text = str(Global.get_player().global_position).
_on_player_ready() is called by a signal from player. When _ready() is called in Globals, player scene didnt load so i cant just move my code in _ready() function. Sorry for not clarifying it in post. Also @onready doesnt help too. If i try to print player in _process() it is printing player and null for some reason each frame:
Calling a _on_ready_player() in _ready() gives an error:
because it didnt load yet.
Thank you
I think what you want is “how do I get my player Node in my glabals script”
I wrote earlier about Ctrl+left click+dragging a node into the script, you do that to creat and onready variable, this is a common method with many scripts and nodes.
Alternatively you can add the player scene to a group then reference it any script. Go to the player scene, click the first node in the tree, find the groups tab (you toggle to where you do signals, it’s on the right side bar near the top). And add to global group “Player”
Then in your Global’s script add an @onready var Player = first_node_in_group(“Player”)
** Just read the other response suggesting creating a class, reading is important…
To create a new class, at the top of your script have “class_name Player”
And about classes, thank you for clarifying, the problem was that i forgot to save the Player scene and Globals script didnt know about Player class, and i thought i need to extend Globals with Player class