Fix issues with reconnecting to server

pull/2/head
Ensar Sarajčić 2022-01-20 18:23:49 +01:00
parent c9652ae651
commit 2e2a73e207
4 changed files with 14 additions and 10 deletions

View File

@ -18,7 +18,7 @@ func _on_ContinueButton_pressed():
func _on_ExitButton_pressed(): func _on_ExitButton_pressed():
get_tree().paused = false get_tree().paused = false
if MultiplayerController.connected: if MultiplayerController.is_online():
MultiplayerController.quit() MultiplayerController.quit()
get_tree().change_scene("res://menu/main_menu.tscn") get_tree().change_scene("res://menu/main_menu.tscn")
get_tree().root.get_child(get_tree().root.get_child_count() - 1).queue_free() get_tree().root.get_child(get_tree().root.get_child_count() - 1).queue_free()

View File

@ -9,7 +9,6 @@ var peers_vehicles = {}
var current_track: Node = null var current_track: Node = null
var current_track_path: String var current_track_path: String
var current_vehicle: String var current_vehicle: String
var connected = false
func _ready(): func _ready():
@ -20,6 +19,13 @@ func _ready():
get_tree().connect("connection_failed", self, "_connection_failed") 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): func create_server(port, track, vehicle):
current_track_path = track current_track_path = track
current_track = load(track).instance() current_track = load(track).instance()
@ -29,7 +35,6 @@ func create_server(port, track, vehicle):
peers[1] = true peers[1] = true
create_player(1, vehicle) create_player(1, vehicle)
get_tree().root.call_deferred("add_child", current_track) get_tree().root.call_deferred("add_child", current_track)
connected = true
func create_client(address, port, vehicle): func create_client(address, port, vehicle):
@ -56,21 +61,20 @@ func _peer_disconnected(peer_id):
func _connected_to_server(): func _connected_to_server():
print("_connected_to_server") print("_connected_to_server")
connected = true
func _connection_failed(): func _connection_failed():
connected = false
current_track = null current_track = null
peers_vehicles.clear() peers_vehicles.clear()
get_tree().network_peer = null
_server_disconnected() _server_disconnected()
func _server_disconnected(): func _server_disconnected():
connected = false
current_track = null current_track = null
peers.clear() peers.clear()
peers_vehicles.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().root.get_child(get_tree().root.get_child_count() - 1).queue_free()
get_tree().change_scene("res://menu/main_menu.tscn") get_tree().change_scene("res://menu/main_menu.tscn")
@ -97,8 +101,8 @@ func destroy_player(peer_id):
func quit(): func quit():
enet_peer.close_connection() enet_peer.close_connection()
connected = false
current_track = null current_track = null
get_tree().network_peer = null
peers.clear() peers.clear()
peers_vehicles.clear() peers_vehicles.clear()

View File

@ -21,7 +21,6 @@ func _ready() -> void:
func spawn_player(player_node: BuggedVehicle, gui: Node) -> void: func spawn_player(player_node: BuggedVehicle, gui: Node) -> void:
self.player_node = player_node self.player_node = player_node
self.player_node.add_child(player_controller)
self.gui = gui self.gui = gui
if ready: if ready:
_spawn_in_player() _spawn_in_player()

View File

@ -256,6 +256,7 @@ func _physics_process(delta: float):
steering = steering_input * lerp(max_steer_angle_rad, speed_steer_angle_rad, steer_speed_factor) 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) emit_signal("steering_updated", steering, steering / max_steer_angle_rad)
if MultiplayerController.is_online():
if get_network_master() == get_tree().get_network_unique_id(): if get_network_master() == get_tree().get_network_unique_id():
_synchronize() _synchronize()