From 2e2a73e2075056fa8b7044edea86ec339e7f557d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Thu, 20 Jan 2022 18:23:49 +0100 Subject: [PATCH] Fix issues with reconnecting to server --- menu/pause_menu.gd | 2 +- network/multiplayer_controller.gd | 16 ++++++++++------ scenes/base_track_level.gd | 1 - vehicles/vehicle.gd | 5 +++-- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/menu/pause_menu.gd b/menu/pause_menu.gd index 13ef641..e81f540 100644 --- a/menu/pause_menu.gd +++ b/menu/pause_menu.gd @@ -18,7 +18,7 @@ func _on_ContinueButton_pressed(): func _on_ExitButton_pressed(): get_tree().paused = false - if MultiplayerController.connected: + if MultiplayerController.is_online(): MultiplayerController.quit() get_tree().change_scene("res://menu/main_menu.tscn") get_tree().root.get_child(get_tree().root.get_child_count() - 1).queue_free() diff --git a/network/multiplayer_controller.gd b/network/multiplayer_controller.gd index d0ba5e0..89e6f80 100644 --- a/network/multiplayer_controller.gd +++ b/network/multiplayer_controller.gd @@ -9,7 +9,6 @@ var peers_vehicles = {} var current_track: Node = null var current_track_path: String var current_vehicle: String -var connected = false func _ready(): @@ -20,6 +19,13 @@ func _ready(): get_tree().connect("connection_failed", self, "_connection_failed") +func is_online(): + var network_peer = get_tree().get_network_peer() + if network_peer == null: + return false + return network_peer.get_connection_status() == NetworkedMultiplayerPeer.CONNECTION_CONNECTED + + func create_server(port, track, vehicle): current_track_path = track current_track = load(track).instance() @@ -29,7 +35,6 @@ func create_server(port, track, vehicle): peers[1] = true create_player(1, vehicle) get_tree().root.call_deferred("add_child", current_track) - connected = true func create_client(address, port, vehicle): @@ -56,21 +61,20 @@ func _peer_disconnected(peer_id): func _connected_to_server(): print("_connected_to_server") - connected = true func _connection_failed(): - connected = false current_track = null peers_vehicles.clear() + get_tree().network_peer = null _server_disconnected() func _server_disconnected(): - connected = false current_track = null peers.clear() peers_vehicles.clear() + get_tree().network_peer = null get_tree().root.get_child(get_tree().root.get_child_count() - 1).queue_free() get_tree().change_scene("res://menu/main_menu.tscn") @@ -97,8 +101,8 @@ func destroy_player(peer_id): func quit(): enet_peer.close_connection() - connected = false current_track = null + get_tree().network_peer = null peers.clear() peers_vehicles.clear() diff --git a/scenes/base_track_level.gd b/scenes/base_track_level.gd index d938664..bf2caa2 100644 --- a/scenes/base_track_level.gd +++ b/scenes/base_track_level.gd @@ -21,7 +21,6 @@ func _ready() -> void: func spawn_player(player_node: BuggedVehicle, gui: Node) -> void: self.player_node = player_node - self.player_node.add_child(player_controller) self.gui = gui if ready: _spawn_in_player() diff --git a/vehicles/vehicle.gd b/vehicles/vehicle.gd index 0892d25..f53a533 100644 --- a/vehicles/vehicle.gd +++ b/vehicles/vehicle.gd @@ -256,8 +256,9 @@ func _physics_process(delta: float): steering = steering_input * lerp(max_steer_angle_rad, speed_steer_angle_rad, steer_speed_factor) emit_signal("steering_updated", steering, steering / max_steer_angle_rad) - if get_network_master() == get_tree().get_network_unique_id(): - _synchronize() + if MultiplayerController.is_online(): + if get_network_master() == get_tree().get_network_unique_id(): + _synchronize() func _generate_engine_sound(rpm_factor):