Edit: Just saw the message about the entities package. Sorry if this is caused by that, but it doesn’t seem related?
Hey hey, I’m trying to create my own graph generator based off the docs here and here, but I’m running into an error on what should be a relatively simple implementation.
Here’s the code to my generator:
using System;
using System.Collections.Generic;
using Pathfinding;
using Pathfinding.Drawing;
using Pathfinding.Serialization;
using Pathfinding.Util;
using Unity.Jobs;
using UnityEngine;
namespace CrashPandas.AI.Navigation
{
[JsonOptIn]
[Pathfinding.Util.Preserve]
public class AstarRoadGraph : NavGraph
{
public override bool isScanned => nodes != null;
public PointNode[] nodes;
class RoadGraphScanPromise : IGraphUpdatePromise
{
public AstarRoadGraph graph;
PointNode[] nodes;
public IEnumerator<JobHandle> Prepare()
{
DriverNode[] driverNodes = RoadNetwork.Instance.DriverNodes;
nodes = new PointNode[driverNodes.Length];
// Create nodes
for (var i = 0; i < driverNodes.Length; i++)
{
var driverNode = driverNodes[i];
nodes[i] = new PointNode
{
gameObject = driverNode.gameObject,
GraphIndex = graph.graphIndex
};
nodes[i].position = (Int3)driverNode.transform.position;
}
// Populate connections
// IF I COMMENT OUT THIS FOR LOOP SO NO CONNECTIONS ARE MADE, NO ERRORS SHOWS IN CONSOLE
for (int i = 0; i < nodes.Length; i++)
{
nodes[i].connections = new Connection[driverNodes[i].nextNodes.Count + driverNodes[i].previousNodes.Count];
for (int j = 0; j < driverNodes[i].nextNodes.Count; j++)
{
var nextNode = driverNodes[i].nextNodes[j];
var nodeToConnectTo = nodes[Array.IndexOf(driverNodes, nextNode)];
nodes[i].connections[j] = new Connection(
nodeToConnectTo,
(uint)Vector3.Distance(driverNodes[i].transform.position,
nextNode.transform.position) * 1000,
true, false);
}
for (int j = 0; j < driverNodes[i].previousNodes.Count; j++)
{
var prevNode = driverNodes[i].previousNodes[j];
var nodeToConnectTo = nodes[Array.IndexOf(driverNodes, prevNode)];
nodes[i].connections[j + driverNodes[i].nextNodes.Count] = new Connection(
nodeToConnectTo,
(uint)Vector3.Distance(driverNodes[i].transform.position,
prevNode.transform.position) * 1000,
false, true);
}
nodes[i].Walkable = true;
nodes[i].SetConnectivityDirty();
}
yield break;
}
public void Apply(IGraphUpdateContext context)
{
graph.DestroyAllNodes();
graph.nodes = nodes;
}
}
protected override void DestroyAllNodes()
{
base.DestroyAllNodes();
nodes = null;
}
protected override IGraphUpdatePromise ScanInternal() => new RoadGraphScanPromise { graph = this };
public override void GetNodes(Action<GraphNode> action)
{
if (nodes == null) return;
for (int i = 0; i < nodes.Length; i++)
{
action(nodes[i]);
}
}
}
}
And the error I get when I hit scan is:
IndexOutOfRangeException: Index was outside the bounds of the array.
Pathfinding.GlobalNodeStorage.GetNode (System.UInt32 nodeIndex) (at ./Packages/com.arongranberg.astar/Core/Pathfinding/GlobalNodeStorage.cs:59)
Pathfinding.HierarchicalGraph+JobRecalculateComponents.Execute () (at ./Packages/com.arongranberg.astar/Core/Pathfinding/HierarchicalGraph.cs:509)
Unity.Jobs.IJobExtensions+JobStruct`1[T].Execute (T& data, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at <44f3679c53d1477a9c6e72f269e3a3a9>:0)
Using the inspector I can see that nodes are populated, but I don’t see any gizmos or anything either. Also note the comment in my code where // Populate connections is. Removing that section seems to scan without error, but still shows no gizmos (likely because there’s no connections for the gizmos to display)
Any help would be appreciated, I’m kind of at a loss here…