change consoler to read a file with intervals

This commit is contained in:
Teatov 2025-02-15 03:07:14 +10:00
parent f10eebd332
commit a604032b79
2 changed files with 27 additions and 16 deletions

View File

@ -2,25 +2,34 @@ extends Node
signal message_sent(message: String) signal message_sent(message: String)
var input_thread: Thread const SERVER_CONSOLE_INPUT_PATH := "user://server_console_input"
var stop: bool = false const READING_INTERVAL: float = 10
var reading_timer: float = 0
func _enter_tree() -> void: func _process(delta: float) -> void:
input_thread = Thread.new() if not Networker.is_dedicated or Referencer.chat == null:
input_thread.start(_process_input) return
reading_timer -= delta
func _exit_tree() -> void: if reading_timer > 0:
stop = true return
input_thread.wait_to_finish()
reading_timer = READING_INTERVAL
func _process_input() -> void: var file := FileAccess.open(SERVER_CONSOLE_INPUT_PATH, FileAccess.READ)
var text := "" if not file:
while not stop and text != "quit": return
text = OS.read_string_from_stdin().strip_edges()
if text and Referencer.chat: var text := file.get_as_text()
print("Inserted text: ", text) if not text:
message_sent.emit(text) return
print("STOPPING!")
message_sent.emit(text)
file = FileAccess.open(SERVER_CONSOLE_INPUT_PATH, FileAccess.WRITE)
if not file:
return
file.store_string("")

View File

@ -7,6 +7,7 @@ const DEFAULT_PORT: int = 10567
const MAX_PEERS: int = 20 const MAX_PEERS: int = 20
var players := {} var players := {}
var is_dedicated: bool = false
var _peer: ENetMultiplayerPeer var _peer: ENetMultiplayerPeer
var _local_player_info := {"name": ""} var _local_player_info := {"name": ""}
@ -35,6 +36,7 @@ func set_local_player_info(username: String) -> void:
func host_game(dedicated: bool = false) -> void: func host_game(dedicated: bool = false) -> void:
print("Hosting...") print("Hosting...")
is_dedicated = dedicated
_peer = ENetMultiplayerPeer.new() _peer = ENetMultiplayerPeer.new()
var error := _peer.create_server(DEFAULT_PORT, MAX_PEERS) var error := _peer.create_server(DEFAULT_PORT, MAX_PEERS)