state chart additions: consumed events export and auto_initialize toggle

This commit is contained in:
Haze Weathers 2023-05-02 13:13:49 -04:00
parent 941f8f0f09
commit 79cbd62a69
2 changed files with 13 additions and 1 deletions

View file

@ -22,6 +22,8 @@ enum ProcessMode {IDLE, PHYSICS}
## whether to process transition delays during physics or idle frames
export (ProcessMode) var transition_process_mode: int = ProcessMode.PHYSICS
## events to consume so that parent state can not
export (Array, String) var consumed_events: Array = []
## whether the current state is active
var active: bool setget _set_active
@ -77,6 +79,10 @@ func _state_event(event: String) -> bool:
# first match is taken
_queue_transition(transition)
return true
if event in consumed_events:
return true
return false
func _process(delta: float) -> void:
@ -113,6 +119,7 @@ func _process_transition(delta: float) -> void:
_queued_transition_time -= delta
# if ready, handle transition and clear queue
if _queued_transition_time <= 0.0:
print(_queued_transition.name)
var transition = _queued_transition
_queued_transition = null
_queued_transition_time = 0.0

View file

@ -2,6 +2,8 @@ tool
class_name StateChart, "state_chart.svg"
extends Node
## whether chart should initialize on its own or be done manually
export var auto_initialize: bool = true
## whether chart should propagate processing events every frame
export var idle_frame_event: bool = false
export var physics_frame_event: bool = false
@ -15,7 +17,10 @@ var _guard_properties: Dictionary = {}
func _ready() -> void:
if Engine.editor_hint:
return
if auto_initialize:
initialize()
func initialize() -> void:
# make sure only one child exists
if get_child_count() != 1:
push_error("StateChart must have exactly one child")