State Machine Logic Block
Related Config File Sections:
Related How To Guides
"State machines" are a type of Logic Block where you can trigger state transitions based on the current state and an event.
Technically, this is a finite state machine as known from CS class.
Video about state machines:
This is an example:
##! mode: my_mode
state_machines:
my_state:
states:
start:
label: Start state
step1:
label:
show_when_active:
show: on
show_tokens: None
events_when_started: step1_start
events_when_stopped: step1_stop
step2:
label:
transitions:
- source: start
target: step1
events: state_machine_proceed
- source: step1
target: step2
events: state_machine_proceed2
events_when_transitioning: going_to_step2
- source: step2
target: start
events: state_machine_proceed3
- source: step1, step2
target: start
events: state_machine_reset
##! test
#! start_game
#! start_mode my_mode
#! mock_event going_to_step2
#! assert_str_condition start device.state_machines.my_state.state
#! post state_machine_proceed
#! assert_str_condition step1 device.state_machines.my_state.state
#! assert_event_not_called going_to_step2
#! post state_machine_proceed2
#! assert_str_condition step2 device.state_machines.my_state.state
#! assert_event_called going_to_step2
Storing the State in a Player Variable
If you want to store the state of your state machine in a player variable your can use a variable_player. You can then use it on slides or in places where conditions do not work (yet).
##! mode: my_mode
state_machines:
my_state:
states:
start:
label: Start state
step1:
label:
show_when_active:
show: on
show_tokens: None
events_when_started: step1_start
events_when_stopped: step1_stop
step2:
label:
transitions:
- source: start
target: step1
events: state_machine_proceed
- source: step1
target: step2
events: state_machine_proceed2
events_when_transitioning: going_to_step2
- source: step2
target: start
events: state_machine_proceed3
- source: step1, step2
target: start
events: state_machine_reset
variable_player:
"{device.state_machines.my_state.state}":
my_player_var:
action: set
string: "{value}"
##! test
#! start_game
#! start_mode my_mode
#! advance_time_and_run .1
#! mock_event going_to_step2
#! assert_str_condition start device.state_machines.my_state.state
#! assert_player_variable start my_player_var
#! post state_machine_proceed
#! advance_time_and_run .1
#! assert_str_condition step1 device.state_machines.my_state.state
#! assert_player_variable step1 my_player_var
#! assert_event_not_called going_to_step2
#! post state_machine_proceed2
#! advance_time_and_run .1
#! assert_str_condition step2 device.state_machines.my_state.state
#! assert_event_called going_to_step2
#! assert_player_variable step2 my_player_var
Monitorable Properties
For
dynamic values and
conditional events, the prefix for state machines is
device.state_machines.(name)
.
state
: The state of this state machine as string. This will be one of your entries in your states section.
Something missing or wrong? You can fix it!
This website is edited by people like you! Is something wrong or missing? Is something out of date, or can you explain it better?
Please help us! You can fix it yourself and be an official "open source" contributor!
It's easy! See our Beginner's guide to editing the docs.
Page navigation via the keyboard: < >
You can navigate this site via the keyboard. There are two modes:
General navigation, when search is not focused:
- F , S , / : open search dialog
- P , , : go to previous page
- N , . : go to next page
While using the search function:
- Down , Up : select next / previous result
- Esc , Tab : close search
- Enter : go to highlighted page in the results