Cheatsheet
A simple guide through the concepts behind solstack.
Core Concepts
There's really only three things you should care about in solstack
: State
s, Tran
s and the Stack
.
A State
represents what your program should run at that moment. A MainMenu
state does not render the player in a game; it renders options and pushes other relevant states on top of itself if the correct inputs are provided, or even quits the application entirely.
Take a look at the previous chapter Use Case for understanding a typical application flow in
solstack
.
A Stack
is a type that holds a list of State
s. When the stack's tick
method is called, it looks for the topmost state it holds and executes its methods (namely on_tick
and on_shadow_tick
). This way, only the topmost state in a stack is run at a time; while the others are effectively paused
.
In v0.3.0 we introduced the concept of a
shadow_tick
. TheStack
now, when ticked, will execute the methodon_shadow_tick
of every single state it holds. It can simply be ignored, of course.
A Trans
represents a transition between State
s on a Stack
. You can perform this directly through the Stack
s methods (namely push
, pop
, quit
, none
, etc.). But the most useful way is through returning a Trans
on the State
s on_update
and/or on_shadow_update
. Returning allows you to easily code conditional transitions, like if the player presses Esc
, push the PauseMenu
state.
Modeling an Application
Bring the important stuff into scope.
Create an application data type that will hold important information. This will be shared mutably by all of your states.
Create some states.
Implement
State
for your states.
Your IDE might bring the
data
parameter on the methods as_data
. This tells rust it's not currently being used. If you're going to use it, change it dodata
.
All of the methods are documented below in Callbacks and in the crate's docs.
All of the
Trans
itions are documented below in Transition and in the crate's docs.
Instatiate your data and a new stack; set up a loop.
If you are goin to use
is_running
, make sure that eventually at least one of your states requests for aTrans::Quit
. Otherwise you'll have an infinite loop. If you need your ticks to be called, for instance, 60 times/frames per second, you'd do that logic here. Thesolstack
lib won't opinate on how or how often totick
, thoughfeature
s are planned for that.
Callbacks
on_start
no
on_stop
no
on_pause
no
on_resume
no
on_tick
yes
on_shadow_tick
NOTE: on_shadow_tick
is called BEFORE on_tick
.
yes
The methods marked as
TRANS
requires returning aTrans
(see how at the chapter below Transitions.
Transitions
Push(Box<State<D>>)
stack.push
stack_push!
trans_push!
Trans::Push
Pushes a new state above the current one. Effectively pauses the current state until everything above it is popped.
Pop
stack.pop
stack_pop!
trans_pop!
Trans::Pop
Pops the current state from the stack.
Replace<Box<State<D>>
stack.replace
stack_replace!
trans_replace!
Trans::Replace
Pops and pushes a new state. Effectively replaces the current state with a new one.
Isolate<Box<State<D>>
stack.isolate
stack_isolate!
trans_isolate!
Trans::Isolate
Pops every state from the stack and pushes a new one. Effectively isolates it as the only state on the stack.
Quit
stack.quit
stack_quit!
trans_quit!
Trans::Quit
Pops everything from the stack. Effectively ends the state stack machine.
None
trans_none!
Trans::None
Does nothing to the stack. Effectively keeps the current state and the stack the way it is.
Stack Ticking (Not a Transition)
stack.tick
stack_tick!
Ticks the current (topmost at the stack) state once and performs any necessary transitions. Also ticks every state's shadow tick and performs their transitions.
Last updated