Open antongit opened 3 years ago
As a follow up from matrix: Could you add this as a Helppatch? HowTo build a state machine.
If one doesn't already know about it it's pretty hard to find.
Hi @antongit, @gregsn, In an attempt to build upon you've already done I started making a Statmachine patch with a little demo in the spirit of your own. The main issues I tried to answer were:
It isn't finished though: I need to figure out how to make the state graph aware of new nodes. I'll setup a repo for it sometime. Feedback welcome,
H
Hi, here is an updated version that works with latest gamma 6.6.
There were some changes regarding VL Type reflection that broke the previous version.
Introduction
This is a patching pattern and a small library for creating and running State Machines' graphs directly in VL. Related to the Quest #8.
For a long time I was using external Tools like QFSM and contributions like u7angel's amazing Automata UI. But I always thought - how cool will it be to make State Machines directly in VL, without the need for any external editors. We already have nodes and links, why do we need anything else?
And then, I came up with a very simple solution. @gregsn has stripped it down even more and cleaned it up.
This solution is made in plain VL, no magic involved. I hope one day vvvv's UI will support it a bit, reducing some copy-pasting and increasing readability.
The lib was already used in two production-grade projects.
Usage scenarios
The machine can be used in two different ways:
Basic (classic) States are just defined by their names (strings) and the rest of the system is listening to the active string.
Advanced States themselves can contain many different properties, operations and even complete apps, that can run simultaneously or only when the state is active.
How it works
States are represented by
Processnodes
. Transitions between states are represented bylinks
assigned tooperations
. To trigger a transition from one state to another a corresponding operation is called. An operation can be called directly (by placing a node) or by sending a string with an operation's name.How to use it
The lib will be provided as a nuget that should be referenced by your document. For now it's just a single
VL.Statemachine.vl
document and some examples.See the provided example patches. A video tutorial is coming soon. Here is the how to in written from.
Basic usage
VL.Statemachine.vl
from your document (for now, later it will be a nuget).VL.Statemachine.vl
, navigate toDefinitions > .Templates.Internal
, copy theStateTemplate
into your document and rename it. This is one of your states.processnodes
of your states in it.StateTemplate
pre-defines 4 inputs, enable/disable them in theProcess node
configuration in thePatch Explorer
. If your need more inputs, just create another copy of one of the inputs and assign it to a new operation.SetAsInitialState
in itsProcess node
configuration).State Manager
to your state machine.SendEvent
.State machine class with states and transitions
State Machine in action
One of the states with two inputs enabled
Advanced usage
In order to provide properties, operations and/or whole
processnodes
to the states:IState
and add Operations/Inputs/Outputs to it.IState
).GetState
of theStateManager
to get the active state and cast it to your new interface.A new Interface
Main state is implementing the new interface
Accessing the active state
Video presentation
The pattern was presented at 13. Worldwide vvvv Meetup (starts at about 2:09): https://youtu.be/2gGPh84y-mU?t=129
Library
VL.Statemachine.zip
Conclusion
This proposal can be seen as a ready-to-use library and as general patching pattern open to any modifications / customizations.
Also inspired by: