# Modify pathfinding cost - DetourNavMeshQuery subclass

**URL:** <https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472>\
**Category:** World Creation\
**Tags:** pathfinding, Navigation, unreal-engine, navmesh, question\
**Created:** [December 26, 2015, 8:04pm UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472 "2015-12-26T20:04:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_d9f58e401](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d9f58e401](https://forums.unrealengine.com/u/anonymous_user_d9f58e401)\
**Post date:** [December 26, 2015, 8:04pm UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/1 "2015-12-26T20:04:29Z")

</div>

Hi,

I want to modify the costs of the pathfinding algorithm of UE4. I’ve been looking at the source code and the costs are calculated in the `DetourNavMeshQuery` class. However, UE 4 doesn’t allow me to create a sublcass of `DetourNavMeshQuery` from File → Create new C++ class menu. I don’t know why.

My question is, Why it doesn’t let me create a subclass of `DetourNavMeshQuery`? And, Is there any other way to do it? Maybe modifying the original class instead of creating a subclass?

Thanks!

---

<div class="post-metadata">

**Author:** ![MilkyEngineer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/milkyengineer/32/918676_2.png) [@MilkyEngineer](https://forums.unrealengine.com/u/MilkyEngineer)\
**Post date:** [December 29, 2015, 3:53am UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/2 "2015-12-29T03:53:33Z")

</div>

First issue, `DetourNavMeshQuery` isn’t a class, it’s just the header file that contains many classes for Detour queries. I assume though, that the class you want to subclass is `dtQueryFilter` - which handles path costs for Detour. The reason why it doesn’t show up in the editor, is that none of the Recast/Detour library classes are UObject classes (except for the classes that Epic Provides for UE4).

Solution #3 shows you how to subclass `dtQueryFilter`, but it’s probably not your best solution - unless you’re invested into implementing your own Recast/Detour solution for UE4. However, the first two solutions might be of use to you.

## Solution 1: UE4 Built-in Path Cost (Easy)

UE4 uses Navigation Areas to modify the cost of a path. In this case, you can subclass `UNavArea` and provide a `DefaultCost` and `FixedAreaEnteringCost`. For example (you can also subclass a blueprint from NavArea as well):

### NavArea\_Example.h

```
UCLASS()
class MYPROJECT_API UNavArea_Example : public UNavArea_Default
{
	GENERATED_BODY()

public:
	UNavArea_Example(const FObjectInitializer& ObjectInitializer);
};

```

### NavArea\_Example.cpp

```
UNavArea_Example::UNavArea_Example(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	DefaultCost = 2.f;
	FixedAreaEnteringCost = 10.f;
}

```

After this, you can then place a **Nav Modifier Volume** in the level where you desire and set the Area Class.

## Solution 2: Modifying DefaultQueryFilter (Moderate)

Add the **Navmesh** module as a public dependency in your _MyProject_.build.cs - this will link in any of the Recast/Detour libraries to your project. Subclass `ARecastNavMesh` in C++ and declare the constructor. Within _DefaultEngine.ini_, add a section (as shown below), to tell the engine to use our Navmesh Implementation. Create a second class below that, which subclasses `FRecastQueryFilter` (you can use `INavigationQueryFilterInterface`, but if you’re extending from RecastNavmesh, there’s a lot of assumptions in the existing code that will mean a crash, i.e. casting, etc). In this second class, you can override any virtual functions to allow changes in the cost of a path. Next, declare a variable of our second class type, and define the `AMyRecastNavMesh` constructor. We will then set the filter implementation for the `DefaultQueryFilter` shared pointer in our constructor. You can see this in the following few classes:

### MyProject.build.cs

```
using UnrealBuildTool;

public class MyProject : ModuleRules
{
	public MyProject(TargetInfo Target)
	{
        PublicDependencyModuleNames.AddRange(
            new string[] {
                "Core",
                "CoreUObject",
                "Engine",
                "InputCore",
                "Navmesh"
            }
        );

        PrivateDependencyModuleNames.AddRange(new string[] { });
	}
}

```

### DefaultEngine.ini

```
[/Script/Engine.NavigationSystem]
+SupportedAgents=(Name="CustomNavmesh",NavigationDataClassName=/Script/MyProject.MyRecastNavMesh)

```

### MyRecastNavMesh.h

```
#pragma once

#include "AI/Navigation/PImplRecastNavMesh.h"
#include "AI/Navigation/RecastNavMesh.h"
#include "MyRecastNavMesh.generated.h"

class MYPROJECT_API FNavFilter_Example : public FRecastQueryFilter
{
	// Override any functions from INavigationQueryFilterInterface/FRecastQueryFilter here
};

UCLASS()
class MYPROJECT_API AMyRecastNavMesh : public ARecastNavMesh
{
	GENERATED_BODY()

public:
	AMyRecastNavMesh(const FObjectInitializer& ObjectInitializer);

	FNavFilter_Example DefaultNavFilter;
};

```

### MyRecastNavMesh.cpp

```
#include "MyProject.h"
#include "MyRecastNavMesh.h"

AMyRecastNavMesh::AMyRecastNavMesh(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	if (DefaultQueryFilter.IsValid())
	{
		DefaultQueryFilter->SetFilterImplementation(dynamic_cast<const INavigationQueryFilterInterface*>(&DefaultNavFilter));
	}
}

```

This will mean that every path finding query by default will use this solution - unless a QueryFilter has been chosen on PathFind. If you want to define a query filter, you can take a look at `URecastFilter_UseDefaultArea`. For example, the Move To node in Behavior Trees allow you to specify a move filter, if none is specified it will use the default (the one we’ve defined above).

## Solution 3: Extending dtQueryFilter (Hard)

If you’re looking to make your own solution, you’re going to have to put on some pretty high boots. The default NavMesh path finding costs utilise the `FRecastQueryFilter` class (as mentioned above), which implements `INavigationQueryFilterInterface` and subclasses `dtQueryFilter`. As mentioned before, if you’re extending from `ARecastNavmesh`, your filtering will have to extend from `FRecastQueryFilter` - doing a search of the entire solution for this class will show you why (C style casts galore on `INavigationQueryFilterInterface` objects to get as a `dtQueryFilter`).

If you’re going to subclass `dtQueryFilter`, you will have to link the **Navmesh** module (as shown for our _MyProject_.build.cs above) to be able to subclass it and make sure to have that class implement `INavigationQueryFilterInterface`. To create the class, you can just make a new Header file and CPP file, then regenerate the project files.

Unfortuantely, you would then have to throw out `ARecastNavmesh`, and start implementing your own subclass of `ANavigationData` in the same fashion that `ARecastNavmesh` does - hence why this is the hardest option.

* * *

Hopefully these solutions are at least insightful or will be of some help! 🙂

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d9f58e401](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d9f58e401](https://forums.unrealengine.com/u/anonymous_user_d9f58e401)\
**Post date:** [December 29, 2015, 11:46am UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/3 "2015-12-29T11:46:16Z")

</div>

Hey, Great answer! Solution #1 really fit my needs. My goal is to create a sort of influence map. I will **fill the scene with a grid of small Nav Modifier Volumes and I will update them on runtime**. However, I don’t know if a lot of Nav Modifier Volumes can be an important downgrade in efficiency. Do you know it?

For this reason I am checking Solution #2. But, which virtual functions can I override to change pathfinding costs? I only see functions to add costs to Nav Areas (like `SetAreaCost(uint8 AreaType, float Cost)` or `SetFixedAreaEnteringCost(uint8 AreaType, float Cost)`).  
Is this nearly the same of what we are doing in Solution #1, isn’t it?

Thank you!

---

<div class="post-metadata">

**Author:** ![MilkyEngineer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/milkyengineer/32/918676_2.png) [@MilkyEngineer](https://forums.unrealengine.com/u/MilkyEngineer)\
**Post date:** [January 5, 2016, 1:45am UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/4 "2016-01-05T01:45:25Z")

</div>

Sorry about the late reply! The Nav Modifier volumes should essentially ‘bake’ the specified _Area Class_ (via Area IDs) into the polys. However, I also expect that the volume will force extra polys to be made to clearly distinguish areas with different area classes (may need clarification on this, but it seems like a sane assumption). This would definitely have an effect on performance as there are more polys to search through, but I envision it wouldn’t be too bad - you can profile it by using the `stat Navigation` command in console and compare the baseline with the added volumes.

Yes, they’re similar solutions, just that Solution #2 allows you to write your own algorithm on path building. `SetAreaCost` is for the duration of the area and `SetFixedAreaEnteringCost` is for the initial cost of entering the area… imagine if those two are added, you have your total cost of traversing that area. If you’re wanting to modify path costs dynamically, you will have to write your own FindPath algorithm that queries the Detour system (like how you’ve started in your other question).

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d9f58e401](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d9f58e401](https://forums.unrealengine.com/u/anonymous_user_d9f58e401)\
**Post date:** [April 21, 2016, 11:10am UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/5 "2016-04-21T11:10:11Z")

</div>

In my opinion, third approach is the best one. I’ve successfully implemented it in less than a day. Thanks

---

<div class="post-metadata">

**Author:** ![MilkyEngineer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/milkyengineer/32/918676_2.png) [@MilkyEngineer](https://forums.unrealengine.com/u/MilkyEngineer)\
**Post date:** [April 21, 2016, 11:25pm UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/6 "2016-04-21T23:25:30Z")

</div>

Awesome, great work man! Glad to help! 😃

---

<div class="post-metadata">

**Author:** ![PasteDog](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PasteDog](https://forums.unrealengine.com/u/PasteDog)\
**Post date:** [July 25, 2016, 8:16am UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/7 "2016-07-25T08:16:46Z")

</div>

Hey, I am trying to achieve the same thing, but I can’t get it to work and what I am doing feels wrong ^^

When I make my own ARecastNavmesh, I need to make so many new classes just to use the custom ARecastNavmesh. I don’t really want to change the engine code and right now my code is just a mess to work with… Did you change the engine to get it to work or did you use some special casting technique I am not aware off?

Could you please explain a bit how you got it to work? ^^

Thanks in advance,

Elias

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d9f58e401](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d9f58e401](https://forums.unrealengine.com/u/anonymous_user_d9f58e401)\
**Post date:** [July 25, 2016, 9:37am UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/8 "2016-07-25T09:37:08Z")

</div>

No, I didn’t change the engine code. I have just overridden some classes (yes, there are many…) and of course I had to use some castings. Here is a gist with my navmesh code:

file  
file

Take a look and tell me if you have any doubt. Most of the methods are equal to the unreal source code except a few like getVirtualCost.

---

<div class="post-metadata">

**Author:** ![PasteDog](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PasteDog](https://forums.unrealengine.com/u/PasteDog)\
**Post date:** [July 25, 2016, 3:53pm UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/9 "2016-07-25T15:53:01Z")

</div>

I got it to work with your code and help!

I was searching to far I think, thank you very much! 🙂

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2fc8a6ac](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2fc8a6ac](https://forums.unrealengine.com/u/anonymous_user_2fc8a6ac)\
**Post date:** [September 20, 2016, 2:07pm UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/10 "2016-09-20T14:07:57Z")

</div>

Hey ,

thanks for sharing the source but it seems not to work.  
The constructor of AMyRecastNavMesh gets called by UClass::CreateDefaultObject(). But the dtQueryFilter\_Example::getVirtualCost() function gets never called when searching a path.

I’m using Unreal 4.12

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_cc8f596b](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_cc8f596b](https://forums.unrealengine.com/u/anonymous_user_cc8f596b)\
**Post date:** [October 20, 2017, 9:00am UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/11 "2017-10-20T09:00:12Z")

</div>

Hi ,

We have working our own version of dtQueryFilter, but we noticed that when setting the SetFilterImplementation in our ARecastNavMesh, the nav modifiers like NavArea\_Obstacle (or any other custom implementation) are ignored, seems like the cost is always bypassed to be Default one. Still those marked as NavArea\_Null work.

We only care about overriding the getVirtualCost, the rest of dtQueryFilter methods haven’t been modified.

Any recommendations where to debug or what other method could be missing in the implementation?  
Have you previously tested this case before?

Thank you.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d9f58e401](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d9f58e401](https://forums.unrealengine.com/u/anonymous_user_d9f58e401)\
**Post date:** [October 20, 2017, 9:05am UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/12 "2017-10-20T09:05:49Z")

</div>

Hi ,

I am sorry but I haven’t worked on this for almost two years so I am not able to help you. You have probably already try it but I suggest you to look the source code and debug step by step the construction and execution of the NavMesh and Pathfinding methods.

My apologies and good luck!

---

<div class="post-metadata">

**Author:** ![eblade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@eblade](https://forums.unrealengine.com/u/eblade)\
**Post date:** [June 2, 2022, 7:16pm UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/13 "2022-06-02T19:16:04Z")

</div>

> [@MilkyEngineer](#):
>
> Solution 3: Extending dtQueryFilter (Hard)

Not quite so hard, it turns out. I can’t share much code, but I will give a quick overview:

- Create a subclass of FRecastQueryFilter, we’ll call it FMyRecastQueryFilter
- Initialize it’s constructor like

```auto
FMyRecastQueryFilter::FMyRecastQueryFilter(bool bIsVirtual) : FRecastQueryFilter(true)
{}

```

- override either passVirtualFilter or getVirtualCost (see dtQueryFilter class for details)
- Create a subclass of UNavigationQueryFilter, we’ll call it UMyNavigationQueryFilter
- Set bIsMetaFilter = false in constructor
- override InitializeFilter to look like

```auto
Filter.SetFilterType<FMyRecastQueryFilter>();
Super::InitializeFilter(NavData, Querier, Filter);

```

- Set this filter (or a blueprint of it) as the filter to use as Default Filter in your AI controller, or use it in the various MoveTo constructs that accept query filters

NOTE: getVirtualCost and passVirtualFilter _will_ be called a LOT. Do not do heavy operations!

---

<div class="post-metadata">

**Author:** ![UPO33](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@UPO33](https://forums.unrealengine.com/u/UPO33)\
**Post date:** [June 9, 2024, 8:55pm UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/14 "2024-06-09T20:55:18Z")

</div>

they have written wrapper over wrapper with inheritance , poly morphism, public, private, etc that at the end simply calls functions of dtQueryFilter .  
but i managed to add my dirt on top of them 😃

```cpp
struct FMyRecastQueryFilter : public FRecastQueryFilter
{

	FMyRecastQueryFilter() {}
	{
		this->SetIsVirtual(true);
	}
	virtual bool passVirtualFilter(const dtPolyRef ref, const dtMeshTile* tile, const dtPoly* poly) const override
	{
		//using default impl
		return passInlineFilter(ref, tile, poly);
	}
	//dtQueryFilter.getVirtualCost 
	virtual dtReal getVirtualCost(const dtReal* pa, const dtReal* pb,
		const dtPolyRef prevRef, const dtMeshTile* prevTile, const dtPoly* prevPoly,
		const dtPolyRef curRef, const dtMeshTile* curTile, const dtPoly* curPoly,
		const dtPolyRef nextRef, const dtMeshTile* nextTile, const dtPoly* nextPoly) const override
	{
		const FVector& PointA = *reinterpret_cast<const FVector*>(pa); // sick C programmer rejected struct :D
		const FVector& PointB = *reinterpret_cast<const FVector*>(pb);
		//using default impl
		return getInlineCost(pa, pb,
			prevRef, prevTile, prevPoly,
			curRef, curTile, curPoly,
			nextRef, nextTile, nextPoly);
	}
};

FSharedConstNavQueryFilter QueryFilter = UNavigationQueryFilter::GetQueryFilter(*NavData, Querier, FilterClass);
TSharedRef<FNavigationQueryFilter> NewNavFillter = MakeShareable(new FNavigationQueryFilter); //lets make our own waste
NewNavFillter->SetFilterType<FMyRecastQueryFilter>();//allocates FMyRecastQueryFilter
float AreaCosts[RECAST_MAX_AREAS];
float FixedCosts[RECAST_MAX_AREAS];
QueryFilter->GetImplementation()->GetAllAreaCosts(AreaCosts, FixedCosts, RECAST_MAX_AREAS);
NewNavFillter->GetImplementation()->SetAllAreaCosts(AreaCosts, RECAST_MAX_AREAS); //copy the data back to new one
//copy other properties too ...

```

---

<div class="post-metadata">

**Author:** ![PazDim](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PazDim](https://forums.unrealengine.com/u/PazDim)\
**Post date:** [June 24, 2024, 3:20pm UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/15 "2024-06-24T15:20:05Z")

</div>

@UPO33 Thank you for this short tutorial, great work! I tried to implement it in my code and here are two results:

1. It really work, I modified **getVirtualCost** by adding little random multiplier and navimesh started to generate diffrenet paths with little randomization :). So I have full control over pathfinding cost.
2. I have problem with pa/pb parameters. I want to implement tactical pathfinding, so I need these parameters to correct final cost by distance to player, player’s sight etc. But looks like type const FVector::FReal\* has wrong description: “[(x, y, z)]”. I tried to draw these coordinates and got wrong results - axes was inverted. Now I use next translation:

```auto
FVector start = FVector(-pa[0], -pa[2], pa[1]);
FVector end = FVector(-pb[0], -pb[2], pb[1]);

```

It works for me. Do you have any similiar problems with coordinates?

---

<div class="post-metadata">

**Author:** ![UPO33](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@UPO33](https://forums.unrealengine.com/u/UPO33)\
**Post date:** [June 28, 2024, 3:19pm UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/16 "2024-06-28T15:19:15Z")

</div>

@PazDim  
search in source code, you need to use Unreal2RecastPoint, Recast2UnrVector

---

<div class="post-metadata">

**Author:** ![PazDim](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@PazDim](https://forums.unrealengine.com/u/PazDim)\
**Post date:** [July 25, 2024, 4:05pm UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/17 "2024-07-25T16:05:14Z")

</div>

Is there any way how to access to Owner controller from filter?

---

<div class="post-metadata">

**Author:** ![masscad](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@masscad](https://forums.unrealengine.com/u/masscad)\
**Post date:** [April 18, 2025, 5:47pm UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/18 "2025-04-18T17:47:18Z")

</div>

I guess the following could help somebody to save some time. This was my approach to manipulate pathfinding based on data from the game world (seems to work)…

Use case was to block a specific area for doing a single path finding query (Flanking ability).

Snippet from actor component at AIController which is responsible for flanking:

```auto
TArray<FVector> UFlankingComponent::FindFlankPathToLocation(FVector PathEndLocation, FVector ActualEnemyLocation)
{
    if (!AIController || !AIController->GetPawn())
    {
        return TArray<FVector>();
    }
    
    FVector PathStartLocation = AIController->GetPawn()->GetActorLocation();

    UNavigationSystemV1* NavSystem = UNavigationSystemV1::GetCurrent(AIController->GetWorld());
    ANavigationData* NavData = NavSystem->GetNavDataForProps(AIController->GetNavAgentPropertiesRef(), AIController->GetNavAgentLocation());

    AGoapShooterCharacter* Char = Cast<AGoapShooterCharacter>(AIController->GetPawn());
    Char->FlankEnemyLocation = ActualEnemyLocation; // temporarily save it there, so it can be accessed by the following QueryFilter
    FSharedConstNavQueryFilter QueryFilter = UNavigationQueryFilter::GetQueryFilter(*NavData, Char,
        UNavQueryFilter_EfficientAvoidFlanking::StaticClass());
    
    FPathFindingQuery Query(Char, *NavData, PathStartLocation,PathEndLocation, QueryFilter);
    Query.SetAllowPartialPaths(false);

    FPathFindingResult PathResult = NavSystem->FindPathSync(Query);

    if (!PathResult.IsSuccessful() || !PathResult.Path.IsValid())
    {
        return TArray<FVector>();
    }
    
    TArray<FVector> FoundPathPoints;
    for (const FNavPathPoint& Point : PathResult.Path->GetPathPoints())
    {
        FoundPathPoints.Add(Point.Location);
    }
    
    return FoundPathPoints;
}

```

NavQueryFilter\_EfficientAvoidFlanking.h

```auto
UCLASS()
class GOAPSHOOTER_API UNavQueryFilter_EfficientAvoidFlanking : public UNavigationQueryFilter
{
	GENERATED_BODY()

public:
	UNavQueryFilter_EfficientAvoidFlanking();

	virtual void InitializeFilter(const ANavigationData& NavData, const UObject* Querier, FNavigationQueryFilter& Filter) const override;
};

```

NavQueryFilter\_EfficientAvoidFlanking.cpp

```auto
UNavQueryFilter_EfficientAvoidFlanking::UNavQueryFilter_EfficientAvoidFlanking()
{
	bIsMetaFilter = false;
	bInstantiateForQuerier = true;
}

void UNavQueryFilter_EfficientAvoidFlanking::InitializeFilter(const ANavigationData& NavData, const UObject* Querier,
	FNavigationQueryFilter& Filter) const
{
	const AGoapShooterCharacter* Char = Cast<AGoapShooterCharacter>(Querier);
	const FFlankRecastQueryFilter FlankFilterImplementation(Char->GetActorLocation(), Char->FlankEnemyLocation);
	Filter.SetFilterImplementation(&FlankFilterImplementation);
	Super::InitializeFilter(NavData, Querier, Filter);
}

```

Somewhere accessable:

```auto
struct FFlankRecastQueryFilter : public FRecastQueryFilter
{
	// default constructor , dont use
	FFlankRecastQueryFilter() : FFlankRecastQueryFilter(FVector::ZeroVector, FVector::ZeroVector)
	{}
	
	FFlankRecastQueryFilter(const FVector& AILocation, const FVector& EnemyLocation)
		: FRecastQueryFilter(true)
		, AILocation(AILocation)
		, EnemyLocation(EnemyLocation)
	{
	}
	
	FVector AILocation;
	FVector EnemyLocation;
	
	virtual bool passVirtualFilter(const dtPolyRef ref, const dtMeshTile* tile, const dtPoly* poly) const override
	{
		// Standard inline filter (area flags, include/exclude, etc.)
		if (!passInlineFilter(ref, tile, poly)) {
			return false;
		}
		
		// Reject if any vertex of the nav mesh overlaps the avoidance area
		const int VertCount = poly->vertCount;
		for (int i = 0; i < VertCount; ++i)
		{
			// Recast coords -> Unreal world point
			const double* RecastPoint = &tile->verts[poly->verts[i] * 3 ]; 
			const FVector PointToTest = Recast2UnrealPoint(RecastPoint);

			bool bInside = IsPointInQuad2D(PointToTest, BuildAvoidanceArea(AILocation, EnemyLocation));

			if (bInside)
			{
				return false;
			}
		}

		return true;
	}

	// virtual dtReal getVirtualCost(const dtReal* pa, const dtReal* pb,
	// const dtPolyRef prevRef, const dtMeshTile* prevTile, const dtPoly* prevPoly,
	// const dtPolyRef curRef, const dtMeshTile* curTile, const dtPoly* curPoly,
	// const dtPolyRef nextRef, const dtMeshTile* nextTile, const dtPoly* nextPoly) const override
	// {
	// 	
	// dtReal BaseCost = getInlineCost(pa, pb,
	// prevRef, prevTile, prevPoly,
	// curRef, curTile, curPoly,
	// nextRef, nextTile, nextPoly);
	// 	
	// // could manipulate costs here instead of blocking the area...
	// FVector PointA(pa[0], pa[1], pa[2]);
	// FVector PointB(pb[0], pb[1], pb[2]);
	// PointA = Recast2UnrealPoint(PointA);
	// PointB = Recast2UnrealPoint(PointB);
	//
	// return BaseCost;
	// }

	virtual INavigationQueryFilterInterface* CreateCopy() const override
	{
		return new FFlankRecastQueryFilter(AILocation, EnemyLocation);
	};
};

```

---

<div class="post-metadata">

**Author:** ![Argamak\_Games](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Argamak\_Games](https://forums.unrealengine.com/u/Argamak_Games)\
**Post date:** [November 16, 2026, 3:20pm UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/19 "2026-11-16T15:20:26Z")

</div>

> [@anonymous\_user\_cc8f596b](#):
>
> when setting the SetFilterImplementation in our ARecastNavMesh, the nav modifiers like NavArea\_Obstacle (or any other custom implementation) are ignored, seems like the cost is always bypassed to be Default one. Still those marked as NavArea\_Null work.

This is because custom filter needs a bit of extra setup. Look in the engine source code, RecastNavMesh.cpp, line 444 (as of UE 5.8), the FNavMeshConfig::FRecastNamedFilterCreator() code. For default filters, it sets some NavLinkFlag (probably important for NavLinks) and also sets various area costs to “unwalkable” or “1.f”. You can do the same setup in your InitializeFilter(…). After that navarea “obstacle” will start working. In your getVirtualCost() do not forget to use Recast2UnrealPoint(…) and similar functions when analyzing vertex positions in Unreal coords system.

---

<div class="post-metadata">

**Author:** ![Argamak\_Games](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Argamak\_Games](https://forums.unrealengine.com/u/Argamak_Games)\
**Post date:** [November 16, 2026, 3:37pm UTC](https://forums.unrealengine.com/t/modify-pathfinding-cost-detournavmeshquery-subclass/342472/20 "2026-11-16T15:37:26Z")

</div>

> [@PazDim](#):
>
> Is there any way how to access to Owner controller from filter?

You should not access pawns from your actual filter implementation, like getVirtualCost() and similar methods. FRecastQueryFilter / dtQueryFilter work in heavily multithreaded scenarious. Accessing pawns from there would crash the engine (and would slow down the filter seriously, if it would not crash the engine). Store all required config in your Recast filter when creating it. What you can do is access your pawn in your UMyNavigationQueryFilter wrapper in InitializeFilter(…). The Querier pointer is usually a pointer to your AIController (unless your filter gets called by somebody else like NavigationTestActor). This gives you a chance to pass some config info from pawn to your actual FRecastQueryFilter. Do not forget to test that Querier is not null and is valid before casting it to your AIController class.
