How can I update the text property a RichTextLabel (MyRTL) from the scriptnode.gd in the below example?
I am trying to change the text property on a Rich Text Label from a script that’s separate to it, but I get the following error:
Invalid set index ‘text’ (on base: ‘null instance’) with value of type ‘String’.
I have tried a few things:
Adding a node2d that does nothing but link to the script
Using classes, ie. preloading the additional script in the mainscene.gd script, making a new instance of the scriptnode.gd script, then accessing the function that updates the label from the main scene.
Tried making scriptnode.gd an autoload script in the project settings
I’ve tried setting up a test project to figure it out with no luck too.
My scene tree looks like this:
When editing scriptnode.gd I can drag and drop from the scene tree into the script fine and the path looks correct, but it crashes at runtime with the error. I can even do .text and the editor sees it, but again at runtime it fails. How can I resolve this?
I’m not quite sure what’s going on with your scene, in part because the way you’re going about it isn’t how I’ve done it before. Have you tried using an exported variable to reference the RichTextLabel inside scriptnode.gd?
@export var label: RichTextLabel
Then you can drag-and-drop that into that field in the inspector and reference it like a RichTextLabel, if all works as expected, i.e.
instantiate() will not work in this case, as it works by unpacking the PackedScene and accessing it, not the node itself. That is, you can add your RTL node to the scene through it and use myfunc() in that case. However, if you want to use a ready-made variant, use @onready var rtl = NodePath to RTL
rtl.myfunc()
I think this should help
Not sure if you have multiple scripts or just changed the names for the post, but that Label is in main_scene.gd, and the other code was in scriptnode.gd.
Multiple scripts, main_scene.gd is attached to the MainScene node, scriptnode.gd is attached to the “scriptnode” node. I’m trying to update the MyRTL’s text property from scriptnode.gd.
If that’s the case, the two code snippets I posted above need to both be inside scriptnode.gd and the MyRTL that’s in your screenshot needs to be set in the scriptnode.gd node in the Inspector.
I ended up making a new project again from scratch and copying these screenshots exactly and it worked. Weirdly, going back to my first example and making the exact same changes didn’t work. However, going back to my actual game project and doing those changes also worked so I’m happy.
Thank you both for helping me with this problem, I’ve been tearing my hair out for a few hours trying to figure it out.