5.4 Extending Constraints

Hi there @aron_granberg
I have a quick question on the new struct-based constraints. How are we now supposed to extend constraints? With the class-based approach, it was quite easy, and I used it frequently. Now I am a bit confused. Maybe introducing an interface would be more convenient in this case (I guess that partials and roslyn generator is out of the question, right)?

  • A* version: 5.4
  • Unity version: 6000.1.7f1

Hi

The new constraints have a filter property which calls an arbitrary delegate to filter.

var nearestNodeconstraint = NearestNodeConstraint.None;

// Set a custom filter. This can be an arbitrary function which takes a GraphNode and returns true or false.
nearestNodeconstraint.filter = (GraphNode node) => ((Vector3)node.position).y > 50;

// Find the closest node to our position which is above y=50
var nearest = AstarPath.active.GetNearest(transform.position, nearestNodeConstraint);

An interface couldn’t be used, because that would also require boxing and the GC in the common case, which is what I want to avoid.

1 Like