Unreal Crashes when Playing

The following codes compiled fine but when I play Unreal crashes.

// Fill out your copyright notice in the Description page of Project Settings.


#include "Hunter.h"
#include "AIController.h"
#include "Math/UnrealMathUtility.h"
#include "NavigationSystem.h"

void AHunter::BeginPlay()
{
	Super::BeginPlay();

    Wandering();
}

void AHunter::Wandering()
{
    if (!GetController()->IsPlayerController())
    {
        FVector GetLocation = SetLocation();
        GetController<AAIController>()->MoveToLocation(GetLocation); 
    }
}

FVector AHunter::SetLocation()
{
    FNavLocation OutLocation; 
    UNavigationSystemV1 FindPath; 

    if (FindPath.GetRandomPointInNavigableRadius(this->GetActorLocation(), NavRadius, OutLocation))
    {
        return OutLocation.Location;
    } 
    else 
    {
        return this->GetActorLocation();
    }
}

NavigationSystemV1 FindPath is at line 28
GetController()->MoveToLocation(GetLocation) is at line 21

What am I doing wrong?

Apparently that cast is failing or GetController within the if is returning nullptr. Does it work if you have

if (AAIController* AIController = GetController<AAIController>())
{
    FVector GetLocation = SetLocation();
    AIController>MoveToLocation(GetLocation); 
}
// Fill out your copyright notice in the Description page of Project Settings.


#include "Hunter.h"
#include "AIController.h"
#include "Math/UnrealMathUtility.h"
#include "NavigationSystem.h"

void AHunter::BeginPlay()
{
	Super::BeginPlay();

    Wandering();
}

void AHunter::Wandering()
{
    if (AAIController* AIController = GetController<AAIController>())
    {
        if (AIController == nullptr) return;

       FVector GetLocation = SetLocation();
       AIController->MoveToLocation(GetLocation); //apprently the crash error keeps on complaining about this.
    }
}

FVector AHunter::SetLocation()
{
    FNavLocation OutLocation; 
    UNavigationSystemV1 FindPath; //apprently the crash error keeps on complaining about this. It is needed so I can access functions in NavigationSystem.h. 
    bool BFP = FindPath.GetRandomPointInNavigableRadius(this->GetActorLocation(), NavRadius, OutLocation); //The function is either a FVector or a bool. So I try to assign as a bool

    if (BFP) 
    {   
        return OutLocation.Location;
    } 
    else 
    {
        return this->GetActorLocation();
    }
}

No it still crashes. Read the comments I made in the code.

Strange, I wouldn’t have expected that to compile. I don’t think you’re supposed to be able to default construct an object of that type.

From the docs you probably want GetCurrent

UNavigationSystemV1* FindPath = UNavigationSystemV1::GetCurrent(this);
if (FindPath)
{
    // Use
}

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms