Seems like you’re understanding it right.
Note that a “state”, as far as your FSM is concerned, can be as simple as one or more “settings” that define a thing. If we are talking about a door, we could say that it has two states: open and closed. For this simple example, we could define both states in code using the same variable, doorIsOpen = true
or doorIsOpen = false
.
The ‘finite’ part means that we have a limited number of these states. In the door example, it could technically be said that there are an infinite number of positions the door can take between open and closed. The FSM requires that we have something other than an infinite number of states. Due to the way FSMs are implemented, it’s generally true that the number of states you want will be equal to the fewest number of states you can get away with and still accomplish what you set out to do.
The ‘machine’ part is descriptive of the way that FSMs switch between these states based on inputs. That would involve the specifics of how we get from an open-door state to a closed one and vice versa—the mechanics.
So, yeah. In a sense, an FSM could be used for many situations where you can create a set of related states and define rules for how and when to transition between them. When implementing this in code, it’s convenient to use classes (OOP). Each state can be based on the same template, so the code that utilizes the states doesn’t have to care which state is active. For example, all the states may have methods like :enter(), :exit(), :update() and so on that have the same names but contain code that is relevant to the particular state. Within a method like the update, there may be code that just carries out the actions of the state. For example, if we’re now talking about a foraging state for an NPC, then the update will direct it to do, or continue to do, foraging things. That update code could also run checks that might indicate that the NPC needs to switch to a new state, and it could then initiate a state transition (rest or fight or whatever).
Since managing transitions is done in each state, each additional state you add increases the number of transitions you have to handle in your classes (since transitions could potentially be happening in all directions). That can become unwieldy and limits the usefulness of FSMs to situations with relatively few states. On the flip side, FSMs can be somewhat of a hassle to implement, which makes them unsuitable for simple situations. That small range of usefulness tends to make FSMs less common than one might expect.
FSMs sit between the simpler solutions like if-elseif conditionals and decision trees and more complex options like behavior trees, that might scale better.