change consoler to read a file with intervals

This commit is contained in:
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
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!")
var file := FileAccess.open(SERVER_CONSOLE_INPUT_PATH, FileAccess.READ)
if not file:
return
var text := file.get_as_text()
if not text:
return
message_sent.emit(text)
file = FileAccess.open(SERVER_CONSOLE_INPUT_PATH, FileAccess.WRITE)
if not file:
return
file.store_string("")