Two state machines can affect how a function is handled. Any techniques to deal with this situation? – General programming and gameplay

0

Hey guys, first a caveat – I’m new to programming, all my experience is with Unreal blueprints, and I may use some technical terms incorrectly. I will try to give concrete examples to avoid any confusion.

I don’t have a progression blocking issue, I just see a situation where I feel like there’s a combat test technique to handle things more efficiently than I know. Maybe experienced programmers can offer advice?

The situation involves too much code to show – I tried to condense it with a simple graphic below.

There are two State Machines in game. GameState observes what is happening in the game, records data about it, and so on.
PlayerState is used to determine how inputs will be handled and what UI elements will be displayed.

An example is that when you are in PlayerState:Movement and you press W, the character will move forward (first person game). When in PlayerState:UI and pressing W, the current UI menu will move the selection up one item.

PlayerState monitors GameState because when GameState changes, in most cases PlayerState should also change. For example, when we enter GameState:PreGame, it’s like a countdown where for 5 seconds the player can’t move, so it goes into PlayerState:InputLocked.

Now you can imagine that if PlayerState is UI and GameState is InGame, ESC can disable a pause menu. But if it’s the same PlayerState but GameState is Menu, then ESC shouldn’t disable the main menu – it would have a different functionality.

So there’s this situation where GameState + PlayerState can create Additional states that are not defined. A bit like a multiplication process taking place.

The simple and obvious solution is to just do a Switch statement using GameState as the enumerator over all the input events that need to reflect it. But it has no modularity and it’s going to be hard to maintain. I find code with lots of conditional checks like this very hard to reason with, so I try to avoid it.

So, have you ever seen a problem like this? Is there a proven method to deal with it? Could you set things up completely differently? Maybe I’m abusing these state machines?

Any advice is appreciated, thank you.

(about the image: the arrows indicate that the source causes the target to change. For example, when GameState: Menu is entered, then PlayerState: UI must also be entered.)

Share.

Comments are closed.