0 votes

I have Node2D and label

- Label

My Example Script to Node2D

extends Node2D func _ready():
var state = Test.new(self)
pass class Test:
var test
func _init(test):
self.test = test test.$"Label".text = "Hello from Test Class" # ERROR test.get_node("Label").text = "Hello from Test Class" pass

in Engine by (50 points)

2 Answers

0 votes
Best answer

$ doesn't make sense in an inner class, because $ really is a shortcut for get_node(...), and get_node is a function inherited in Node. So you can't use $ unless your inner class is also a node, I wonder if that's possible though, never tested that.

However, using $ literally in place of get_node when calling it from a node variable... I don't think it's supposed to work. You could go ahead and just write test.get_node("Label").text instead. Maybe it could work if test is a node present in the tree though.

by (28,735 points)
selected by

than you for your answer :)

0 votes

There's no direct way as @Zylann already said but a way I do it is first declaring a variable myself with the Nodes info and then passing myself when creating an instance of the class

extends Node2D

var myself

class Test:

    var scene
    func _init(scn):

        self.scene = scn
        scn.get_node("Label").text = "Hello from Test Class"

func _ready():
    myself = get_tree().get_current_scene()
    t = Test.new(myself)
by (26 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.