game/user_interface/worker_prompt.gd

54 lines
1.1 KiB
GDScript3
Raw Permalink Normal View History

class_name WorkerPrompt extends Control
##
2023-02-01 01:05:48 +01:00
## The work being performed by the prompt has ended.
##
signal prompt_completed()
##
2023-02-01 01:05:48 +01:00
## The prompt has been started.
##
signal prompted()
var _worker_thread := Thread.new()
##
2023-02-01 01:05:48 +01:00
## Used to display the progress of the prompt or [code]null[/code] to not display anything.
##
@export
var progress: Range = null
##
2023-02-01 01:05:48 +01:00
## Starts the prompt, emitting [signal prompted].
##
2023-02-01 01:05:48 +01:00
## [signal prompt_completed] is emitted when the work being performed by the prompt has ended.
##
2023-02-01 19:26:41 +01:00
## 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()
2023-02-01 19:26:41 +01:00
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()