Custom CPU Brains
The brain is the class that decides what the CPU does each frame. The default, the Deck Brain, plays the tactics of the CPU Profile, and covers most needs. Write your own brain for a boss with scripted patterns, a tutorial opponent, a special story fight, or an AI of a different kind.
| Base class | Use |
|---|---|
AFT CPU Deck Brain (UAFTCpuDeckBrain) | Keep the tactics of the profile and change only some cases (call Run Default Brain for the rest), or add custom conditions. |
AFT CPU Brain (UAFTCpuBrain) | A fully custom AI. The tactics are then only used if your brain reads them. |
UAFTCpuSimpleBrain | Compact C++ example (the previous AFT AI): guard on reaction, random reachable command, footsies. It ignores the tactics; only the levels are read. |
Pick the class in the profile's Brain (CPU Editor > Profile & Levels), then set its settings below it: the brain is an instanced object of the profile, so two profiles can use the same Blueprint brain with different values. A child profile with an empty Brain uses its parent's brain.
Determinism rules
The CPU runs inside the deterministic simulation: the same inputs must give the same decisions on every machine, and a rollback replays the frames again. A brain must:
- read the fight through the context only (perception, fighters, actions);
- draw random numbers with the context's Random functions only, never
FMath::Randor Blueprint Random nodes; - keep what it remembers between frames in the context's Memory (16 integers, rolled back with the state), never in Blueprint variables or C++ members. The brain object of a profile is shared by every CPU playing it and read-only during the fight: its variables are settings.
- use integers (the perception is in integers) and the deterministic AFT Fixed nodes for the math, never Float math nodes, whose result can differ between two machines.
See Writing Deterministic Code.
Blueprint brain
- Create a Blueprint class whose parent is AFT CPU Brain (or AFT CPU Deck Brain to keep the tactics).
- Add its settings as variables and tick Instance Editable: they are edited per profile, in its Brain details.
- Override the On Think function (My Blueprint > Functions > Override). It runs every frame of the fight for the CPU team, and again on every frame the simulation plays again (rollback), with an AFT CPU Context. It is a const function: it reads its variables but can't change them.
- Pick the Blueprint as the Brain of a CPU profile and set its variables.
Most brains only decide when the CPU is free: start On Think with a branch on Is Busy.
The template has an example, /AFT/Data/Cpu/BP_AFT_CpuBrain_Approach (child of the Deck Brain): when Get Perception gives a
Distance above its Approach Distance variable (250 cm by default) and the CPU can act, it holds forward this frame
(Hold Direction); otherwise Run Default Brain plays the tactics of the profile.
| Context function | Description |
|---|---|
Get Perception | What the CPU sees this frame (FAFTCpuPerception): distance, height difference, health and meter percentages, frame advantage, distances to the walls, round time, incoming projectile, opponent threat range, pressure, combo count, own and opponent states (airborne, crouching, attacking, recovering, stunned, knocked down, approaching), opponent action category and guard type. |
Get Level, Get Level Settings | The CPU level (1 to 8) and its settings. |
Get Fighter, Get Opponent Fighter | Fighter data of the CPU and of its opponent. |
Get Current Action, Get Opponent Action, Get Opponent Action Frame | Current actions, and the frame of the opponent's action. |
Is Busy | Inputs are still queued. |
Can Perform Action, Get Action Reach | The action could start now; its horizontal reach in cm (99999 for projectiles, 0 without hitbox). |
Perform Action | Inputs the command of the action, motion included. With Allow Mistakes, the level execution applies. |
Hold Direction, Press Buttons | Holds a numpad direction (relative to the facing) and buttons, presses buttons, for some frames. |
Block | Guards this frame (Auto, High, Low): call it every frame while blocking. |
Wait, Dash Forward, Backdash, Clear Orders | Movement and queue control. |
Random Range, Random Chance | Deterministic random numbers. |
Get Memory, Set Memory | Memory slots 0 to 15, rolled back with the simulation. |
Run Default Brain | Deck brains: plays the tactics of the profile this frame. |
Example: a boss that always anti-airs. Subclass AFT CPU Deck Brain and, in On Think:
- If Is Busy is true, return.
- Get Perception: if Opponent Airborne is true, Distance is below 200 and Random Chance (60) succeeds, call Perform Action with the boss uppercut and return.
- Otherwise call Run Default Brain: the tactics of the profile play as usual.
Custom conditions
A tactic condition of type Custom is evaluated by the brain of the profile: override Evaluate Custom Condition
(Blueprint or C++) in a Deck Brain subclass. It receives the condition's Custom Name and the perception, and returns whether
it passes. For example, LastStand could pass when Own Health is below 20 % and the opponent is cornered. Without an
override, a custom condition fails.
C++ brain
Derive from UAFTCpuBrain (or UAFTCpuDeckBrain) and override Think. Your module needs the AFTCore dependency.
// BossBrain.h
#pragma once
#include "Cpu/AFTCpuBrain.h"
#include "BossBrain.generated.h"
class UAFTActionAsset;
/** Anti-airs every jump with its uppercut, plays the tactics of the profile otherwise. */
UCLASS(Blueprintable, meta = (DisplayName = "Boss Brain"))
class UBossBrain : public UAFTCpuDeckBrain
{
GENERATED_BODY()
public:
/** Set per profile, in its Brain details. */
UPROPERTY(EditAnywhere, Category = "Boss")
TObjectPtr<UAFTActionAsset> AntiAir;
virtual void Think(FAFTCpuContext& Context) const override;
};
// BossBrain.cpp
#include "BossBrain.h"
#include "Sim/AFTBattleSimulation.h"
void UBossBrain::Think(FAFTCpuContext& Context) const
{
const FAFTCpuPerception& P = Context.Perception;
int32& LastAntiAirFrame = Context.Memory(0); // rolled back with the simulation
if (!Context.IsBusy() && P.bOwnActionable && P.bOpponentAirborne && P.Distance < 200
&& P.Frame - LastAntiAirFrame > 30 && Context.Random().Chance(40 + Context.GetLevel() * 7))
{
if (Context.PerformAction(Context.FighterData.FindAction(AntiAir), /*bAllowMistakes*/ true))
{
LastAntiAirFrame = P.Frame;
return;
}
}
Super::Think(Context); // On Think if a Blueprint child implements it, else the tactics
}
FAFTCpuContext gives:
| Member | Description |
|---|---|
Perception, LevelSettings, Profile, FighterData | What the CPU sees, its level settings, the compiled profile (tactics sorted by situation and priority) and the compiled fighter. |
GetSelf(), GetOpponent(), GetOpponentData(), GetLevel(), GetFacing() | Fighter states and data. |
Random(), Memory(Index) | Deterministic random numbers, and the rolled back memory. |
FindCommand, CanPerformCommand, GetActionReach, GetCommandInputFrames | Move list queries. |
IsBusy, ClearOrders, PerformCommand, PerformAction, HoldNumpad, PressButtons, Wait, DashForward, Backdash, GetGuardBits | Orders, played by the executor over the next frames. |
SetRawInput | Input bits for this frame only, bypassing the executor. |
ReportDecision | Sends a decision to the debug sink (CPU Editor simulation, tests). |
UAFTCpuDeckBrain::RunDeck (the deck logic) is virtual too, and DetectSituation, IsReactive and IsDefensive are public
static helpers. UAFTCpuSimpleBrain::Think in AFTCpuBrain.cpp is a complete small brain to start from.
The AFT.Core.Cpu.CustomBrain test plugs a C++ brain through a profile and checks that it drives the inputs, that its memory
is rolled back, and that its motion comes out exactly. AFT.Core.Cpu.BlueprintBrain does the same with the Blueprint example
(sync test on every frame, a second simulation, a rollback replay).