0 votes

I made a "Network.gd" GDscript and made two functions, startServer and joinServer, that were basically copied and pasted from this page

func startServer():
    var peer = NetworkedMultiplayerENet.new()
    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://Network.gd").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 MainMenu.gd, 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

in Engine by (12 points)

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

1 Answer

+1 vote

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


#in network.gd
var getTree

#in main.gd
var Network = load("res://Network.gd").new()

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

and use getTree in network.gd

by (18 points)

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

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

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 Frequently asked questions and 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 [email protected] with your username.