I am making a multiplayer game. But this question is all local. I am using hierarchical state machines for the player objects. So each behavior is its own class deriving from a class PlayerState. The players have a direct reference to two main states ‘Battle’ and ‘Build’. Those states each have references to their child states.
I would like to create a game manager that can put all the players on a client into either Battle or Build state when the client joins the game. To do this, I will have to create a function that essentially does something like this:
void UpdateAllPlayerStates(PlayerState someState)
{
foreach (Player player in playerList)
{
player.statemachine.SetState(someState)
}
}
Now here is my question. I can’t just pass any PlayerState into this, because the top of the state machine only can take Battle or Build. This won’t be a problem if I simply think carefully about how I use the function. But it makes me wonder, how can I limit the inputs here to make sure it only accepts certain classes that are derived from PlayerState?
So in fake code, I’m looking for something like this:
void UpdateAllPlayerStates(PlayerState someState) where someState is of type Battle or Build
{
foreach (Player player in playerList)
{
player.statemachine.SetState(someState)
}
}