AStar2D
Path search on an explicit graph of 2D points with directed edges and entry weights.
Create a graph, add points with AddPoint, connect them using ConnectPoints, and query GetIdPath or GetPointPath. These are route data: the engine creates no collision clearance, moves no character and does not automatically update graph positions from Entities. Dispose the owned instance.
ArePointsConnected accepts either direction by default; use false for a directed query. A partial path may not reach the goal: check its last ID. A disabled start produces no path even with partial enabled.
Transition cost is _ComputeCost(fromId, toId) × destination weightScale. Optimal search requires an admissible consistent heuristic. For arbitrary nonnegative costs, overriding _EstimateCost to return 0 is a clear starting strategy. Native defaults use Euclidean distance; calling C# base virtual methods does not retrieve that formula.
Small graphs, directed edges, weights, disabled points, partial paths, closest queries and C# callbacks were tested in Windows headless. Large graphs, concurrent search, physical clearance and other platforms were not accepted.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
AStar2DConstructor
public AStar2D();Creates an empty owned graph, not a level Entity or navigation mesh. Add points and edges explicitly. Dispose the instance when no longer needed.
Properties1
NeighborFilterEnabledProperty
public bool NeighborFilterEnabled { get; set; }Enables _FilterNeighbor calls while expanding neighbors; false by default. An override returning true rejects that transition. Does not alter GetPointConnections, ArePointsConnected or geometric closest queries.
Methods30
AddPointvoid
public void AddPoint(long id, Vector2 position, float weightScale = 1f);Adds a nonnegative ID at a finite position. weightScale is a finite nonnegative multiplier for entering this point, default 1. Reusing an ID updates position/weight while retaining edges and disabled state. Adds no edges by itself. Invalid IDs/negative weights produce native diagnostics; validate NaN/Infinity before calling.
| Name | Type | Default |
|---|---|---|
id | long | — |
position | Vector2 | — |
weightScale | float | 1f |
idExisting nonnegative ID; AddPoint also accepts a new ID. Not an array index.
positionFinite position in your chosen graph coordinate system.
weightScaleFinite entry-cost multiplier ≥0. Check the heuristic for weights <1.
ArePointsConnectedbool
public bool ArePointsConnected(long id, long toId, bool bidirectional = true);Checks a direct edge rather than route reachability. Crucially, bidirectional=true accepts an edge in EITHER direction; it does not require two edges. false tests only id→toId. To require two-way connectivity, query both directions with false. Disabled state and neighbor filtering do not change this query.
| Name | Type | Default |
|---|---|---|
id | long | — |
toId | long | — |
bidirectional | bool | true |
idExisting nonnegative ID; AddPoint also accepts a new ID. Not an array index.
toIdAnother existing point ID; for path queries, the goal may be unreachable.
bidirectionalFor ArePointsConnected, true accepts either direction; for ConnectPoints/DisconnectPoints it acts on both. false always means id→toId.
boolWhether a direct edge satisfies the selected direction rule, not route reachability.
Clearvoid
public void Clear();Removes all points and edges and resets the available-ID candidate to 0. Existing C# result arrays remain snapshots. Does not promise to release all reserved memory.
ConnectPointsvoid
public void ConnectPoints(long id, long toId, bool bidirectional = true);Connects two existing distinct points: both directions by default, or only id→toId with false. Reconnecting does not duplicate a neighbor. Checks no collision/geometry clearance and moves no character.
| Name | Type | Default |
|---|---|---|
id | long | — |
toId | long | — |
bidirectional | bool | true |
idExisting nonnegative ID; AddPoint also accepts a new ID. Not an array index.
toIdAnother existing point ID; for path queries, the goal may be unreachable.
bidirectionalFor ArePointsConnected, true accepts either direction; for ConnectPoints/DisconnectPoints it acts on both. false always means id→toId.
DisconnectPointsvoid
public void DisconnectPoints(long id, long toId, bool bidirectional = true);Removes an edge between existing points. true removes both directions; false removes only id→toId, retaining the reverse edge if present. Points remain. Does not create a missing edge.
| Name | Type | Default |
|---|---|---|
id | long | — |
toId | long | — |
bidirectional | bool | true |
idExisting nonnegative ID; AddPoint also accepts a new ID. Not an array index.
toIdAnother existing point ID; for path queries, the goal may be unreachable.
bidirectionalFor ArePointsConnected, true accepts either direction; for ConnectPoints/DisconnectPoints it acts on both. false always means id→toId.
GetAvailablePointIdlong
public long GetAvailablePointId();Returns the current available nonnegative candidate. Does not reserve it: repeated calls without AddPoint can return the same value. Removed IDs can be reused; this is neither a global UUID nor a promise of the smallest free ID.
longAvailable candidate ID without reservation.
GetClosestPointlong
public long GetClosestPoint(Vector2 toPosition, bool includeDisabled = false);Finds the geometrically closest point, breaking equal-distance ties by smaller ID. Excludes disabled points by default. Does not test routes, edges or neighbor filtering. Source initializes squared-distance threshold to 1e20: extreme coordinates require separate validation.
| Name | Type | Default |
|---|---|---|
toPosition | Vector2 | — |
includeDisabled | bool | false |
toPositionFinite geometric-query position in the same coordinate system as graph points.
includeDisabledtrue includes disabled candidates in the closest-point query; false by default.
longGeometrically nearest eligible candidate ID, or −1 if none was found.
GetClosestPositionInSegmentVector2
public Vector2 GetClosestPositionInSegment(Vector2 toPosition);Projects onto the closest existing segment with both endpoints enabled. This is geometric: edge direction and neighbor filtering do not determine the projection. Returns Vector2.Zero when no eligible segment exists; zero is also a valid position, not a reliable failure marker. Do not depend on segment order for distance ties.
| Name | Type | Default |
|---|---|---|
toPosition | Vector2 | — |
toPositionFinite geometric-query position in the same coordinate system as graph points.
Vector2Projected position; Vector2.Zero is also used when no segment was found.
GetIdPath2 overloads
public long[] GetIdPath(long fromId, long toId);Returns route IDs including fromId and toId. Both IDs must exist. No reachable route yields an empty array; enabled fromId==toId yields one ID, while a disabled start yields empty even with partial enabled. allowPartialPath=true permits a path to the best reached candidate by heuristic then travel cost; its endpoint may differ from toId. A disabled goal is not visited. Check the last ID before declaring arrival. Do not run concurrent searches/mutations on one instance.
| Name | Type | Default |
|---|---|---|
fromId | long | — |
toId | long | — |
fromIdExisting path-start ID or callback source-point ID.
toIdAnother existing point ID; for path queries, the goal may be unreachable.
long[]New long[] ordered from start to end; empty without a result, and a partial endpoint may not be the goal.
public long[] GetIdPath(long fromId, long toId, bool allowPartialPath = false);Returns route IDs including fromId and toId. Both IDs must exist. No reachable route yields an empty array; enabled fromId==toId yields one ID, while a disabled start yields empty even with partial enabled. allowPartialPath=true permits a path to the best reached candidate by heuristic then travel cost; its endpoint may differ from toId. A disabled goal is not visited. Check the last ID before declaring arrival. Do not run concurrent searches/mutations on one instance.
| Name | Type | Default |
|---|---|---|
fromId | long | — |
toId | long | — |
allowPartialPath | bool | false |
fromIdExisting path-start ID or callback source-point ID.
toIdAnother existing point ID; for path queries, the goal may be unreachable.
allowPartialPathtrue permits ending at a reached candidate when the goal is inaccessible. Does not guarantee reaching toId or allow a disabled start.
long[]New long[] ordered from start to end; empty without a result, and a partial endpoint may not be the goal.
GetPointCapacitylong
public long GetPointCapacity();Returns internal point-storage capacity, not the number of points or a path-length limit. Use GetPointCount for the count.
longCurrent internal-storage capacity rather than count.
GetPointConnectionslong[]
public long[] GetPointConnections(long id);Returns outgoing neighbor IDs for an existing point. The other end of a one-way edge can have an empty list. Includes disabled/filtered neighbors: this describes graph structure rather than eligible transitions in the current search. Do not assume sorted order.
| Name | Type | Default |
|---|---|---|
id | long | — |
idExisting nonnegative ID; AddPoint also accepts a new ID. Not an array index.
long[]New long[] of outgoing IDs with no sorting promise.
GetPointCountlong
public long GetPointCount();Returns the number of existing points, including disabled ones. Not the edge count or capacity.
longNumber of existing points, including disabled ones.
GetPointIdslong[]
public long[] GetPointIds();Returns a snapshot of all IDs, including disabled points. IDs can be sparse and exceed int.MaxValue; do not use them as point-array indices without your own mapping. Order is not guaranteed to be sorted.
long[]New long[] containing every ID, with no sorting promise.
GetPointPath2 overloads
public Vector2[] GetPointPath(long fromId, long toId);Returns path-point positions in the coordinate system supplied to AddPoint. Complete/partial-path rules match GetIdPath; this is neither a smoothed trajectory nor collision clearance. Use the ID path to verify arrival for a partial query. Mutating the returned C# array does not edit the graph. Do not query one instance concurrently.
| Name | Type | Default |
|---|---|---|
fromId | long | — |
toId | long | — |
fromIdExisting path-start ID or callback source-point ID.
toIdAnother existing point ID; for path queries, the goal may be unreachable.
Vector2[]New Vector2[] ordered from start to end; empty without a result.
public Vector2[] GetPointPath(long fromId, long toId, bool allowPartialPath = false);Returns path-point positions in the coordinate system supplied to AddPoint. Complete/partial-path rules match GetIdPath; this is neither a smoothed trajectory nor collision clearance. Use the ID path to verify arrival for a partial query. Mutating the returned C# array does not edit the graph. Do not query one instance concurrently.
| Name | Type | Default |
|---|---|---|
fromId | long | — |
toId | long | — |
allowPartialPath | bool | false |
fromIdExisting path-start ID or callback source-point ID.
toIdAnother existing point ID; for path queries, the goal may be unreachable.
allowPartialPathtrue permits ending at a reached candidate when the goal is inaccessible. Does not guarantee reaching toId or allow a disabled start.
Vector2[]New Vector2[] ordered from start to end; empty without a result.
GetPointPositionVector2
public Vector2 GetPointPosition(long id);Reads the stored position of an existing ID. Graph positions do not automatically track Entity2D. A missing ID produces diagnostics and a zero fallback, so check HasPoint first.
| Name | Type | Default |
|---|---|---|
id | long | — |
idExisting nonnegative ID; AddPoint also accepts a new ID. Not an array index.
Vector2Stored Vector2; diagnosed zero fallback for an invalid ID.
GetPointWeightScalefloat
public float GetPointWeightScale(long id);Reads the entry-cost multiplier of an existing point. Not an individual edge cost or heuristic. For an invalid ID, a diagnosed zero fallback must not be treated as a valid weight.
| Name | Type | Default |
|---|---|---|
id | long | — |
idExisting nonnegative ID; AddPoint also accepts a new ID. Not an array index.
floatEntry multiplier; diagnosed zero fallback for an invalid ID.
HasPointbool
public bool HasPoint(long id);Tests ID existence independently of disabled state and edges. Use before queries requiring an existing point.
| Name | Type | Default |
|---|---|---|
id | long | — |
idExisting nonnegative ID; AddPoint also accepts a new ID. Not an array index.
booltrue if the ID exists, including disabled points.
IsNeighborFilterEnabledbool
public bool IsNeighborFilterEnabled();Reads whether searches invoke _FilterNeighbor. The flag neither guarantees an override exists nor changes stored edges.
boolCurrent NeighborFilterEnabled value.
IsPointDisabledbool
public bool IsPointDisabled(long id);Reads the disabled state of an existing point. A disabled start cannot begin a search; disabled neighbors are skipped. Not an ID-existence test.
| Name | Type | Default |
|---|---|---|
id | long | — |
idExisting nonnegative ID; AddPoint also accepts a new ID. Not an array index.
boolDisabled flag of an existing point; an invalid ID is not an ordinary result.
RemovePointvoid
public void RemovePoint(long id);Removes an existing ID and its incoming and outgoing edges. Disabling instead retains graph structure. The removed ID can become the next GetAvailablePointId candidate.
| Name | Type | Default |
|---|---|---|
id | long | — |
idExisting nonnegative ID; AddPoint also accepts a new ID. Not an array index.
ReserveSpacevoid
public void ReserveSpace(long numNodes);Reserves storage for a positive point count without creating points. An allocation setting, not grid generation or a maximum-ID limit. Zero/negative values produce diagnostics; do not use it as a shrink command.
| Name | Type | Default |
|---|---|---|
numNodes | long | — |
numNodesPositive desired point-storage capacity, not a count of Entities to create.
SetNeighborFilterEnabledvoid
public void SetNeighborFilterEnabled(bool enabled);Sets the same flag as NeighborFilterEnabled. Enable it to invoke _FilterNeighbor; an override alone is insufficient.
| Name | Type | Default |
|---|---|---|
enabled | bool | — |
enabledEnables/disables search-filter calls, not graph points themselves.
SetPointDisabledvoid
public void SetPointDisabled(long id, bool disabled = true);Disables an existing point by default; false enables it again. The point and edges remain. Disabling neither removes the ID nor tests its existence.
| Name | Type | Default |
|---|---|---|
id | long | — |
disabled | bool | true |
idExisting nonnegative ID; AddPoint also accepts a new ID. Not an array index.
disabledtrue disables the point; false restores it to searches.
SetPointPositionvoid
public void SetPointPosition(long id, Vector2 position);Changes an existing point position for subsequent queries and default costs. Edges remain; previously returned path arrays do not update. Supply finite coordinates in one consistent coordinate system.
| Name | Type | Default |
|---|---|---|
id | long | — |
position | Vector2 | — |
idExisting nonnegative ID; AddPoint also accepts a new ID. Not an array index.
positionFinite position in your chosen graph coordinate system.
SetPointWeightScalevoid
public void SetPointWeightScale(long id, float weightScale);Changes the finite nonnegative entry-cost multiplier. It multiplies _ComputeCost even for C# overrides; the start has no separate entry charge. Values below 1 can make the default Euclidean heuristic overestimate: configure an admissible consistent heuristic, such as 0 with nonnegative costs, for reliable search.
| Name | Type | Default |
|---|---|---|
id | long | — |
weightScale | float | — |
idExisting nonnegative ID; AddPoint also accepts a new ID. Not an array index.
weightScaleFinite entry-cost multiplier ≥0. Check the heuristic for weights <1.
_ComputeCostfloat
public virtual float _ComputeCost(long fromId, long toId);Overridable base cost for a directed transition between connected points. The engine additionally multiplies it by the destination weightScale. Native behavior without an override is Euclidean distance. C# base._ComputeCost returns default(float), not the native formula: explicitly compute GetPointPosition(fromId).DistanceTo(GetPointPosition(toId)) for distance. Return a finite nonnegative cost.
| Name | Type | Default |
|---|---|---|
fromId | long | — |
toId | long | — |
fromIdExisting path-start ID or callback source-point ID.
toIdAnother existing point ID; for path queries, the goal may be unreachable.
floatFinite nonnegative base cost; the engine still applies destination weight.
_EstimateCostfloat
public virtual float _EstimateCost(long fromId, long endId);Overridable remaining-cost heuristic, not a charged transition cost. Native search without an override uses Euclidean distance. Optimal routes require an admissible consistent estimate; closed points are not reopened. With nonnegative costs, return 0 avoids overestimation but can expand the search. C# base._EstimateCost itself returns default(float), not native distance.
| Name | Type | Default |
|---|---|---|
fromId | long | — |
endId | long | — |
fromIdExisting path-start ID or callback source-point ID.
endIdExisting goal ID used to estimate remaining cost.
floatFinite consistent lower-bound estimate of remaining cost.
_FilterNeighborbool
public virtual bool _FilterNeighbor(long fromId, long neighborId);Override to reject fromId→neighborId: true skips the neighbor, false permits ordinary processing. Called only with NeighborFilterEnabled=true. Do not mutate the graph from the callback or mistake true for permission. A search filter, not a physics collision test.
| Name | Type | Default |
|---|---|---|
fromId | long | — |
neighborId | long | — |
fromIdExisting path-start ID or callback source-point ID.
neighborIdExisting ID of the outgoing neighbor being considered.
booltrue rejects this transition; false does not filter it.