AI in Lyra Starter: How AI Works in UE5

In game development, AI (Artificial Intelligence) is what gives life to Non-Player Characters (NPCs), making them reactive, intelligent, and engaging. In Unreal Engine, AI is powered by systems like Behavior Trees, Blackboards, and AI Controllers. The Lyra Starter Game1, a sample project by Epic Games, provides an excellent foundation for understanding how AI is implemented and how it can be used to create interactive and dynamic gameplay.

In this blog post, we’ll break down how AI works in Lyra Starter Game, how it makes decisions, and how these decisions translate into actions that affect the gameplay.

Here’s a quick clip of me getting demolished by bots on a Macbook.

The Core Components of AI in Lyra Starter Game

In Lyra Starter Game, AI is designed to interact intelligently with the environment. Several key components drive the AI’s decision-making and actions:

  1. AI Controllers
  2. Behavior Trees
  3. Blackboards
  4. Perception System
  5. Navigation System
  6. Tasks and Actions

Let’s dive into how these components are used in Lyra’s AI system.

1. AI Controllers: The Brain Behind the Action

In Lyra Starter Game, the AI Controller is responsible for controlling the AI character (pawn). It directs how the AI reacts to stimuli in the game world and interacts with the Behavior Tree to determine the next course of action.

  • Possession: When an AI character is possessed by an AI Controller, the controller takes over and manages its behavior.
  • Perception: The controller manages sensory inputs (like seeing or hearing enemies).
  • Behavior Tree: The controller executes the Behavior Tree, which governs what the AI does based on the data from the Blackboard.

Here’s an example of how the AI Controller sets up the Behavior Tree in Lyra Starter Game:

void ALyraAIController::BeginPlay()
{
    Super::BeginPlay();

    // Set the behavior tree for this AI controller
    if (BehaviorTree && BlackboardComponent)
    {
        RunBehaviorTree(BehaviorTree);  // This begins executing the Behavior Tree
    }
}

This example sets up the Behavior Tree and starts it when the AI controller begins.

2. Behavior Trees: Structuring AI Decision-Making

At the heart of Lyra Starter Game AI is the Behavior Tree. This visual scripting tool defines the decision-making logic for AI characters. In the game, the Behavior Tree drives how NPCs (e.g., enemies) decide what actions to take based on conditions like their current state, the environment, or the presence of the player.

For example, the Behavior Tree might ask:

  • Does the AI have ammo?
  • Is there an enemy in sight?
  • Should the AI move to a different position or attack?

Each decision leads to specific actions, such as moving, attacking, or searching for cover.

Here’s a high-level breakdown of how a typical Behavior Tree works in Lyra Starter Game:

  • Root Node: The behavior tree starts at the root and branches into different actions.
  • Condition Nodes: The tree checks conditions like whether the AI has ammo or whether it can find an enemy.
  • Action Nodes: Based on the conditions, the AI performs tasks like moving to a position, attacking, or searching for a weapon.

3. Blackboard: The Memory of the AI

The Blackboard in Lyra Starter Game stores key pieces of information that the AI uses for decision-making. The Blackboard helps the AI remember things like:

  • The TargetEnemy: The current enemy the AI is focused on.
  • OutOfAmmo: Whether the AI has run out of ammo.
  • MoveGoal: The destination where the AI needs to go.

The Blackboard is updated during gameplay and is crucial for the Behavior Tree’s decision-making process.

For example, when an AI perceives an enemy, the AI Controller updates the Blackboard with the new target. The Behavior Tree then checks the Blackboard to decide whether to attack or move.

Here’s how the Blackboard might be updated when the AI perceives an enemy in Lyra Starter Game:

void ALyraAIController::OnPerceptionUpdated(const TArray<AActor*>& SensedActors)
{
    for (AActor* Actor : SensedActors)
    {
        if (IsValid(Actor))
        {
            // Update the target in the Blackboard when an enemy is perceived
            BlackboardComponent->SetValueAsObject("TargetEnemy", Actor);
        }
    }
}

