Best way to access my variables from my Characters

Heey guys,

I was wondering what is the best way to build my AI.
Right now I got my BasicEnemy which comes from ACharacter where I got multiple variables such as health,damage and so on.

Now I want to build multiple other AIChars from this BasicEnemy for example BookEnemy, im doing the same for the controller for the AI where I got a BasicController and so on.

Now I added a service in my behaviour tree that calls a certain function in my PegControllerLight controller that has to reduce the health from the player by the damage of BookEnemy.

AAI_PegEnemyLight* AiChar = (AAI_PegEnemyLight*)GetPawn();
AiChar->Damage

I thought I was able to use the code above to get the Damage. but it says unable to read memory…What am I doing wrong ?

Thanks in regards

Hi Falko,

It looks like GetPawn() does not return AAI_PegEnemyLight. You should use Cast to check this, or you can get basic pawn class (if I correctly understand your Damage variable is in BasicEnemy class)

BasicEnemy* pEnemy = Cast<BasicEnemy>(GetPawn());
If (pEnemy)
{
// to do smth
pEnemy ->Damage = ... 
}

Best regards,

Hi ,

yes its correct that my Damage variable is being made in my BasicEnemy, but I overwrite it in AAI_PegEnemyLight constructor. And that is the variable that I need to get…

When I use
AAI_BasicEnemy* pEnemy = Cast(GetPawn());

I indeed can cast it to an BasicEnemy and read the variables.

How come I cant use

AAI_PegEnemyLight* AiChar = Cast(GetPawn());

To receive the variables I set in the constructor if my AI_PegEnemyLight

My first guess is that somewhere in your basic class, like in begin play for example, you are writing over the damage in your derived class’s constructor. Is the cast for the derived class not working or are the variables not what you want them to be?

My variables were not right, I changed the variables in met BasicEnemy to

/** Speed */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = AIStats)
float Damage;

So I can just set them in my Blueprint and use

BasicEnemy* pEnemy = Cast(GetPawn()); as suggested by to get the Damage.

Thanks for the help guys