Polarith AI
1.7
Path

Besides the comfortable inbuilt components Follow Waypoints, Linear Path and Unity Pathfinding, one particular strength of our path system is its extendability which allows you to easily combine your custom pathfinding solution and/or your custom path structure with behaviours derived from AIMFollowPath, like AIMFollowWaypoints. The following diagram should provide you a basic overview of the system.

Figure 1: Basic class diagram showing the software design of the path system.

The integration can easily be done by inheriting from AIMPathConnector. This abstract base class is intended for transferring your custom path (way)points to the behaviours derived from AIMFollowPath. Note that it makes no difference whether the points are calculated by a pathfinding solution or if they come from pre-defined path structures. The data flow is ensured by the method GetPoints, whereby every behaviour of type AIMFollowPath can have at least one AIMPathConnector for retrieving the point data.

Of course, there are a few important aspects which needs to be considered during the implementation depending on your concrete scenario. These are described by the examples in the following two sections. First, how to combine your custom path data with the inbuilt behaviours. Second, how to properly integrate your custom pathfinding solution.

How to Integrate Custom Path Data

For the use of custom path data, you need to provide a data adapter derived from AIMPathConnector which implements the protected property points so that it returns custom points in global coordinates which are of Unity's type Vector3. Note that your script will automatically be a component due to the fact that AIMPathConnector inherits from MonoBehaviour. The points in your custom class needs to be provided in a collection of type IList. This can be, for example, an ordinary list as well as a pure array. The following code snippet demonstrates an example for such an implementation.

using Polarith.AI.Move;
using System.Collections.Generic;
using UnityEngine;
public class CustomPath : AIMPathConnector
{
// Your custom list storing the points in global coordinates
[SerializeField]
private List<Vector3> waypoints;
// This property is called by the method AIMPathConnector.GetPoints
// for transferring the point data
protected override IList<Vector3> points
{
get { return waypoints; }
}
}

If you want to exchange or modify your path at runtime, there are two other important things which have to be considered. First, GetPoints returns always a copy of the provided points. So if you want to avoid the memory allocation, you can use the method GetPointsNonAlloc. instead. Second, if the path data has been modified or exchanged by your path system, then you have to call the delegate PathChanged. This delegate can be used to register additional functionality, for example, it notifies Follow Path behaviours that the path has updated.

How to Integrate Custom Pathfinding

The integration of a custom pathfinding solution is quite similar to the integration of custom point data. Please, have a look at the given class diagram at the top of this page. As you can see, we have also provided a class especially for custom pathfinding called AIMPathfinding. There are three major aspects to care about when deriving from this class. First, you have to implement the method CalculatePath. Within this method, your (asynchronous) pathfinding algorithm needs to be started. Second, you have to call the delegate PathChanged after the pathfinding process has finished, whereby the Update method is intended to be used for checking this. The delegate is applied for notifying other components using the path that there was an update. Third, like for custom point data, you have to implement the points property returning the solution of the pathfinding algorithm. When you do these things, you will come up with a script which should be similar to the following example.

using Polarith.AI.Move;
using System.Collections.Generic;
using UnityEngine;
// Class using/containing your custom pathfinding solution
public class CustomPathfinding : AIMPathfinding
{
// Your custom pathfinding back-end
[SerializeField]
private Pathfinding pathfinding;
// Transfers the computed waypoints
protected override IList<Vector3> points
{
get { return pathfinding.waypoints(); }
}
// Starts the computation of the path
public override void CalculatePath(Vector3 destination)
{
pathfinding.CalculatePath(destination);
}
// Checks if the computation has finished
protected override void Update()
{
base.Update();
// Notify the related components that there was a data update
if (pathfinding.PathIsReady)
PathChanged();
}
}
Imprint