Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Setting Up Detection by Affiliation for AI in Unreal C++

Tech May 8 5

During AI perception setup, I encountered an issue where when two AI characters appear in the scene, they detect eachother and trigger pursuit. To differentiate the detected actors, this functionality is required.

Unreal Official Documentation: This property can be used to set up team-based visual perception. Currently, Affiliation can only be defined in C++. For Blueprints, you can use the Detect Neutrals option to detect all actors, then filter actor types using Tags.

Step 1: Create a header file for the TeamAffiliation enum

// TeamType.h

#pragma once

UENUM(BlueprintType)
enum class ETeamAffiliation : uint8
{
    FRIENDLY,
    ENEMY,
    NEUTRAL
};

Note: Ensure the header file is placed in the correct project path (it may not default to the same location as other code files).

Define the affiliation in the player and AI character classes

// PlayerCharacter.h
#include "TeamType.h"

public:
    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Affiliation", meta = (AllowPrivateAccess = "true"))
    ETeamAffiliation Affiliation = ETeamAffiliation::FRIENDLY;  // Set to friendly

// AICharacter.h
#include "TeamType.h"
#include "AIController.h"  // Include the appropriate AI controller

public:
    UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Affiliation", meta = (AllowPrivateAccess = "true"))
    ETeamAffiliation Affiliation = ETeamAffiliation::ENEMY;  // Set to enemy

    UFUNCTION()
    void OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus Stimulus);

// AICharacter.cpp

// In BeginPlay:
EnemyAIController->PerceptionComponent->OnTargetPerceptionUpdated.AddDynamic(this, &AAICharacter::OnTargetPerceptionUpdated);

// Implementation of the perception callback
void AAICharacter::OnTargetPerceptionUpdated(AActor* Actor, FAIStimulus Stimulus)
{
    if (!Actor)
    {
        return;
    }

    if (Stimulus.IsActive())  // When an actor is detected
    {
        // Check if the detected actor is the player character
        APlayerCharacter* Player = Cast<APlayerCharacter>(Actor);
        // If the affiliation differs, start chasing
        if (Player && Player->Affiliation != this->Affiliation)
        {
            UE_LOG(LogTemp, Warning, TEXT("Controller found player"));
            EnemyAIController->GetBlackboardComponent()->SetValueAsObject(TEXT("Target"), Player);
            EnemyAIController->GetBlackboardComponent()->SetValueAsBool(TEXT("FindPlayer"), true);
        }
    }
    else
    {
        UE_LOG(LogTemp, Warning, TEXT("No player found"));
        EnemyAIController->GetBlackboardComponent()->SetValueAsObject(TEXT("Target"), nullptr);
        EnemyAIController->GetBlackboardComponent()->SetValueAsBool(TEXT("FindPlayer"), false);
    }
}

After this setup, AI characters should no longer chase each other.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.