Finite State Machine with std::variant -- Bartlomiej Filipek

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.

Finite State Machine with std::variant

by Bartlomiej Filipek

From the article:

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.”
  • when the health point goes to 0, then we have to restart the game only if there are some remaining lives available

Here’s a basic diagram with states and transitions:

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.