Comments (4)
State IDs are 0-based, steadily increasing for every (including empty) state in the hierarchy.
They can be queried using static StateID stateId<TState>()
function available on all Root
s, Control
s and Plan
s.
from hfsm2.
Thanks for responding so quickly to my request.
Sorry, i'm not native English, I‘m not sure if I have expressed it correctly.
let's example basic_audio_player
int
main() noexcept {
// construct everything
Context title;
Logger logger;
FSM::Instance machine(title, &logger);
// do the work :)
machine.react(Play{"any"}); // Idle -> Playing
machine.react(Stop{}); // Playing -> Idle
if (machine.isActive<Idle>()) {
// do something
} else if (machine.isActive<Playing>()) {
// do something
} else if (machine.isActive<Paused>()) {
// do something
} else {
// do something
}
/*
switch (machine.current_state_id()) { // my stupid idea
case FSM::stateId<Idle>(): {
// do something
break;
} case FSM::stateId<Playing>(): {
// do something
break;
} case FSM::stateId<Paused>(): {
// do something
break;
} default:
// do something
}
*/
return 0;
}
So, what is elegant approach that implement switch (machine.current_state_id())
.
Thanks again,
Kiwi
from hfsm2.
- A hierarchical FSM might or might not have only one active state. E.g. in test_internal_transitions, when
B_1_1
is active,B
andB_1_2
are also active.
Flat state machines do though, and FFSM2 does haveStateID Root::activeStateId()
. - The case you're describing is what we can call 'a passive FSM', i.e. when external-to-FSM code is checking its current configuration and does something based on that (let's call it a passive FSM).
Alternatively, I prefer using a different paradigm (active FSMs), by moving the logic dependent on which state is active into the state methods.
I.e. the logic from theif
statement in your example above I'd move into anotherreact()
method for all relevant states:
struct DoMoreWork {}; // new event
struct Idle : Base {
void react(const DoMoreWork& event, FullControl& control) {
// do more work here
}
};
struct Playing : Base {
void react(const DoMoreWork& event, FullControl& control) {
// do more work here
}
};
struct Paused : Base {
void react(const DoMoreWork& event, FullControl& control) {
// do more work here
}
};
int main() {
// ...
machine.react(DoMoreWork{}); // more work gets done here
}
from hfsm2.
I got it now, thank you
from hfsm2.
Related Issues (20)
- Reacting to events deeply HOT 2
- Compile error in B_<TFirst>::stateId() HOT 3
- The logger can freeze due to unknown methods HOT 5
- Only one headless region is allowed in an FSM instance HOT 1
- Trouble with HFSM2 master version in MSVC2019 16.6.5 HOT 9
- Can't get Context from Control in Utility function HOT 2
- Tracking use in Godot Engine HOT 14
- Usage within ecs systems like entt HOT 2
- Using in embedded systems HOT 3
- Port travis to Github Actions HOT 3
- Looped plan HOT 2
- State machine stuck in update method HOT 3
- Macro conflict between sys/ioctl.h and hfsm2/machine.hpp HOT 4
- thread-safe?
- Fetching payload contents HOT 2
- Broken links in doc HOT 1
- Does it support writing the states in different files? HOT 1
- State members? HOT 2
- Any example about "Hierarchical" ? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hfsm2.