Looking to see if there is a way to close a hosting connection fully

Godot Version

Godot 4.2.2

Question

I’ve just been getting a weird problem in my game. and it fixes itself completely when the host completely quits the game. The problem is when the host quits the game to the main menu no one else can create a server on the same port, when switching to a different port or the original hoster quits the game fully the problem is fixed so it is definitely a problem with the port remaining active or something?

is there any specific syntax that I am missing that would fix this? or alternatively if someone has experience with GodotSteam maybe you could tell me if this would ever be a problem using steam networking as that is what the release game will be using. Your time is much appreciated

This is the quit code incase that is the problem

func _on_quit_pressed():
	var Mission = Global.current_mission
	var id := multiplayer.get_unique_id()
	if Global.is_multiplayer == true:
		if multiplayer.is_server():
			disconnect_all(id)
			multiplayer.multiplayer_peer.disconnect_peer(multiplayer.get_unique_id())
		else:
			disconnect_self.rpc(id)
		get_tree().change_scene_to_file("res://Scenes/Menu.tscn")
	elif Global.is_multiplayer == false:
		get_tree().change_scene_to_file("res://Scenes/Menu.tscn")
@rpc("call_local", "any_peer")
func disconnect_self(peer_id : int):
	for i in GlobalMissionManager.Players:
		if GlobalMissionManager.Players[i].id == peer_id:
			GlobalMissionManager.Players.erase(peer_id)
	reset_player_list()
	await get_tree().create_timer(0.1).timeout
	multiplayer.multiplayer_peer.disconnect_peer(peer_id)
@rpc("call_local", "any_peer")
func disconnect_all(host):
	for i in GlobalMissionManager.Players:
		if i != host:
			multiplayer.multiplayer_peer.disconnect_peer(i)

You aren’t closing the server, only disconnecting everybody so the port is still in use and others could re-connect too. Use multiplayer.multiplayer_peer.close(), if you don’t rely on the peer_disconnected signal to clean up, then you can probably omit manully disconnecting all of your peers and just close the server, change the scene.

2 Likes

Thank you so much! Okay, I tested it a little and it causes an error in the disconnected function which happens when multiplayer.server_disconnected

here’s the function

func disconnected():
	var Mission = Global.current_mission
	if Mission != null:
		if get_tree().get_root().has_node(Mission):
			get_tree().get_root().get_node(Mission).queue_free()
	get_tree().change_scene_to_file("res://Scenes/Menu.tscn")
	var screen = load("res://Scenes/Multiplayer/disconnected_Screen.tscn").instantiate()

it happens on get_tree().change_scene_to_file("res://Scenes/Menu.tscn") with the error

Cannot call method ‘change_scene_to_file on a null value’

Maybe another change_scene_to_file has already run on that frame, tough to say

1 Like

That was it! thank you so much for the help! it works completely now!

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