How to fetch a node with a custom class.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DriNeo
:warning: Old Version Published before Godot 3 was released.

The error is: “Invalid set index ‘something’ (on base: ‘null instance’).”

What am I doing wrong ?

# autoloadFile.gd
extends Node

var something
class Slot extends Node:
 func _init():
  get_node(/root/autoloadFile).something = "Hello"
func _ready():
 var instance = Slot.new()
 print(something)

The error seems to be fixed with “_ready” function instead of “_init” but the last line doesn’t work and it doesn’t throw error.

extends Node
var something = ""
  
class Slot extends Node:
 func _ready():
  get_node("/root/autoloadFile").something = "Hello"

func _ready():
 var oops = Slot.new()
 print(something)

DriNeo | 2016-03-07 00:29

:bust_in_silhouette: Reply From: volzhs

_init will be called when it’s created.
when _init is called, that node is not added to scene.

So, get_node function can not be executed properly, because your get_node looking for “/root/…”, it will work properly after the node is added to a scene.

and the second case as your comment,
_ready will be called after it’s added to scene.
You just created an instance and not added to a scene.

It will work after you do add_child.

func _ready():
    var oops = Slot.new()
    add_child(oops)
    print(something)