Hi all,
I am working on a peer-to-peer Pong game.
My issue now is on Windows when the host closes the game window.
I am using the following function to capture when the game window is closed:
func _notification(what):
if what == MainLoop.NOTIFICATION_WM_QUIT_REQUEST:
if get_tree().network_peer != null:
rpc_id(0, "stop_game", get_instance_id())
get_tree().quit()
I tell the connected client to stop their execution of the game physics and load the initial screen again.
remote func stop_game(_requester) -> void:
get_tree().network_peer = null
get_tree().call_group_flags(SceneTree.GROUP_CALL_REALTIME, "player", "stop_network_tickrate")
get_tree().call_group_flags(SceneTree.GROUP_CALL_REALTIME, "player", "stop_physics")
get_tree().call_group_flags(SceneTree.GROUP_CALL_REALTIME, "BallGroup", "stop_physics")
get_tree().change_scene_to(network_setup_scene)
This code works as expected when playing the game on Linux or macOS but not on Windows.
I have those "helper" functions on my player and ball scenes that will stop the physics execution and also a timer that I use.
For the player:
func stop_network_tickrate() -> void:
print("player stop_network_tickrate")
$NetworkTickRate.stop()
func stop_physics() -> void:
print("player stop_physics")
set_physics_process(false)
For the ball:
func stop_physics() -> void:
print("ball stop_physics")
set_physics_process(false)
I have added those print
statements to make sure those functions were being called. I can see those on my output console:
player stop_network_tickrate
player stop_network_tickrate
player stop_physics
player stop_physics
ball stop_physics
The need to stop the physics process is because I am calling is_network_master
, which fails when there is no peer connected to the network.
func _physics_process(delta) -> void:
if is_network_master():
rset_unreliable("puppet_velocity", velocity)
else:
velocity = puppet_velocity
The error message was like this:
get_unique_id: The multiplayer instance isn't currently active. <C++ Error> Condition "!active" is true. Returned: 0 <C++ Source> modules/enet/networked_multiplayer_enet.cpp:658 @ get_unique_id() <Stack Trace> player.gd:42 @ _on_NetworkTickRate_timeout()
But when I close the game windows on Windows, it doesn't even call the stop_game
function.
Any suggestions on what is happening? Also, do you have any suggestions on a different way of managing that?
Thank you.