0

I try to implement the pawn navigation to the player in my game. When I tried that in BP site, it worked perfectly, but I try to transform that in C++ code. And I got a kind of wird error. Befor this I faced another error but I find the NavigationSystem was changed a bit in my ue version, but I figured out problem by changing to UNavigationSystemV1. It might be interfering with my class?

NavPath pointer it gave me the error.

Here is the error list:

> Error (active)    E0393   pointer to incomplete class type is not allowed 37
> Error (active)    E0393   pointer to incomplete class type is not allowed 40
> Error C2027   use of undefined type 'UNavigationPath'37
> Error C2027   use of undefined type 'UNavigationPath' 40

Here is the part of problematic code:

FVector ASTrackerBot::GetNextPathPoint()
{
ACharacter* PlayerPawn =  UGameplayStatics::GetPlayerCharacter(this, 0);

UNavigationPath* NavPath = UNavigationSystemV1::FindPathToActorSynchronously(this, 
GetActorLocation(), PlayerPawn);

if (NavPath->PathPoints.Num() > 1)
{

    return NavPath->PathPoints[1];
}


return GetActorLocation();

}

Here is my entire private cpp file:

#include "AI/STrackerBot.h"
#include "Components/StaticMeshComponent.h"
#include "GameFramework/Character.h"
#include "Kismet/GameplayStatics.h"
#include "Runtime\NavigationSystem\Public\NavigationSystem.h"
#include "Runtime/Engine/Classes/Components/InputComponent.h"


// Sets default values
ASTrackerBot::ASTrackerBot()
{
    // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

    MeshComp = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComp"));
    MeshComp->SetCanEverAffectNavigation(false);
    RootComponent = MeshComp;
}

// Called when the game starts or when spawned
void ASTrackerBot::BeginPlay()
{
    Super::BeginPlay();
    
}

FVector ASTrackerBot::GetNextPathPoint()
{
    // parcurgere drum pana la locatia playerului
    ACharacter* PlayerPawn =  UGameplayStatics::GetPlayerCharacter(this, 0);
    
    UNavigationPath* NavPath = UNavigationSystemV1::FindPathToActorSynchronously(this, GetActorLocation(), PlayerPawn);

    if (NavPath->PathPoints.Num() > 1)
    {
        // Return next point in the path
        return NavPath->PathPoints[1];
    }

    // nu a reusti sa gaseasca drumul
    return GetActorLocation();
}

// Called every frame
void ASTrackerBot::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

}

And here is my header file:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "STrackerBot.generated.h"

UCLASS()
class GAME_API ASTrackerBot : public APawn
{
    GENERATED_BODY()

public:
    // Sets default values for this pawn's properties
    ASTrackerBot();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

    UPROPERTY(VisibleAnywhere, Category = "Components")
    UStaticMeshComponent* MeshComp;

    FVector GetNextPathPoint();

public: 
    // Called every frame
    virtual void Tick(float DeltaTime) override;

};

2 Answers2

1

The definition of UNavigationPath needs to be available at the point where you do NavPath->PathPoints. Without the definition, how does the compiler know that UNavigationPath even has a member called PathPoints? You can have a pointer to a declared but undefined class (i.e., one you defined like class UNavigationPath; with no body) and pass it around but you can't access any of its members. Find the header file that defines UNavigationPath and make sure it's included in your source.

Willis Blackburn
  • 8,068
  • 19
  • 36
  • In UE 4.20 and above the UNavigationPath it's included in NavigationSystem.h, which I included. #include "Runtime/NavigationSystem/Public/NavigationSystem.h" – Robert Maximus Feb 20 '21 at 20:23
  • Curiously that `#include` line in your source uses backslashes instead of forward slashes. Does that work on your system? – Willis Blackburn Feb 20 '21 at 20:39
  • First time it was generated automatically like that, and then I changed to forward slashes style, it seems to work in both ways. – Robert Maximus Feb 20 '21 at 20:43
  • @RobertMaximus try to directly include the relevant header: /Engine/Source/Runtime/NavigationSystem/Public/NavigationPath.h – Secundi Feb 20 '21 at 21:31
  • I think something is wonky with your includes or the include path. You're doing `#include "Components/StaticMeshComponent.h"` and then later you include `InputComponent.h` with a different path even though these headers are in the same directory. Are you sure your build isn't emitting warnings about not being able to find header files, or maybe including the same header through different paths? – Willis Blackburn Feb 20 '21 at 21:45
  • @WillisBlackburn no warnings, it's only those errors – Robert Maximus Feb 20 '21 at 21:52
  • What's the command you're using to compile? – Willis Blackburn Feb 20 '21 at 23:22
  • @RobertMaximus, what do you mean by that? Does it produce the same compile error with that approach? – Secundi Feb 20 '21 at 23:32
  • @Secundi with that approach it gave me the LNK2019 error, which is not an actually a code error, but it's not working like that. – Robert Maximus Feb 21 '21 at 09:06
  • @RobertMaximus I suggest that you compile your file with the option that dumps out the preprocessor output, which if you're using Visual C++ is probably `/E` (with line numbers) or `/EP` (without). It will expand all the `#include` files and `#define` macros and enable you to see exactly what the compiler is getting as input. Load up the preprocessed file (it will be huge) and see if there's a definition for `UNavigationPath` somewhere. If you upload that file somewhere and post a link, I'll take a look at it. – Willis Blackburn Feb 21 '21 at 15:30
1

You may also need to add the NavigationSystem module to your Project.Build.cs file in the PublicDependencyModuleNames array.

DevilsD
  • 545
  • 1
  • 5
  • 12