Fix join logic in menu panel

This commit is contained in:
JonShard
2026-07-29 14:45:09 +02:00
parent 68b6d4d1dc
commit dce694dac4
4 changed files with 30 additions and 51 deletions
-3
View File
@@ -1,10 +1,7 @@
extends MultiplayerSpawner
@export var kitchen_scene: PackedScene
func _ready() -> void:
if not kitchen_scene:
push_error("StationSpawner missing kitchen_scene")
+2 -12
View File
@@ -34,7 +34,7 @@ func _process(delta: float) -> void:
# if we're held or moving, reset timer and return
if _pickable.get_picked_up_by() or _rigid.linear_velocity.length_squared() > pow(minimum_speed_square,2):
_time_left = time_to_despawn
_set_visible(true)
get_parent().visible(true)
return
@@ -50,14 +50,4 @@ func _process(delta: float) -> void:
# Make item blink in and out before despawning
if _time_left < time_to_despawn / 2:
_set_visible(int(_time_left * 5.0) % 2 == 0)
# `visible` is not a replicated property, so a blink driven only here would make
# the item flicker on the host and stay solid on clients. Until it is synced,
# only warn when there is nobody else to disagree with.
func _set_visible(value: bool) -> void: # TODO: Fix blinking
if true:# NetworkManager.is_online():
get_parent().visible = true
return
get_parent().visible = value
get_parent().visible(int(_time_left * 5.0) % 2 == 0)
+23 -36
View File
@@ -6,8 +6,8 @@ extends Control
## directly.
const JON_SCENE := "res://Scenes/jon_scene.tscn"
const MULTIPLAYER_SCENE := "res://Scenes/multiplayer.tscn"
const SETTINGS_PATH := "user://vryhungry_settings.cfg"
const MULTIPLAYER_SCENE := "res://Scenes/multiplayer.tscn"
var _ip := "127.0.0.1"
@@ -22,11 +22,12 @@ var _ip := "127.0.0.1"
@onready var _join_btn: Button = %JoinButton
@onready var _back_btn: Button = %BackButton
# store original text so we can say "connecting..." while pinging server
var original_join_text: String
func _ready() -> void:
print("Menu Panel _ready")
if _bypass_menu_for_cmdline():
return
for child in _keypad.get_children():
if child is Button:
child.pressed.connect(_on_key.bind(child.text))
@@ -35,29 +36,12 @@ func _ready() -> void:
_join_menu_btn.pressed.connect(_show_join_view)
_join_btn.pressed.connect(_on_join_pressed)
_back_btn.pressed.connect(_show_root_view)
NetworkManager.connection_failed.connect(_handle_connection_failed)
_load_ip()
_show_root_view()
_refresh_ip()
## --server / --join <ip> on the command line skip straight to the multiplayer
## scene, same as the previously headless-tested main.tscn flow.
func _bypass_menu_for_cmdline() -> bool:
var args := OS.get_cmdline_user_args()
if args.has("--server"):
NetworkManager.request_host()
get_tree().change_scene_to_file.call_deferred(MULTIPLAYER_SCENE)
return true
if args.has("--join"):
var idx := args.find("--join")
var addr := "127.0.0.1"
if idx + 1 < args.size():
addr = args[idx + 1]
NetworkManager.request_join(addr)
get_tree().change_scene_to_file.call_deferred(MULTIPLAYER_SCENE)
return true
return false
original_join_text = _join_btn.text
func _show_root_view() -> void:
print("Menu Panel show root view")
@@ -69,7 +53,8 @@ func _show_join_view() -> void:
print("Menu Panel show join view")
_root_view.visible = false
_join_view.visible = true
set_status("Enter host IP, then Join")
_status.text= "Enter host IP, then Join"
_status.add_theme_color_override("font_color", Color.WHITE)
func _on_key(key: String) -> void:
@@ -95,17 +80,26 @@ func _on_john_pressed() -> void:
func _on_host_pressed() -> void:
print("Menu Panel press host")
NetworkManager.host()
get_tree().change_scene_to_file.call_deferred(MULTIPLAYER_SCENE)
func _on_join_pressed() -> void:
print("Menu Panel press host")
print("Menu Panel press join")
if _ip.is_empty():
set_status("Enter an IP first")
_status.text= "Enter an IP first"
_status.add_theme_color_override("font_color", Color.ORANGE)
return
_save_ip()
NetworkManager.request_join(_ip)
get_tree().change_scene_to_file.call_deferred(MULTIPLAYER_SCENE)
_join_btn.disabled = true
_join_btn.text = "Connecting..."
NetworkManager.join(_ip)
func _handle_connection_failed():
print("Menu Panel _handle_connection_failed")
_status.text= "Connection failed"
_status.add_theme_color_override("font_color", Color.RED)
_join_btn.disabled = false
_join_btn.text = original_join_text
## Remembers the last IP the player tried to join, so the keypad starts
@@ -121,10 +115,3 @@ func _save_ip() -> void:
cfg.load(SETTINGS_PATH)
cfg.set_value("network", "last_ip", _ip)
cfg.save(SETTINGS_PATH)
## Called by network_manager (e.g. on connection_failed after a bounce back to
## this menu) to show feedback.
func set_status(text: String) -> void:
if _status:
_status.text = text
+5
View File
@@ -2,6 +2,7 @@ extends Node
const DEFAULT_PORT := 24565
const MAX_CLIENTS := 7
const MULTIPLAYER_SCENE := "res://Scenes/multiplayer.tscn"
signal player_joined(peer_id: int)
@@ -37,6 +38,7 @@ func host(port: int = DEFAULT_PORT) -> Error:
multiplayer.multiplayer_peer = peer
print("NetworkManager HOST started on port %d (peer id %d)" % [port, multiplayer.get_unique_id()])
session_started.emit(true)
get_tree().change_scene_to_file.call_deferred(MULTIPLAYER_SCENE)
return OK
@@ -142,6 +144,8 @@ func _on_peer_disconnected(peer_id: int) -> void:
func _on_connected_to_server() -> void:
print("NetworkManager connected_to_server (my id=%d)" % multiplayer.get_unique_id())
session_started.emit(false)
get_tree().change_scene_to_file.call_deferred(MULTIPLAYER_SCENE)
func _on_connection_failed() -> void:
print("NetworkManager connection_failed")
@@ -156,6 +160,7 @@ func _on_server_disconnected() -> void:
# --- Command-line driven test bootstrap -----------------------------------
func _handle_cmdline() -> void:
print("NetworkManager _handle_cmdline")
var args := OS.get_cmdline_user_args()
if args.has("--server"):
print("NetworkManager cmdline: --server")