Dear Community,
I recently wrote a wiki on how to add to the UE4 C++ AI path following system, to add in custom pathing coding!
UE4 Wiki Link: Writing Custom C++ AI Code
https://wiki.unrealengine./AI_Navigation_in_C%2B%2B,_Customize_Path_Following_Every_Tick
Well I now have my best demo yet of the effectiveness of coding structure!
I prove to you in the video that I am using just multi-threaded C++ to dynamically calculate AI Jump paths for my AI units to follow the player through many complex jumping sequences!
- I am using just C++ coding, no helpers in the editor!
2.** In thread I share my own UE4 C++ code with you** for how to get all the nav polys, the actual portions of the nav mesh so that you can do your own fancy custom UE4 C++ AI pathing code!
- In thread I also show you how I know when the UE4 pathing system can’t find a way to the player without doing jumps!
**Video: 's Multi-Threaded C++ AI Jump Pathing**
https://youtube./watch?v=sMMSQdnyt6o
Once again, I am doing all the jumping calculations **dynamically via C++** using the nav areas and my custom path following component!
Mult-Threaded
The code I use in video is multi-threaded using the UE4 C++ Task Graph system:
UE4 Wiki Link: UE4 Multi-threading
https://wiki.unrealengine./Multi-Threading:_Task_Graph_System
**C++ Code For You**
I inject my custom AI jump pathing code in way:
```
/*
Custom UE4 C++ AI Path Follow Component
By
*/
#pragma once
//Super
#include "Navigation/PathFollowingComponent.h"
UCLASS()
class UJoyPathFollowComp : UPathFollowingComponent
{
GENERATED_BODY()
:
UJoyPathFollowComp(const FObjectInitializer& ObjectInitializer);
/** follow current path segment */
virtual void FollowPathSegment(float DeltaTime) override;
};
```
```
void UJoyPathFollowComp::FollowPathSegment(float DeltaTime)
{
//Pointer Safety Checks
if (MovementComp == NULL || !Path.IsValid())
{
return;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Use Jump/Fall Pathing?
//~~~~~~~~~~~~~~~~~~~~~~~~~~~
if(Path->IsPartial()) //AI could not reach player, try using jump pathing!
{
//I send out instructions to my custom character class here
JoyChar->ReceiveJumpFallPathingRequest();
return;
//~~~
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Proceed normally (no jump pathing)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Super::FollowPathSegment(DeltaTime);
}
```
Explanation
FollowPathSegment is the UE4 Path Follow tick function, and so when you want to add completely custom coding you can use function as your starting point to adjust normal UE4 pathing behavior!
Path is Partial
The way I tell whether I need to try using custom jump pathing code is whether or not the UE4 Pathing system has detected a partial path!
Then my AI jump code runs and teaches the AI to get to the player using C++ calculated Jumps!
if(Path->IsPartial()) //AI could not reach player, try using jump pathing!
**How to Get all the UE4 Nav Polys**
In the video above you will see I get all the UE4 Nav Polys in order to do all my C++ AI Jump calculations!
Here's the function I wrote to do , just for you!
```
**// UE4 C++ AI Code for you!
// add to your custom path follow component!**
**//Nav Data **
FORCEINLINE const ANavigationData* **GetMainNavData**(FNavigationSystem::ECreateIfEmpty CreateNewIfNoneFound)
{
UNavigationSystem* NavSys = GetWorld()->GetNavigationSystem();
if(!NavSys) return NULL;
return NavSys->GetMainNavData(CreateNewIfNoneFound);
}
**//Choose Which Nav Data To Use**
FORCEINLINE const ANavigationData* **JoyGetNavData()** const
{
if(!MovementComp)
{
return GetMainNavData();
}
const FNavAgentProperties& AgentProperties = MovementComp->GetNavAgentPropertiesRef() ;
const ANavigationData* NavData = GetNavDataForProps(AgentProperties) ;
if (NavData == NULL)
{
VSCREENMSG("ERROR USING NAV DATA");
NavData = GetMainNavData();
}
return NavData;
}
//VERY IMPORTANT FOR CRASH PROTECTION !!!!!
FORCEINLINE bool **TileIsValid**(const ARecastNavMesh* NavMesh,int32 TileIndex) const
{
if(!NavMesh) return false;
//~~~~~~~~~~~~~~
const FBox TileBounds = NavMesh->GetNavMeshTileBounds(TileIndex);
return TileBounds.IsValid != 0;
}
bool **NavPoly_GetAllPolys**(TArray<NavNodeRef>& Polys);
```
```
**//'s UE4 Nav code to get all the nav polys!**
bool UJoyPathFollowComp::****NavPoly_GetAllPolys****(TArray<NavNodeRef>& Polys)
{
if(!MovementComp) return false;
//~~~~~~~~~~~~~~~~~~
//Get Nav Data
const ANavigationData* NavData = JoyGetNavData();
const ARecastNavMesh* NavMesh = Cast<ARecastNavMesh>(NavData);
if(!NavMesh)
{
return false;
}
TArray<FNavPoly> EachPolys;
for(int32 v = 0; v < NavMesh->GetNavMeshTilesCount(); v++)
{
//CHECK IS VALID FIRST OR WILL CRASH!!!
// 256 entries but only few are valid!
// using continue in case the valid polys are not stored sequentially!
if(****!TileIsValid****(NavMesh,v))
{
continue;
}
NavMesh->GetPolysInTile(v,EachPolys);
}
//Add them all!
for(int32 v = 0; v < EachPolys.Num(); v++)
{
Polys.Add(EachPolys[v].Ref);
}
}
```
Enjoy!
Have fun writing custom UE4 AI C++ code and getting all the nav polys so you can truly do whatever you want with UE4’s awesome navigation system!
https://www.mediafire./convkey/a416/xh2a0w6qocsc9xb6g.jpg