I have a Finite State Machine (FSM) and I want to perform an action when a state is entered/set for the first time, and only that first time. Otherwise, I want a different set of actions to run each time the state is active.
For example:
When variable 'state' = 'falling':
Trigger once:
[Initial action]
[All subsequent actions]
Using ‘Trigger once’ doesn’t work though in this case. I’ve previously used a separate boolean variable to set when a state is first changed, for example:
[Some other code]:
Set variable 'state' = 'falling'
Set variable 'state_changed' = true
When variable 'state' = 'falling':
When variable 'state_changed' = true:
[Initial action]
Set variable 'state_changed' = false
[All subsequent actions]
But it’s not ideal since you need a separate variable. So what I’ve just started doing is setting a slightly different state initially, then changing it to the proper state after the first run:
[Some other code]:
Set variable 'state' = 'falling_initial'
When variable 'state' = 'falling_initial':
[Initial action]
Set variable 'state' = 'falling'
When variable 'state' = 'falling':
[All subsequent actions]
It’s a bit hackish I guess (since ‘falling_initial’ technically shouldn’t be a separate state), but it works and doesn’t require additional variables.
What do others use in this situation? Any better ideas?