Interface IEdgeMod<TNode>
Namespace: AnyPath.Native
Assembly: AnyPath.dll
Syntax
public interface IEdgeMod<TNode> where TNode : unmanaged, IEquatable<TNode>
Type Parameters
| Name | Description |
|---|---|
| TNode |
Methods
ModifyCost(in TNode, in TNode, ref float)
Allows for modification of the cost of a path segment, or totally discarding it as a possibility. This is called during the A* algorithm for every segment between locations A* encounters.
Declaration
bool ModifyCost(in TNode from, in TNode to, ref float cost)
Parameters
| Type | Name | Description |
|---|---|---|
| TNode | from | The source node for this edge |
| TNode | to | The other end of the edge |
| float | cost | Modify this parameter to alter the cost of the segment. Be careful as to not make the cost lower than a value that the heuristic function of the graph would calculate, as this can result in sub optimal paths. For some graph types this may not be immediately noticable but for true graph structures, providing a lower cost than the heuristic may cause the path to contain detours that look strange. |
Returns
| Type | Description |
|---|---|
| bool | When you return false, the edge is discarded as a possibility. This can be used for instance to simulate a closed door. |
Remarks
The default NoProcessing does not touch the cost and returns true. Keeping the original graph as is. The burst compiler is smart enough to detect this and totally discard this method
ModifyEdgeBuffer(in TNode, ref NativeList<Edge<TNode>>)
Allows for modifying the edge buffer after it is filled by the Collect(TNode, ref NativeList<Edge<TNode>>) implementation. This is called every time the algorithm visits a node and right after the default graph implementation has filled it. Leave this method empty if you don't need it. The Burst compiler will strip the code away and this is no impact on performance.
This can be used to create "portals" that aren't part of the default graph. Here's an example for the square grid:
private SquareGridCell portalFrom;
private SquareGridCell portalTo;
public void ModifyEdgeBuffer(in SquareGridCell from, ref NativeList{Edge{SquareGridCell}} edgeBuffer)
{
// One portal, but we could also use a hashset to match against many.
// If we're on the portal position, add an edge/link to the portal destination.
if (from.Equals(portalFrom))
edgeBuffer.Add(new Edge{SquareGridCell}(portalTo, cost: 1));
}
You can use the generic readymade AdditionalEdges<TNode> modifier for this use case which uses a hashmap internally. If your use case is more specific, there may be more better ways to do it (as the one illustrated above, where there is only one portal).
You can also add other dynamic stuff, or remove edges from the default implementation. While you can remove, it may be more performant to use ModifyCost(in TNode, in TNode, ref float) and return false to discard an edge.
Declaration
void ModifyEdgeBuffer(in TNode from, ref NativeList<Edge<TNode>> edgeBuffer)
Parameters
| Type | Name | Description |
|---|---|---|
| TNode | from | The node the algorithm is currently at. Similar to Collect(TNode, ref NativeList<Edge<TNode>>). |
| NativeList<Edge<TNode>> | edgeBuffer | Pre filled list containing the edges that originate from from. |
Remarks
This can be called multiple times for the same from node. It should act the same every time within a single pathfinding query.