Monday, September 19, 2016

Get an ENUM value from an Enemy AI Behaviour Tree in Unreal Engine 4.13.0 using C++

Often we intermingle C++ and Blueprints, and need for the two to communicate. With Behavior Trees, using ENUMs is an everyday occurrence and on occasion we need to access said values in C++ Code. Below a Service I created called Check For Player which overrides base UBTService, uses the TickNode to check for the existence of a Player that the EnemyAI sees.

See below:


void UBTService_CheckForPlayer::TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
AEnemyAI *EnemyPC = Cast<AEnemyAI>(OwnerComp.GetAIOwner());

if ( EnemyPC) {
        AJumperCharacter *Enemy = Cast<AJumperCharacter>(GetWorld()->GetFirstPlayerController()->GetPawn());
        if (!Enemy) {
            return;
        } else if (Enemy && EnemyPC->tChar->EnemyLife>0) {
                OwnerComp.GetBlackboardComponent()->SetValue<UBlackboardKeyType_Object>(EnemyPC->PatrolKeyID,EnemyPC);
                OwnerComp.GetBlackboardComponent()->SetValue<UBlackboardKeyType_Object>(EnemyPC->EnemyKeyID,Enemy);
        }
        uint8 EnemyMode = OwnerComp.GetBlackboardComponent()->GetValueAsEnum("EnemyModeBB");
FString str = FString::FromInt(EnemyMode);
EnemyPC->tChar->SetMode(EnemyMode);
/*
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, str);
*/
}

}
My EnemyCharacter.cpp referenced above has different modes. Since I get an unsigned int8 from GetValueAsEnum("EnemyModeBB"), which is my enum variable in my BehaviourTree , I need to translate the uint8 to an ENUM on my EnemyCharacter class. This isn't necessary and you can case it out but since EnemyCharacter is my main class with all of the EnemyProperties, I want to send the information back.  See below:

void AEnemyCharacter::SetMode(uint8 m) {
    if (Mode!=EnemyMode::Hit)
        switch (m) {
            case 0:SetMode(EnemyMode::Attack);  break;
            case 1:SetMode(EnemyMode::Dead);  break;
            case 2:SetMode(EnemyMode::Flee);  break;
            case 3:SetMode(EnemyMode::Hit);  break;
            case 4:SetMode(EnemyMode::Idle);  break;
            case 5:SetMode(EnemyMode::Patrol);  break;
            case 6:SetMode(EnemyMode::Pursue);  break;
            case 7:SetMode(EnemyMode::Talk);  break;
            case 8:SetMode(EnemyMode::Taunt);  break;
            default:SetMode(EnemyMode::Pursue);  break;
break;
        }

}


Here is the definition of EnemyMode in EnemyCharacter.cpp class:

UENUM(BlueprintType)
enum class EnemyMode : uint8
{
    Attack      UMETA(DisplayName="Attack"),
    Dead        UMETA(DisplayName="Dead"),
    Flee        UMETA(DisplayName="Flee"),
    Hit         UMETA(DisplayName="Hit"),
    Idle        UMETA(DisplayName="Idle"),
    Patrol      UMETA(DisplayName="Patrol"),
    Pursue      UMETA(DisplayName="Pursue"),
    Talk        UMETA(DisplayName="Talk"),
    Taunt       UMETA(DisplayName="Taunt")

};

Also, here is a screenshot for the ENUM:


The variable definition in my BehaviourTree BlackBoard (zombieBB):


No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...