x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

error C2664: Cannot convert argument 1 from 'const TWeakObjectPtr'

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: alt text

Product Version: UE 4.21
Tags:
screenshot-192.png (25.1 kB)
more ▼

asked Apr 06 '19 at 08:04 PM in C++ Programming

avatar image

yMatinho
1

avatar image ChiefOfMischief Apr 07 '19 at 12:31 AM

Hmmm I can't repro the error. Although on a side note, you should not have #include "GladiatorReview.h" in your .h file. the 'class AGladiatorReview* GetEnemy() const;' is a forward decoration of the class. This is done to prevent circle includes. can you post the header for the gladiatorreview class?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

It seems to me the problem is you are calling up your PawnCanBeSeen function using a FConstPawnIterator which contains (as the name implies) a constant object pointer. However, your function, as it is currently declared, requires it be fed a non-constant object pointer, which means the compiler assumes that the function is allowed to modify the pointed-to object (even if it doesn't actually do so).

This means your function isn't allowed to be fed a constant object pointer, like those contained by the FConstPawnIterator, because constant objects can't be modified, but the compiler assumes your function does modify this parameter (in this case, the pointed-to object).

So try changing your PawnCanBeSeen function declaration to make its parameter constant, like so:

 UFUNCTION(BlueprintCallable, Category = "Behavior")
 bool PawnCanBeSeen(const APawn* target);

Since you aren't modifying the pointed-to object within this function anyway, it should be fine to make this change. (Actually, I would dare to say this reflects good coding practice.) This will tell the compiler that your function does not modify the pointed-to target object in any way, and allow the function to work the way you need it to.

Or at least I think.

Hopefully that does it and everything compiles fine after.

more ▼

answered Apr 07 '19 at 01:34 AM

avatar image

the_batch
1.2k 4 5 8

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question