I’ve been trying to reference other nodes or scripts and it always gives out the null value. I managed to work around it by making a global script, but now I want to make a reference within the global script, and I’m lost.
The goal is to make a global function I could call on to sort of have some sort of prefab for dialogues (it’s true there might be other ways of doing this, but not knowing how to properly reference nodes/scripts is a real bullet in the foot). Therefore, here I’m trying to reference a RichTextLabel named Dialogue.
Here’s the script (a simple one, really):
extends Node
@onready var dialogue: RichTextLabel = $CanvasLayer/Dialogue
var napple
var mc_direction
func talk() -> void:
dialogue.add_theme_font_size_override("normal_font_size", 30)
dialogue.text = "Hey, wassup."
“@onready var dialogue: RichTextLabel = $CanvasLayer/Dialogue” doesn’t work as intended. I tried get_node(“Dialogue”) as well but no success there either.
If the image doesn’t load, basically Dialogue is under CanvasLayer, itself under the root node Node. The global script wasn’t initially attached to any nodes (didn’t work), but I attached it to the root node (still doesn’t work).
Have you tried exporting the reference, using @export? That way, you would reference the Dialogue node manually before launching the game, avoiding any possible issue with the order of ready/onready calls (I’m assuming you have a null reference error on ready, before the reference is set by @onready, but I may be wrong).
Just note that I’m not so used to that way of doing things (I’m not even using gdscript), so my suggestion may be wrong, and please anybody correct me in such a case.
Global scripts are attached to a new node created under /root, next to your current scene. Run the game then check out the “remote” tab in the scene tree to see what is actually being created. I’m betting you added the .gd script as a global which will not have any children when created, you could add the .tscn scene as global which will create the entire scene and it’s children under /root.
Your script attached to the Scene’s Node should work, but I’m betting you have copies of this script operating elsewhere, such as a Global.
Try printing the node’s path on ready, maybe it’s tree (aka all it’s descendants), this will help debug how many Node have this script and where.