Error handling on peer creation
Some checks failed
linting & formatting / build (push) Failing after 5s

This commit is contained in:
Rob Kelly 2025-01-13 18:56:12 -07:00
parent 76ffac3b2c
commit 563d222456
3 changed files with 122 additions and 4 deletions

View File

@ -38,6 +38,9 @@ const RANDOM_NAMES = [
@onready var hostname_input: LineEdit = %HostnameInput
@onready var join_port_input: SpinBox = %JoinPortInput
@onready var err_already_in_use: ColorRect = %ErrAlreadyInUse
@onready var err_cant_create: ColorRect = %ErrCantCreate
func _ready() -> void:
name_input.text = RANDOM_NAMES.pick_random()
@ -73,19 +76,34 @@ func _build_game() -> Game:
return game
func _peer_ok(err: Error) -> bool:
match err:
ERR_ALREADY_IN_USE:
err_already_in_use.show()
return false
ERR_CANT_CREATE:
err_cant_create.show()
return false
return true
func start_host_lobby() -> void:
var game: Game = _build_game()
var peer := ENetMultiplayerPeer.new()
peer.create_server(host_port_input.value, MAX_CLIENTS)
if not _peer_ok(peer.create_server(int(host_port_input.value), MAX_CLIENTS)):
return
var game: Game = _build_game()
add_sibling(game)
game.multiplayer.multiplayer_peer = peer
queue_free()
func join_lobby() -> void:
var game: Game = _build_game()
var peer := ENetMultiplayerPeer.new()
peer.create_client(hostname_input.text, join_port_input.value)
if not _peer_ok(peer.create_client(hostname_input.text, int(join_port_input.value))):
return
var game: Game = _build_game()
add_sibling(game)
game.multiplayer.multiplayer_peer = peer
queue_free()

View File

@ -165,6 +165,104 @@ layout_mode = 2
text = "Cancel
"
[node name="ErrAlreadyInUse" type="ColorRect" parent="."]
unique_name_in_owner = true
visible = false
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
color = Color(0, 0, 0, 0.156863)
[node name="PanelContainer" type="PanelContainer" parent="ErrAlreadyInUse"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -20.0
offset_top = -20.0
offset_right = 20.0
offset_bottom = 20.0
grow_horizontal = 2
grow_vertical = 2
[node name="MarginContainer" type="MarginContainer" parent="ErrAlreadyInUse/PanelContainer"]
layout_mode = 2
theme_override_constants/margin_left = 32
theme_override_constants/margin_top = 16
theme_override_constants/margin_right = 32
theme_override_constants/margin_bottom = 16
[node name="VBoxContainer" type="VBoxContainer" parent="ErrAlreadyInUse/PanelContainer/MarginContainer"]
layout_mode = 2
theme_override_constants/separation = 16
[node name="Label" type="Label" parent="ErrAlreadyInUse/PanelContainer/MarginContainer/VBoxContainer"]
layout_mode = 2
theme_override_colors/font_color = Color(1, 0.3, 0.3, 1)
theme_override_constants/outline_size = 4
theme_override_font_sizes/font_size = 24
text = "ERROR: Connection already in use."
horizontal_alignment = 1
[node name="Button" type="Button" parent="ErrAlreadyInUse/PanelContainer/MarginContainer/VBoxContainer"]
layout_mode = 2
size_flags_horizontal = 4
text = "OK"
[node name="ErrCantCreate" type="ColorRect" parent="."]
unique_name_in_owner = true
visible = false
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
color = Color(0, 0, 0, 0.156863)
[node name="PanelContainer" type="PanelContainer" parent="ErrCantCreate"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -20.0
offset_top = -20.0
offset_right = 20.0
offset_bottom = 20.0
grow_horizontal = 2
grow_vertical = 2
[node name="MarginContainer" type="MarginContainer" parent="ErrCantCreate/PanelContainer"]
layout_mode = 2
theme_override_constants/margin_left = 32
theme_override_constants/margin_top = 16
theme_override_constants/margin_right = 32
theme_override_constants/margin_bottom = 16
[node name="VBoxContainer" type="VBoxContainer" parent="ErrCantCreate/PanelContainer/MarginContainer"]
layout_mode = 2
theme_override_constants/separation = 16
[node name="Label" type="Label" parent="ErrCantCreate/PanelContainer/MarginContainer/VBoxContainer"]
layout_mode = 2
theme_override_colors/font_color = Color(1, 0.3, 0.3, 1)
theme_override_constants/outline_size = 4
theme_override_font_sizes/font_size = 24
text = "ERROR: Can't create connection."
horizontal_alignment = 1
[node name="Button" type="Button" parent="ErrCantCreate/PanelContainer/MarginContainer/VBoxContainer"]
layout_mode = 2
size_flags_horizontal = 4
text = "OK"
[connection signal="pressed" from="Menu/MainMenu/Host" to="." method="show_host_menu"]
[connection signal="pressed" from="Menu/MainMenu/Join" to="." method="show_join_menu"]
[connection signal="pressed" from="Menu/MainMenu/Quit" to="." method="quit"]
@ -172,3 +270,5 @@ text = "Cancel
[connection signal="pressed" from="Menu/HostMenu/Cancel" to="." method="show_main_menu"]
[connection signal="pressed" from="Menu/JoinMenu/Connect" to="." method="join_lobby"]
[connection signal="pressed" from="Menu/JoinMenu/Cancel" to="." method="show_main_menu"]
[connection signal="pressed" from="ErrAlreadyInUse/PanelContainer/MarginContainer/VBoxContainer/Button" to="ErrAlreadyInUse" method="hide"]
[connection signal="pressed" from="ErrCantCreate/PanelContainer/MarginContainer/VBoxContainer/Button" to="ErrCantCreate" method="hide"]

BIN
ui.theme

Binary file not shown.