How do you get_tree inside a loaded script?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Elkien3

I made a “” GDscript and made two functions, startServer and joinServer, that were basically copied and pasted from this page

func startServer():
    var peer =
    peer.create_server(SERVER_PORT, MAX_PLAYERS)
    print(get_tree()) #added that for debug purposes
    #get_tree().set_network_peer(peer) commented out since it stops the game

I am trying to use these from my MainMenu script with

var Network = load("res://").new()



the resulting print is [Object:null] and if the line after would trigger an error: Attempt to call function 'set_network_peer' in base 'null instance' on a null instance.
The script does not cause the game to stop if the code is put directly into the, but I’d guess I’d need to use these functions or other functions in other scenes so I would rather be able to have them all in a single file and load them up as needed.

I’m quite new to Godot and I couldn’t find any questions/answers close enough for me to figure out how to do/fix this, so I appreciate any help.

I’m using Godot stable 3.1.1 on Windows 10

what about making it autoload then?
add to autoload and use it everywhere when need.

volzhs | 2019-05-30 04:46

:bust_in_silhouette: Reply From: Asasma_Sei

Method get_tree() is defined in Node but not Object
you need extends Node in your code

or you can let other node sent a get_tree() in


var getTree

var Network = load("res://").new()

func _ready():
   Network.getTree = get_tree()

and use getTree in

I didn’t include the entire, but it did have extends Node at the start of the file. had extends Node2D not sure if thats important

The second method worked though, thank you for the help.

Elkien3 | 2019-06-02 17:02