Making a FSM in C++

I am currently experimenting with making GOAP AI and I need to make a FSM. I came from Unity a while back and used state machines there to handle player states. I am trying to translate this example of GOAP from C# to C++ in UE4.

Website: Goal Oriented Action Planning for a Smarter AI
Source Code: goap/Assets/Standard Assets/Scripts/AI at master · sploreg/goap · GitHub

The questions I have are as followed:

  • What class should a FSM be made with? (Empty, Actor Component, or Game State)
  • Should the FSM be attached to the Pawn it will control or should it be referenced by that pawn?
  • How should I implement the states? (switch statement, if statements)

If anyone could answer 1 or all of these questions or even point in the direction to some useful info, would be greatly appreciated.

Thank you,
Russ.

I think you could use blackboards from original UE4 AI system as state machine. Here docs for AI

https://docs.unrealengine.com/latest/INT/Gameplay/AI/index.html

I didn’t play with so not sure how that exacly works. I only know you end point of whole AI system is AIController (that operates the same as PlayerController) and that controller controls Pawn. Someway or another you could plug your AI library to AIController and use blackbords for states, so studu UE4 AI system and override it in right way

That isn’t really what I want to do. I’m trying to do this in C++ as a learning exercise.

I think my biggest roadblock is getting information on how to properly implement a switch statement or stack and what class the FSM should be. I think it should be an actor component. I’m finding it hard to find any info on these subjects that isn’t blueprint related.

I explain to you how AI works in UE4, if you want to fit any AI system in UE4 you should go in similar route or else you end up rewriting everything. Anything that controls pawn should be in controller, controller is a soul of a pawn. As for state machines, blackboard is a only one that i know in UE4.

Just so you know, this is an enormous question (the realm of possibilities for answers are endless). I’ve done exactly what you’re asking about. I really dislike Unreal’s BT implementation to the point I think it’s a detriment to any game that has any kind of complexity to use it. It does however, handle a lot of things that trip up programmers nicely so it’s risky for beginners to do their own.

That said, people normally put behavior code in a controller. Pawns link to controllers (If you have a character selected in the Editor, scroll down on the properties to “Pawn” and you’ll see where you link to a controller). This way, one character can have any number of behaviors.

If you’re starting out, then I would start with a switch statement for the different states. Over time, that’ll get cumbersome as complex character can end up with dozens of states (I’ve seen hundreds too). I’d keep the switch cases short. Make a function for each state that returns some sort of status. Putting a lot of code within the cases becomes hard to manage quickly.

Later on, you’ll want to use some sort of state manager rather than a switch statement.

That is some great help sir.

I need a little clarification though. You say to attach a pawn to a controller which I think I found in editor(Posses by AI controller). Is this necessary or can I create AI without it? If I made an FSM as an ActorComponent class and attached it to my Pawn would that work?

I plan on uploading my finished code to help other people who have the same question. The C++ documentation is pretty lacking compared to BP.

Edit: After digging through the source files I see that I can make a custom AI controller class. It will be a lot of work but also a great learning experience and possibly even reduce the amount of code the engine reads during runtime. I may not need many of the functions in the existing AI controller.

I’m marking this question answered. I would still like to hear your thoughts on a custom AI Controller or making an FSM an ActorComponent class.

Thank You for the help.

I see so it sounds like it will be a bit of work on my end.

My whole purpose for this is to learn and experiment I definitely see how the standard controller would save on time. I love to code and doing things the “hard way” is a great learning exercise that will help me understand UE4 that much more.

Thank you again

You can put it in an actor component if you want. Most people put it in the controller simply because that’s its intended purpose, it gives you the ability to switch behaviors during the game, it gives you the ability to run different behavior trees, and the controller already has code to affect the behavior of the character it controls. For example, UpdateControlRotation controls the orientation of the character, and controllers are tied to the path finding system. One character can use many controllers and/or one controller can be used to control many characters.

I don’t think you have to override the Possess function. Possession will happen automatically when you set it up in the editor (when you select your character in the editor scroll down the Details panel to the ‘Pawn’ section and enter your controller class in “AI Controller Class”). You can also set up the controller in the class defaults of your character blueprint. You can switch controllers at any point (through the Possess and Unposess functions).

You can leave the AI Controller Class empty and make your behavior in the Actor component if you want though. It’s less flexible, but you know your needs better than anyone on the internet.