Topic was automatically imported from the old Question2Answer platform.
Asked By
Pin
Old Version
Published before Godot 3 was released.
I have Node2D and label
Node2D
- 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
$ 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.
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)