Monday, September 9, 2019

Overriding OnPossess() with Unreal Engine 4.23.0

Recently Possess() has been deprecated from UE4, and when writing classes based on AAIController you have to use the function OnPossess().  When using the former vs the latter, you will have compilation problems, and potential breaks in the Artificial Intelligence for your various Pawns, and Blueprints, Blackboard, etc.


Above: AI Book I am currently reading. Quite insightful.


If you are using Blueprints, it's a matter of changing them. However you are using C++ code you must first do this in the header file (.h) of your class inheriting from AAIController:



 private: 

    UPROPERTY(transient)
    class UBlackboardComponent *BlackboardComp;
    
    UPROPERTY(transient)
    class UBehaviorTreeComponent *BehaviorComp;
   
 public:

   virtual void OnPossess(APawn *InPawn) override
  
    bool isDead = false;
    uint8 EnemyKeyID;
    uint8 PatrolKeyID;
    uint8 EnemyModeBBKeyID;
    uint8 isDeadBBKeyID;
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MoveEnemy")


Then in the implementation file (.cpp) file you implement like this:



 void AEnemyAI::OnPossess(APawn *InPawn) {
    Super::OnPossess(InPawn);
    AEnemyCharacter *Char = Cast<AEnemyCharacter>(InPawn);
    
    if (Char && Char->Enemy) {
        BlackboardComp->InitializeBlackboard(*Char->Enemy->BlackboardAsset);
        EnemyKeyID = BlackboardComp->GetKeyID("Target");
        PatrolKeyID = BlackboardComp->GetKeyID("PatrolTarget");
        EnemyModeBBKeyID = BlackboardComp->GetKeyID("EnemyModeBB");
        isDeadBBKeyID = BlackboardComp->GetKeyID("isEnemyDeadBB");
        tChar = Char;
        BehaviorComp->StartTree(*Char->Enemy);
    }
 }

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...