FSM for game state flow

Using a BP Interface

Step 1:
Create your Blueprint Interface file, and add all of the states you need as functions. I’ve abstracted it down to A B and C to keep it generic, but a meaningful name is useful. At this point, you should also have a matching enum file.

d2cb5b152a.png

d2cb5b152a.png

Step 2:
Go to your GameMode BP and make a public variable of your enum. (This doesn’t have to be your GameMode BP, you could use GameState or anything else, as long as you can reference it. ) Now you have a way to find the current state from anywhere.

Step 3:
Attach your Blueprint Interface to every Blueprint that needs to listen for state changes. If you can’t find the place, you have to select “Class Settings” and then select from the “Add” dropdown in the Interfaces section.

1cb851cea0.png

Step 4:
Go back to your GameMode BP (or whatever you’re using), and set up as following:

1cb851cea0.png

This will update the enum whenever a state has been triggered.

Step 5:

This step is more of a convenience, but at this point I like to make a macro for querying the current state, so that I don’t have to do it manually each time I need it.

00740c00b1.png

Step 6:

At this point, you’re pretty much set up to use your Interface system how you want to. In this example, we have the Player controller listening for each state change. We’re also using the macro we made earlier to bind Left Mouse Button to different actions depending on the current state.

Finally, the event on the right is illustrating how you can trigger state changes. Using that method, the signal gets sent to every Blueprint that’s listening for it (all of the blueprints that are attached to the interface).

Note: A BP doesn’t need to be attached to the interface in order to send the state change signal, it only needs to be attached to receive it.