I have a button which when pressed calls OpenWindow(). In OpenWindow i want to instantiate a Scene in the main node, but i always get a main = NULL. I tried to change the node or have a reference through @export but it didnt work. When _ready is called it prints out the path and even the main node but when i then afterwards try to do the same thing with OpenWindow, which is called after _ready, there no longer is a path for the current node or main.In this code i tried to copy the path when i can get it in main but that also doesnt work. Here is the Code:
func OpenWindow(ID:String):
print(main)
var CustomWindowScene = load(“res://Scenes/Windows/default.tscn”) #print(ID) #var CustomWindowScene = load(“res://Scenes/Windows/”+ ID +“.tscn”)
print(CustomWindowScene)
var window = CustomWindowScene.instantiate()
main.add_child(window)
And here is the Output:
Your main is loading correctly. The “NULL” you are seeing is the Custom Window Scene, which means that the file path is most likely incorrect. The following code correctly loads an object into the main that you are looking for with the following scene tree:
var main
func _ready() -> void:
print("self: ", self.get_path())
print("parent: ", get_parent())
print("parent.parent: ", get_parent().get_parent())
main = get_parent().get_parent()
await get_tree().create_timer(1.0).timeout
OpenWindow("HELLO")
func OpenWindow(ID:String):
print(main)
var CustomWindowScene = load("res://Scenes/Windows/default.tscn")
#print(ID)
#var CustomWindowScene = load(“res://Scenes/Windows/”+ ID +“.tscn”)
print(CustomWindowScene)
var window = CustomWindowScene.instantiate()
main.add_child(window)
`
You should consider using signals to add windows like this, as if you were to move this button down a level, you would need to change how to find main, where if you used a signal, a script running on main could detect that signal and put the element in the correct spot no matter where the original button is located on the scene tree. You can also use hooks to send in arguments like id.
I tryed it and when i called OpenWindow in _ready it worked fine but when i called it through a button press it once again returned NULL. Here is a photo of the tree, if i overlooked something there:
with the button being a child of Desktop. I also tryed and it worked with Signals, thank you for the Tip. I think i am gonna do it with Signals but does any one know why in the first place WindowOpener gets seemingly “unloaded” after _ready is called?
I am pretty sure that Desktop_Script is not a node on the scene tree, but a class. Consequently, trying to get its parent will return null. You would want to load the actual node on the scene tree for the function to work
@hexgrid@frozen_coder ty for the help i tryed both get_node() and print(get_tree_string_pretty()) but i dont know how but window manager is somehow getting removed from the game tree. I didnt find a solution for that but i found a work around by Autoloading the script.