digint / tinyfsm

A simple C++ finite state machine library
https://digint.ch/tinyfsm
MIT License
952 stars 174 forks source link

Can we have multiple instances of the same Fsm? #3

Open heLomaN opened 7 years ago

heLomaN commented 7 years ago

Since declare typedef F const * state_ptr_const_t; in class Fsm, we cannot get more than one instance for a specific Fsm. Is there any way to avoid this?

digint commented 7 years ago

As you state correctly, Fsm and states are all static instances of template types. This means that it's not possible to have more than one instance for as specific Fsm.

It might be possible with some template metaprogramming magic, and I remember doing some experiments with this, but it became pretty complicated, so I dropped it (as one main goal of tinyfsm is to keep it simple).

digint commented 6 years ago

Here's how you can have multiple state machines of the same type, declaring a template state machine: examples/api/multiple_switch.cpp.

pgruenbacher commented 6 years ago

I was going to use this library for doing fsm for like thousands of AI bots... this no longer seems viable if I there's not a way to instantiate a Fsm object for each bot. (Novice c++ learner so not positive if there's a workaround).

pgruenbacher commented 6 years ago

hm nvermind i'll just be using https://www.boost.org/doc/libs/1_64_0/libs/msm since it matches more closely what I was needing in terms of functionality.

01e9 commented 6 years ago

Same problem. I wanted to use this library for a websocket server - websockets as state machines. I came across Boost.SML.

martinerk0 commented 1 year ago

@digint Can I ask why is it this way, instead of FSM being class that you can create multiple instances of?

ekigwana commented 11 months ago

Man this bit me this week. I need multiple instances of the same machine. There really needs to be an example of how to do that. Even for the basic case it took me a second to figure out that I have to use namespaces to avoid issues due to unreleated FSMs. Now I want n FSMs of type B and I just get crashes. Fun