Skip to content

Instantly share code, notes, and snippets.

@JoshLmao
Last active May 16, 2024 18:37
Show Gist options
  • Save JoshLmao/a71e10d70c88b1c23418f20bcab26977 to your computer and use it in GitHub Desktop.
Save JoshLmao/a71e10d70c88b1c23418f20bcab26977 to your computer and use it in GitHub Desktop.
🎮 UE4 C++ Finite State Machine - Basic Structure Example
#include "UE4_FSM_Structure.h"
// Sets default values
AUE4_FSM_Structure::AUE4_FSM_Structure()
{
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AUE4_FSM_Structure::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AUE4_FSM_Structure::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
FSMUpdate();
}
void AUE4_FSM_Structure::FSMUpdate()
{
// List all expected states and call relevant state functions
if (State == GameStates::IDLE)
{
if (Event == GameEvents::ON_ENTER) {
Idle_Enter();
}
if (Event == GameEvents::ON_UPDATE) {
Idle_Update();
}
}
if (State == GameStates::RETREAT)
{
if (Event == GameEvents::ON_ENTER) {
Retreat_Enter();
}
if (Event == GameEvents::ON_UPDATE) {
Retreat_Update();
}
}
// Append any GameStates you add to this example..
}
void AUE4_FSM_Structure::SetFSMState(GameStates newState)
{
// Append any GameStates you add to this example to this switch statement...
switch( State )
{
case GameStates::IDLE:
Idle_Exit();
break;
case GameStates::RETREAT:
Retreat_Exit();
break;
default:
UE_LOG(LogTemp, Error, TEXT("Unexpected state has not been implemented!"), newState);
return;
}
// Set new GameStates state and begin OnEnter of that state
State = newState;
Event = GameEvents::ON_ENTER;
}
void AUE4_FSM_Structure::Idle_Enter()
{
// Change to GameEvents to Update when called
Event = GameEvents::ON_UPDATE;
}
void AUE4_FSM_Structure::Idle_Update()
{
// Called once a frame when in the IDLE GameStates state
// Implement functionality for Idle...
}
void AUE4_FSM_Structure::Idle_Exit()
{
// Implement any functionality for leaving the Idle state
}
void AUE4_FSM_Structure::Retreat_Enter()
{
// Change to GameEvents to Update when called
Event = GameEvents::ON_UPDATE;
}
void AUE4_FSM_Structure::Retreat_Update()
{
// Called once a frame when in the RETREAT GameStates state
// Implement functionality for Retreat...
}
void AUE4_FSM_Structure::Retreat_Exit()
{
// Implement any functionality for leaving the Retreat state
}
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "UE4_FSM_Structure.generated.h"
UCLASS()
class EXAMPLE_API AUE4_FSM_Structure : public AActor
{
GENERATED_BODY()
public:
AUE4_FSM_Structure();
protected:
virtual void BeginPlay() override;
public:
virtual void Tick(float DeltaTime) override;
private:
enum GameStates { IDLE, RETREAT };
GameStates State = GameStates::IDLE;
enum GameEvents { ON_ENTER, ON_UPDATE };
GameEvents Event = GameEvents::ON_ENTER;
void FSMUpdate();
void SetFSMState(GameStates newState);
void Idle_Enter();
void Idle_Update();
void Idle_Exit();
void Retreat_Enter();
void Retreat_Update();
void Retreat_Exit();
};
@JoshLmao
Copy link
Author

An example of how to implement a basic finite state machine inside Unreal Engine in C++. This method uses enumerators to control the flow and make it simple to add on or remove parts of your FMS.

Any questions, hit me up on Twitter: 🐦@JoshLmao

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment