Is there an example of finding path asynchronously in navigation system

I’m recently studying the source code about AI in UE4, and I notice UE4 seems to have a navigation System supporting finding path asynchronously(Because I find a function called “FindPathAsync” in NavigationSytem.cpp), but I can not find any places using this function in source code or a simple example about this, even in Blueprint I can not find any related functions, so I want to know how to use it, Is there a simple example about it?

1 Like

Still there’s nothing out there. Can anyone provide an example?

1 Like

This is a snippet of my code used to get path from a character to a mission objective:

void AHeroCharacterBase::ShowPathToObjective()
{
	FObjectiveBase lastObjective;
	bool isObjectiveFound = false;

	AChroniclesGameMode* gameMode = Cast<AChroniclesGameMode>(GetWorld()->GetAuthGameMode());
	if (gameMode)
	{
		if (IsValid(gameMode->GetObjectivesManager()))
		{
			TArray<FObjectiveBase> objectives = gameMode->GetObjectivesManager()->GetVisibleObjectivesWithLocation();
			if (objectives.Num() > 0)
			{
				lastObjective = objectives[objectives.Num() - 1];
				isObjectiveFound = true;
			}
		}
	}

	if (isObjectiveFound)
	{
		FNavAgentProperties navAgentProperties;
		navAgentProperties.AgentHeight = 100;
		navAgentProperties.AgentRadius = 10;
		navAgentProperties.bCanWalk = true;
		navAgentProperties.bCanFly = false;

		FPathFindingQuery navParams;
		navParams.EndLocation = lastObjective.GetLocation();
		navParams.StartLocation = GetActorLocation();
		ANavigationData* navData = GetWorld()->GetNavigationSystem()->MainNavData;
		navParams.QueryFilter = UNavigationQueryFilter::GetQueryFilter<UNavigationQueryFilter>(navData);
		navParams.NavData = navData;

		FNavPathQueryDelegate del;
		del.BindUObject(this, &AHeroCharacterBase::PathToObjectiveFound);

		GetWorld()->GetNavigationSystem()->FindPathAsync(navAgentProperties, navParams, del, EPathFindingMode::Regular);	
	}
}


void AHeroCharacterBase::PathToObjectiveFound(uint32 aPathId, ENavigationQueryResult::Type aResultType, FNavPathSharedPtr aNavPointer)
{
	FNavPathPoint lastNavPoint;
	bool isFirstPoint = true;
	for (FNavPathPoint navPoint : aNavPointer->GetPathPoints())
	{
		DrawDebugPoint(GetWorld(), navPoint.Location, 10, FColor::Red, false, 100);
		if (!isFirstPoint)
		{
			DrawDebugLine(GetWorld(), lastNavPoint.Location, navPoint.Location, FColor::Red, false, 100);
		}
		lastNavPoint = navPoint;
	isFirstPoint = false;
	}
}
2 Likes