Open utterances-bot opened 1 year ago
"Here’s a basic diagram with states and transitions:"
I think a gremlin has got in and eaten it...
PlayerAlive and PlayerDead both have a member remainingLives_. Couldn't PlayerAlive be derived from PlayerDead and thus only have remainingLives once? The living arising from the dead!
thanks @2kaud! I've just fixed the state diagram, The states are just flat structures, but we can improve them and derive if needed.
Since this is a game performance would be important. The std::variant version is using a number of library functions which the compile may not be able to optimize as well as the enum version.
Besides one disadvantage you already mentioned (size being max size of all events) - the other disadvantage is the increase of compilation time. For big codebases with 100s of events - it can be a problem - comparing to e.g. classic GoF state pattern with event being represented by base class and all its variants being only forward declared. But maybe with C++20 modules it can be not so much visible.
Another plus for the variant implementation, is that it's easy to test the individual onEvent() functions.
Finite State Machine with std::variant - C++ Stories
In this blog post, I’ll show you how to convert a “regular” enum-style finite state machine into a modern version based on std::variant from C++17. This technique allows you to improve design, work with value types and enhance code quality. States Let’s start with a basic example: we want to track a game player’s health status we’d like to respond to events like “Hit by a monster” or “Healing bonus.
https://www.cppstories.com/2023/finite-state-machines-variant-cpp/