Get_tree().change_scene_to_file() is crashing my game

Godot Version

Godot Engine v4.2.1.stable.official.b09f793f5

Question

I have the following code in a GameManager autoload script:

extends Node

@rpc("call_local", "reliable")
func load_scene(game_scene_path):
	get_tree().change_scene_to_file(game_scene_path)

For some reason, whenever I call this method from my menu, it loads on all connected peers but not on the server player. In fact, it crashes their game. For more information, here is my NetworkManager:

extends Control

const DEFAULT_ADDRESS = "127.0.0.1"
const DEFAULT_PORT = 8910
var peer

var players = {}

# Called when the node enters the scene tree for the first time.
func _ready():
	multiplayer.peer_connected.connect(peer_connected)
	multiplayer.peer_disconnected.connect(peer_disconnected)
	multiplayer.connected_to_server.connect(connected_to_server)
	multiplayer.connection_failed.connect(connection_failed)
	if "--server" in OS.get_cmdline_args():
		hostGame()

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass

# this get called on the server and clients
func peer_connected(id):
	print("Player Connected " + str(id))
	
# this get called on the server and clients
func peer_disconnected(id):
	print("Player Disconnected " + str(id))
	players.erase(id)
	var player_list = get_tree().get_nodes_in_group("Player")
	for i in player_list:
		if i.name == str(id):
			i.queue_free()
			
# called only from clients
func connected_to_server():
	print("connected To Sever!")
	SendPlayerInformation.rpc_id(1, name, multiplayer.get_unique_id())

# called only from clients
func connection_failed():
	print("Couldnt Connect")

@rpc("any_peer")
func SendPlayerInformation(name, id):
	if !players.has(id):
		players[id] ={
			"name" : name,
			"id" : id,
			"score": 0
		}
	
	if multiplayer.is_server():
		for i in players:
			SendPlayerInformation.rpc(players[i].name, i)

func hostGame():
	peer = ENetMultiplayerPeer.new()
	var error = peer.create_server(DEFAULT_PORT, 2)
	if error != OK:
		print("cannot host: " + error)
		return
	peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER)
	
	multiplayer.set_multiplayer_peer(peer)
	print("Waiting For Players!")
	
	
func host_game(name):
	hostGame()
	SendPlayerInformation(name, multiplayer.get_unique_id())


func add_client(ip, port):
	peer = ENetMultiplayerPeer.new()
	if ip == "" and port == "":
		peer.create_client(DEFAULT_ADDRESS, DEFAULT_PORT)
	else:
		peer.create_client(ip, int(port))
	peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER)
	multiplayer.set_multiplayer_peer(peer)

and my menu is just calling the following line:

GameManager.load_scene.rpc("res://scenes/test-scene.tscn")

Figured it out! If there is no root/game node then it will try to queue_free the game and everything will break! Solution was adding a “game” node to the main scene that everything inherited from

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.