4. Tasks and Actions: Enacting AI Behaviors

The Tasks and Actions are the actual behaviors the AI will enact based on decisions made in the Behavior Tree. These are the steps that make up the AI’s actions in the game world, such as:

  • Movement: Moving the AI to a target position.
  • Attacking: Shooting or performing an attack.
  • Search: Looking for new weapons or cover.

These tasks can be created as either Blueprints or C++ classes in Unreal Engine.

For instance, here’s how a MoveTo task could be implemented in C++:

class UMyMoveToTask : public UBTTaskNode
{
public:
    virtual EBTNodeResult::Type ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) override
    {
        // Logic for moving AI to a location
        APawn* Pawn = OwnerComp.GetAIOwner()->GetPawn();
        FVector TargetLocation = ...;  // Get target position (e.g., from the Blackboard)
        Pawn->MoveToLocation(TargetLocation);
        
        return EBTNodeResult::Succeeded;
    }
};

5. Perception System: How AI Sees the World

Lyra Starter Game uses the AI Perception System2 to detect things like enemies, sounds, and other stimuli. The AI Perception Component senses changes in the game world (e.g., seeing a player or hearing a noise) and updates the Blackboard with the relevant information, which the Behavior Tree then uses to decide the next action.

Here’s how the AI Perception System works in Lyra Starter Game:

void ALyraAIController::OnPerceptionUpdated(const TArray<AActor*>& SensedActors)
{
    for (AActor* Actor : SensedActors)
    {
        if (IsValid(Actor))
        {
            // Update the target in the Blackboard when an enemy is perceived
            BlackboardComponent->SetValueAsObject("TargetEnemy", Actor);
        }
    }
}

6. Navigation System: How AI Moves

In Lyra Starter Game, AI movement is handled by the Navigation System, which uses a NavMesh (Navigation Mesh) to determine how AI characters can move through the world. The NavMesh defines which areas are walkable for AI and where they can move.

When an AI needs to move to a target (e.g., a player or an item), it queries the NavMesh to find the best path and navigates through obstacles.

Here’s how movement might be implemented in Lyra Starter Game:

FVector TargetLocation = BlackboardComponent->GetValueAsVector("MoveGoal");
UNavigationSystemV1::GetCurrent(GetWorld())->SimpleMoveToLocation(GetPawn(), TargetLocation);

How AI Enacts Behaviors in Lyra Starter Game

To sum up, here’s how the AI in Lyra Starter Game enacts behaviors:

  1. Decision Making: The Behavior Tree checks the Blackboard for key information (e.g., target, ammo status).
  2. Action Selection: Based on the data, the Behavior Tree determines which Task or Action the AI should perform (e.g., moving to a new location or attacking).
  3. Execution: The AI Controller enacts the task by interacting with the pawn and the game world (e.g., moving the pawn or performing an animation).
  4. Perception and Updates: As the game progresses, the AI Perception System updates the Blackboard with new information, which the Behavior Tree uses to decide what the AI should do next.

For example, if the AI is out of ammo, the Behavior Tree might direct it to find a new weapon. If the AI perceives an enemy, it may choose to attack.


Wrapping Up

Lyra Starter Game offers a fantastic foundation for understanding how Unreal Engine’s AI systems work. By combining AI Controllers, Behavior Trees, Blackboards, Perception, and Navigation, Lyra’s AI creates dynamic behaviors that can react to both the game world and the player’s actions.

If you’re working with Lyra Starter Game or another Unreal Engine project, experimenting with these systems can help you create responsive and engaging AI characters. Whether you’re designing enemy AI for a simple combat system or a complex boss fight, Unreal Engine provides the tools you need to create intelligent NPCs that challenge players.

  1. https://www.fab.com/listings/93faede1-4434-47c0-85f1-bf27c0820ad0 ↩︎
  2. https://dev.epicgames.com/documentation/en-us/unreal-engine/ai-perception-in-unreal-engine ↩︎

Leave a Reply

Your email address will not be published. Required fields are marked *