Hello
I’m using Godot 4.3 for development of a game and I’m quite new at this.
I have a problem which I set a variable on _ready() function in a global script file, then when i call it from another script it shows ‘null’.
When the control node is ready, it sets the variable ‘running_skill’ and prints it - then when the player press a button - the variable contains ‘null’.
There are 2 node scripts, Player’s 2D Body and Control (-global script):
Control - changes the theme for a child node on player’s call
extends Control
@onready var running_skill: Panel
func _ready() -> void:
running_skill = get_node("%running_skill")
if running_skill:
print(running_skill)
func activate_running_skill(active:bool) -> void:
if running_skill:
print("got it")
else:
print("not happening")
Player’s - trigger the function in the control’s script.
The problem is that globals are loaded before every other scene. This means there is no node called running_skill inside the scene_tree when the global is calling _ready
the ‘running_skill’ panel is in the scene_tree of the control which has it’s script global. the function ‘_ready’ on control’s global script works and when i call the function that within that code its working great. after the loading of all of the scenes, I call it from another script, the player’s. but then, the function in the global script can’t find the panel that it found on ‘_ready’
control (global script)> containers… > running_skill (panel)
player
when i use ‘print(running_skill)’ on control’s ‘_ready’ - it works. i succeed to do so with both direct path and get_node (still don’t know which is better). but, after that i call the function that already know the panel but it says it’s a null.
I thought that maybe the variable which points at the panel should be exported, but the function i call use it, i don’t need it outside.
Did you make the scene an autoload or just the script?
Global scripts (in fact scripts in general) don’t carry the node that they are attached to.
The node carries the script.
When you make that script an autoload a node is created and added to the scene.
This is stated in the documents here as a note.
When autoloading a script, a Node will be created and the script will be attached to it. This node will be added to the root viewport before any other scenes are loaded.
I will check how to make the node a singleton instead of the script.
What i did actually did when I called the global function from outside is to open another panel which is already loaded ?
Thank you, that was the problem. Now the Control Node is available when called from Player’s script. But, I read that global nodes should be removes from the root scenes tree - how do I make the global Control Node visible then ?