UPathFindingComponent¶
Pathfinding in MegaGrid is managed by this actor component.
Variables¶
bool bIsHex;
- Tells pathfinding whether to use hex calculations.
int32 MaxIterations = 25000;
- Maximum pathfinding iterations. When exceeded, returns an empty path.
int32 MaxPathLength = 300;
- Maximum tiles in path. When exceeded, returns an empty path.
bool bUseHeap;
- Whether to use heap optimization. May increase accuracy.
TArray<FName> TypesToBlock;
- Types that should be treated as obstacles without affecting calculations.
TArray<FName> TypesToIgnore;
- Types that should be considered as having zero movement cost.
TArray<FName> StatesToBlock;
- States that should be treated as obstacles.
UDataTable* TileTypeMapping;
- Data table of TileTypeMapping
, used for getting movement costs.
bool bUseDiagonals = false;
- Whether to use diagonals for square grids.
bool bUseWeightedDiagonals = false;
- Whether to use weighted diagonals.
float DiagonalCost = 1;
- Higher cost means less diagonal paths.
TMap<FName, float> TypeMovementCostMap;
- Precomputed movements costs for each tile type.
bool bUseMovementCost = true;
- Whether to use movement costs? False = faster pathfinding, less accuracy.
float DistanceBias = 0;
- How much bias does the pathfinder have towards Distance? Lower = Higher Accuracy (Slower). Higher = Lower Accuracy (Faster).
float HeuristicSizeMultiplier = 500;
- Multiplier for influencing Distance. Lower for smaller tile counts, vice versa
float MovementCostBias = 1;
- How much bias does the pathfinder have towards MovementCost (GCost)? Lower = Higher Accuracy(Slower), Higher = Lower Accuracy(Faster)
FRunnable* CurrentRunnable;
- Runnable used for async pathfinding.
FRunnableThread* CurrentThread;
- Thread used for async pathfinding.
IsGridGenerated()¶
Returns a bool indicating if the grid has been populated with tiles.
bool IsGridGenerated();
GetDistance()¶
Heuristic or distance function. Returns the distance between two tiles.
float GetDistance(FIntPoint CurrentIndex, FIntPoint TargetIndex);
Inputs
CurrentIndex
- Index of start tile.
TargetIndex
- Index of target / end tile.
GetHexDistance()¶
Heuristic or distance function for hexagonal grid. Returns the distance between two tiles.
float GetHexDistance(FIntPoint CurrentIndex, FIntPoint TargetIndex);
Inputs
CurrentIndex
- Index of start tile.
TargetIndex
- Index of target / end tile.
GetLowestFCostNode¶
Returns the tile data of the lowest F Cost in the given Open List.
FTileData GetLowestFCostNode(TArray<FTileData>& OpenList) const;
Inputs
OpenList
- Tile Data array reference.
GeneratePath()¶
Generates or retraces path if pathfinding was successful. Returns the tile indices of the final path.
TArray<FIntPoint> GeneratePath(FIntPoint StartIndex, FIntPoint TargetIndex,
TMap<FIntPoint, FTileData>& GridData);
Inputs
CurrentIndex
- Index of start tile.
TargetIndex
- Index of target / end tile.
GridData
- GridData reference.
IsTileWalkable()¶
Returns a bool indicating if a given tile is walkable.
bool IsTileWalkable(FIntPoint TileIndex, TMap<FIntPoint, FTileData>& GridData);
Inputs
TileIndex
- Index of tile.
GridData
- GridData reference.
IsTileValid()¶
Returns a bool indicating if a given tile is walkable. Specific to Blueprint.
bool IsTileValid(FIntPoint TileIndex);
Inputs
TileIndex
- Index of tile.
GetPrecomputedNeighbors()¶
Gets precomputed neighbors for the given tile.
Warning
This is not used in the current implementation. If you want to use this, precompute neighbors during grid generation. Not for async, will crash if used.
TArray<FIntPoint> GetPrecomputedNeighbors(FIntPoint TileIndex);
Inputs
TileIndex
- Index of tile.
StopPathFindingThread();¶
Stops and clears the current async pathfinding thread.
void StopPathFindingThread();
FPathCompleteDelegate¶
Blueprint compatible event triggered when async pathfinding completes. Returns an FPathStruct
.
FPathCompleteDelegate OnPathComplete;
FPathFollowDelegate¶
Blueprint compatible event triggered during each iteration of StartMovingOnPath()
. Returns an FTransform
.
FPathFollowDelegate FollowPath;
PrecomputeMovementCosts()¶
Preloads movement costs from TileTypeMapping
data table into TypeMovementCostMap
.
void PrecomputeMovementCosts();
FindPath()¶
Finds a path between two tiles. Returns an FPathStruct
containing the final
path array and a EPathCompleteReason
. Synchronous function.
Note
Use in low frequencies.
FPathStruct FindPath(FIntPoint StartIndex, FIntPoint TargetIndex);
Inputs
StartIndex
- Index of start tile.
TargetIndex
- Index of target / end tile.
FindPathAsync()¶
Finds a path between two tiles. Asynchronous function, triggers the OnPathComplete
event upon completion. Which in turn returns an
FPathStruct
.
void FindPathAsync(FIntPoint StartIndex, FIntPoint TargetIndex);
StartIndex
- Index of start tile.
TargetIndex
- Index of target / end tile.
GetTotalMovementCost()¶
Returns the total movement cost of a given path. Use this to calculate how much points are needed to traverse a path.
float GetTotalMovementCost(TArray<FIntPoint> Path);
Inputs
Path
- Path array.
PathMovementTimerHandle¶
Timer to handle delays for StartMovingOnPath()
.
FTimerHandle PathMovementTimerHandle;
StartMovingOnPath()¶
Moves along the given path. Triggers the FollowPath
event which returns a FTransform
.
void StartMovingOnPath(TArray<FVector> Path, double Interval, double LerpSpeed, double Delay);
Inputs
Path
- Path array.
Interval
- Interval between the each timer call.
LerpSpeed
- Speed of movement.
Delay
- Delay between each movement, increase for tile-to-tile movement.