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)
var input_thread: Thread
var stop: bool = false
const SERVER_CONSOLE_INPUT_PATH := "user://server_console_input"
const READING_INTERVAL: float = 10
var reading_timer: float = 0
func _enter_tree() -> void:
input_thread = Thread.new()
input_thread.start(_process_input)
func _process(delta: float) -> void:
if not Networker.is_dedicated or Referencer.chat == null:
return
reading_timer -= delta
func _exit_tree() -> void:
stop = true
input_thread.wait_to_finish()
if reading_timer > 0:
return
reading_timer = READING_INTERVAL
var file := FileAccess.open(SERVER_CONSOLE_INPUT_PATH, FileAccess.READ)
if not file:
return
var text := file.get_as_text()
if not text:
return
func _process_input() -> void:
var text := ""
while not stop and text != "quit":
text = OS.read_string_from_stdin().strip_edges()
if text and Referencer.chat:
print("Inserted text: ", text)
message_sent.emit(text)
print("STOPPING!")
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
var players := {}
var is_dedicated: bool = false
var _peer: ENetMultiplayerPeer
var _local_player_info := {"name": ""}
@ -35,6 +36,7 @@ func set_local_player_info(username: String) -> void:
func host_game(dedicated: bool = false) -> void:
print("Hosting...")
is_dedicated = dedicated
_peer = ENetMultiplayerPeer.new()
var error := _peer.create_server(DEFAULT_PORT, MAX_PEERS)