Hello everyone, I am a beginner in c ++ in eu4 and I am reading the book ‘Mastering Unreal Engine 4’ and I found a problem. When I write the AI code, at the time of compiling these errors appear: *EnemyAIController.cpp(104): error C2664: ‘bool AEnemyAIController::PawnCanBeSeen(APawn *)’: Cannot convert argument 1 from ‘const TWeakObjectPtr’ to 'APawn ’ and EnemyAIController.cpp (43): error C2664: ‘void UBlackboardComponent :: SetValueAsVector (const FName &, FVector)’: Cannot convert argument 1 from ‘int32’ to ‘const FName &’. Does anyone there know how to fix these errors? If you know please reply to this message. Thank you!
CPP:
//This is the CPP
#include "EnemyAIController.h"
#include "Enemy.h"
#include "Bellz.h"
#include "BehaviorTree/BehaviorTree.h"
#include "BehaviorTree/BehaviorTreeComponent.h"
#include "BehaviorTree/BlackboardComponent.h"
#include "BehaviorTree/Blackboard/BlackboardKeyType_Object.h"
AEnemyAIController::AEnemyAIController() {
BlackboardComp = CreateDefaultSubobject<UBlackboardComponent>(TEXT("Blackboard"));
BehaviorTreeComp = CreateDefaultSubobject<UBehaviorTreeComponent>(TEXT("BehaviorTree"));
bWantsPlayerState = true;
}
void AEnemyAIController::Possess(APawn * InPawn)
{
AEnemy* _tempEnemy = Cast<AEnemy>(InPawn);
if (_tempEnemy && _tempEnemy->EnemyBehaviorTree) {
BlackboardComp->InitializeBlackboard(*_tempEnemy->EnemyBehaviorTree->BlackboardAsset);
EnemyKeyID = BlackboardComp->GetKeyID("Enemy");
EnemyPositionKeyID = BlackboardComp->GetKeyID("EnemyPosition");
BehaviorTreeComp->StartTree(*_tempEnemy->EnemyBehaviorTree);
}
}
void AEnemyAIController::BeginInactiveState()
{
}
void AEnemyAIController::SetEnemy(class APawn * InPawn)
{
if (BlackboardComp) {
BlackboardComp->SetValue<UBlackboardKeyType_Object>(EnemyKeyID, InPawn);
BlackboardComp->SetValueAsVector(EnemyPositionKeyID, InPawn->GetActorLocation());
SetFocus(InPawn);
}
}
AGladiatorReview * AEnemyAIController::GetEnemy() const
{
if (BlackboardComp) {
return Cast<AGladiatorReview>(BlackboardComp->GetValue<UBlackboardKeyType_Object>(EnemyKeyID));
}
return NULL;
}
void AEnemyAIController::UpdateControlRotation(float DeltaTime, bool bUpdatePawn)
{
FVector TheCenter = GetFocalPoint();
if (!TheCenter.IsZero() && GetPawn()) {
FVector Direction = TheCenter - GetPawn()->GetActorLocation();
FRotator TheNewRotation = Direction.Rotation();
TheNewRotation.Yaw = FRotator::ClampAxis(TheNewRotation.Yaw);
SetControlRotation(TheNewRotation);
APawn* const _tempPawn = GetPawn();
if (_tempPawn && bUpdatePawn) {
_tempPawn->FaceRotation(TheNewRotation, DeltaTime);
}
}
}
bool AEnemyAIController::PawnCanBeSeen( APawn * target)
{
return false;
}
void AEnemyAIController::OnSearchEnemy()
{
APawn* _tempPawn = GetPawn();
if (_tempPawn == NULL) {
return;
}
const FVector _tempLocation = _tempPawn->GetActorLocation();
float BestDiskSQ = MAX_FLT;
AGladiatorReview* PlayerPawn = NULL;
for (FConstPawnIterator It = GetWorld()->GetPawnIterator(); It; ++It) {
AGladiatorReview* TestPawn = Cast<AGladiatorReview>(*It);
if (PawnCanBeSeen(*It)) {
AEnemy* const _testEnemy = Cast<AEnemy>(TestPawn);
if (_testEnemy) {
}
else {
if (TestPawn && TestPawn->GetIsStillAlive()) {
const float _distanceSq = (TestPawn->GetActorLocation() - _tempLocation).SizeSquared();
if (_distanceSq < BestDiskSQ) {
BestDiskSQ = _distanceSq;
PlayerPawn = TestPawn;
}
}
}
}
if (PlayerPawn) {
SetEnemy(PlayerPawn);
}
}
}
/*if (BlackboardComp) {
BlackboardComp->SetValue<UBlackboardKeyType_Object>(EnemyKeyID, InPawn);
BlackboardComp->SetValueAsVector(EnemyPositionKeyID, InPawn->GetActorLocation());
SetFocus(InPawn);
}*/
Header File:
#pragma once
#include "CoreMinimal.h"
#include "AIController.h"
#include "BehaviorTree/BehaviorTreeComponent.h"
#include "GladiatorReview.h"
#include "EnemyAIController.generated.h"
/**
*
*/
UCLASS()
class BELLZ_API AEnemyAIController : public AAIController
{
GENERATED_BODY()
public:
AEnemyAIController();
UPROPERTY(transient)
UBlackboardComponent* BlackboardComp;
UPROPERTY(transient)
UBehaviorTreeComponent* BehaviorTreeComp;
virtual void Possess(class APawn* InPawn) override;
virtual void BeginInactiveState() override;
UFUNCTION(BlueprintCallable, Category = "Behavior")
void SetEnemy(class APawn* InPawn);
UFUNCTION(BlueprintCallable, Category = "Behavior")
class AGladiatorReview* GetEnemy() const;
UFUNCTION(BlueprintCallable, Category = "Behavior")
void UpdateControlRotation(float DeltaTime, bool bUpdatePawn);
UFUNCTION(BlueprintCallable, Category = "Behavior")
bool PawnCanBeSeen(APawn* target);
UFUNCTION(BlueprintCallable, Category = "Behavior")
void OnSearchEnemy();
protected:
int32 EnemyKeyID;
int32 EnemyPositionKeyID;
};
Compiler Result: