09 - AI Behavior
An AAIController subclass that detects a player within a sight radius using a radar check (FVector::Dist), fires a multicast alert, and tracks a current target.
Full source
include "CoreMinimal.h"
include "GameFramework/AIController.h"
include "TimerManager.h"
uclass AExampleAIController : AAIController meta: api=EXAMPLES_API
{
prop public float SightRadius EditAnywhere BlueprintReadWrite category="Example" default=1000.0f
declare DynamicMulticast FOnAlert (OneParam AActor*, Target)
field public FOnAlert OnAlert
field private AActor* CurrentTarget
method public void SensePawns(float DeltaTime, FVector Origin, const TArray<APawn*>& Pawns) {
for (APawn* P : Pawns)
{
if (P && FVector::Dist(Origin, P->GetActorLocation()) <= SightRadius)
{
OnAlert.Broadcast(P);
CurrentTarget = P;
}
}
}
method public virtual void BeginPlay() override {
Super::BeginPlay();
}
}
Line by line
uclass AExampleAIController : AAIController- an AI controller (drives a pawn).prop public float SightRadius ... default=1000.0f- how far the AI can "see".declare DynamicMulticast FOnAlert (OneParam AActor*, Target)- a multicast alert carrying the detected actor.field public FOnAlert OnAlert- the broadcastable member.field private AActor* CurrentTarget- the AI's current focus (private).SensePawns(...)- iterates the pawns; for any pawn withinSightRadius, broadcastsOnAlertand setsCurrentTarget.method ... for (APawn* P : Pawns)- note the range-basedforpasses through verbatim - TPScript doesn't need to understand every C++ construct, it just emits your body.
Networking-adjacent patterns
- Multicast delegate for "AI spotted something" that other systems (HUD, other AI, quests) can listen to without polling.
- Event-driven instead of tight-coupling: the alert emits and anyone bound reacts.
Extending
In a real AI, you'd call SensePawns from a perception/overlap event or a tick, then use CurrentTarget + MoveToActor (from AAIController) to chase.
Next: 10 - UI HUD.