Showing posts with label unreal. Show all posts
Showing posts with label unreal. Show all posts

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);
    }
 }

Friday, September 23, 2016

Linear Interpolation (LERP) with C++ and Unreal Engine 4.13.0


In Unreal Engine 4 you will eventually need Linear Interpolation between two values to do something like ping pong between two float values across Time Delta. I wrote a "ghetto" function that does this and takes the arguments min, max, factor (scale), and then the value itself, which it increments/decrements accordingly. You can also do this with trig functions across Delta Time to achieve a similar effect. You can always get Delta Time from the Overridable Tick Method.

Here is the code:

float GameHelper::IncrementLerp(float x, float v) {
    return v+=x;
}

float GameHelper::xLerp (float mMin, float mMax, float mFactor, float value) {
    if (value>=mMin && goBackwards){
        value = IncrementLerp(-mFactor,value);
        if (value<=mMin) {
            goForwards = true;
            goBackwards = false;
        }
    } else if (goForwards && value<=mMax) {
        value = IncrementLerp(mFactor,value);
        if (value>=mMax) {
            goBackwards = true;
            goForwards = false;
        }
    }
    return value;
}

I use it for sky lighting in a game I am working on. The sky light appears to be pulsating from 0.7f to 2.7f intensity. I use this function and expose other functions in Blueprints. HD demo of my latest game, Jumper utilizing the pulsating::



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