I saw another question on this part of the course and I applied another solution to prevent the host for starting the game before the other player joins in… I simply show the start button after the client joins. Don’t know if it is correct for later parts of the course, but here is what I did:
# levels/menu/menu.gd
extends Node
# reference to UI controls
@export var ui: Control
@export var level_container: Node
@export var level_scene: PackedScene
@export var ip_line_edit: LineEdit
@export var status_label: Label
# reference to the 2 HBoxContainers
@export var host_hbox: HBoxContainer
@export var not_connected_hbox: HBoxContainer
func _ready():
multiplayer.connection_failed.connect(_on_connection_failed)
multiplayer.connected_to_server.connect(_on_connected_to_server)
func _on_host_button_pressed():
# hide the not connected HBoxContainer
not_connected_hbox.hide()
Lobby.create_game()
# change the status label to "Waiting for players..."
status_label.text = "Waiting for players..."
func _on_join_button_pressed():
# hide the not connected HBoxContainer
not_connected_hbox.hide()
Lobby.join_game(ip_line_edit.text)
status_label.text = "Connecting..."
func _on_start_button_pressed():
# hide the UI on game start. Call on every client (no id specified)
hide_menu.rpc()
change_level.call_deferred(level_scene)
func change_level(scene: PackedScene):
# remove the current level if there is one. using for loop
for child in level_container.get_children():
level_container.remove_child(child)
child.queue_free()
level_container.add_child(scene.instantiate(), true)
func _on_connection_failed():
status_label.text = "Connection failed"
# better to re-enable the buttons if the connection failed
not_connected_hbox.show()
func _on_connected_to_server():
status_label.text = "Connected!"
# tell the server to change the status label when a player joins
player_joined.rpc()
# make an rpc to tell everyone that game has started and hide the UI
# The server must be the only one to tell everyone that the game has started,
# and also himself has to hide the UI
@rpc("call_local","authority","reliable")
func hide_menu():
ui.hide()
# make an rpc to tell the server to change status label when a player joins
@rpc("call_remote","any_peer","reliable")
func player_joined():
status_label.text = "Player joined!"
# show the host HBoxContainer
host_hbox.show()