AI Commands - NEW!
AI Commands
AI Commands are a basic command pattern implementation to let AI implement custom actions. In depth tutorial: https://www.youtube.com/watch?v=xEr1nRDLB8c
In the Ascent Combat Framework (ACF), the Command System provides a structured way to issue and manage commands for AI-controlled characters. This system is designed to handle high-level AI behaviors such as such as following, reaching a place, healing etc.
The framework ensures that commands are executed in a networked environment, making use of Unreal Engine's Server-Client replication system. This document will guide you through the process of creating, adding, and executing commands for AI within ACF.
This document explains how to create, execute, and manage AI commands using:
UACFBaseCommand(Base class for custom AI commands).UACFCommandsManagerComponent(Handles execution and queuing of AI commands).
Understanding the Command System
The ACF Command System is built around GameplayTags, which are used to represent different commands that AI can perform. These commands are managed and executed through specific functions, ensuring reliability and validation on the server-side.
Key Components
Here are the core functions provided in the Command System:
TriggerCommand(const FGameplayTag& command): Sends a command request to the AI.ExecutePendingCommand(): Processes and executes the next command in the queue.TerminateCurrentCommand(): Cancels the current AI action.HasPendingCommands(): Checks if the AI has pending commands in the queue.OnCommandFinished: Event that fires when an AI completes a command.Key Components
Function
Description
ExecuteCommand(AACFAIController* controller)Starts execution of the command (to be implemented in child classes).
EndCommand(AACFAIController* controller)Handles cleanup and final actions when the command ends.
FinishExecution(bool bSuccess)Marks the command as completed and triggers the
OnCommandFinishedevent.GetAIController()Retrieves the AI controller executing the command.
GetWorld()Provides access to the game world for interaction.
Additionally, the
OnCommandFinisheddelegate notifies the Command Manager when execution is complete.2. Understanding
UACFCommandsManagerComponent(The Command Processor)The
UACFCommandsManagerComponentis responsible for:Queuing and executing commands for an AI.
Ensuring commands are processed in the correct order.
Handling command completion events.
Working in sync with ACF AI Behavior Trees.
How Commands Are Processed
The AI controller requests a command (e.g., follow player).
The Command Manager adds the command to a queue.
Every 3 seconds, the ACF AI Behavior Tree checks for pending commands.
If a command exists, the Command Manager executes it.
When execution finishes, the next command in the queue is processed.
Key Functions in
UACFCommandsManagerComponentFunction
Description
TriggerCommand(UACFBaseCommand* Command)Adds a command to the queue and starts execution if no command is running.
ExecutePendingCommand()Processes and executes the next command in the queue.
HasPendingCommands()Checks if there are any commands waiting to be executed.
TerminateCurrentCommand()Stops the currently executing command.
3. Creating a New AI Command
To define a custom AI command, follow these steps:
Step 1: Create a Child Class of
UACFBaseCommandEach new command must inherit from
UACFBaseCommand. Example: Follow Player Command.#include "ACFFollowPlayerCommand.h" #include "AI/ACFAIController.h" #include "GameFramework/Character.h" #include "AIController.h" #include "NavigationSystem.h" void UACFFollowPlayerCommand::ExecuteCommand_Implementation(AACFAIController* controller) { if (!controller || !controller->GetPawn()) { FinishExecution(false); return; } ACharacter* AICharacter = Cast<ACharacter>(controller->GetPawn()); APawn* PlayerPawn = UGameplayStatics::GetPlayerPawn(GetWorld(), 0); if (AICharacter && PlayerPawn) { // Use AI MoveTo to follow the player controller->MoveToActor(PlayerPawn, 100.0f); FinishExecution(true); } else { FinishExecution(false); } }