Skip to content

Pathfinder

Implements A* algorithm: - with one modification that target point can be located not only in a node but also in some point between 2 nodes. - Works on graph constructed with Entities and uses following components: Node (a vertex), Edge (a line that connects two nodes) and EdgeRef - Heuristic and cost modifiers have to be provided externally by providing own module in exchange or in addition to PathfinderBasicEuclideanModule. - Can be used directly, but preferred way is to create request PathfinderRequest which is processed by the PathfinderSystem.

How to add in bootstrap

AddModule<ConnectionsModule>(); // requires also connections module because it provides the graph
AddModule<PathfinderModule>();
AddModule<PathfinderBasicEuclideanModule>(); // can by replaced, or you can add more cos modifiers.

How to provide custom cost modifier

See how PathfinderBasicEuclideanModule, EuclideanDistanceHeuristic and EuclideanCostMultiplier are made.

How to request a path:

Entity requestEntity = EntityManager.CreateEntity(typeof(PathfinderRequest), typeof(MyTag));
PathfinderRequest request = new(requestEntity, start, target, 5f);
EntityManager.SetComponentData(requestEntity, request);

How to wait and read the result:

  • Make a query
  • iterate over entities,
  • remove the path if don't need it anymore.
    EntityCommandBuffer ecb = new EntityCommandBuffer(Allocator.Temp);
    Entities.WithAll<MyTag>().ForEach((Entity entity, DynamicBuffer<PathfinderResult> path) => {
        ecb.DestroyEntity(entity);
    }).Run();
    ecb.Playback(EntityManager);
    
    or:
    EntityCommandBuffer ecb = new EntityCommandBuffer(Allocator.Temp);
    foreach ((DynamicBuffer<PathfinderResult> path, Entity entity)
            in SystemAPI.Query<DynamicBuffer<PathfinderResult>>().WithAll<MyTag>().WithEntityAcccess()) {
        ecb.DestroyEntity(entity);
    }
    ecb.Playback(EntityManager);
    

Handle errors, not found paths

EntityCommandBuffer ecb = new EntityCommandBuffer(Allocator.Temp);
foreach ((PathfinderError error, Entity entity)
        in SystemAPI.Query<PathfinderError>().WithAll<MyTag>().WithEntityAccess()) {
    // do something with error.Code
    ecb.DestroyEntity(entity);
}
ecb.Playback(EntityManager);