game/user_interface/worker_prompt.gd

54 lines
1.1 KiB
GDScript

class_name WorkerPrompt extends Control
##
## The work being performed by the prompt has ended.
##
signal prompt_completed()
##
## The prompt has been started.
##
signal prompted()
var _worker_thread := Thread.new()
##
## Used to display the progress of the prompt or [code]null[/code] to not display anything.
##
@export
var progress: Range = null
##
## Starts the prompt, emitting [signal prompted].
##
## [signal prompt_completed] is emitted when the work being performed by the prompt has ended.
##
## Either this function or [signal prompt_completed] can be awaited upon to handle asynchronous
## scheduling of tasks to execute after completion.
##
func prompt(steps: Array) -> void:
LocalPlayer.override_controls(hidden)
show()
prompted.emit()
while _worker_thread.is_alive():
await get_tree().process_frame
_worker_thread.start(func () -> void:
var count := steps.size()
var total := float(count)
for i in count:
steps[i].call()
if progress != null:
progress.value = i / total)
while _worker_thread.is_alive():
await get_tree().process_frame
_worker_thread.wait_to_finish()
prompt_completed.emit()
hide()