Support Forum

PointGraph.node contains empty nodes

Good day! I add new points to the Point Graph in Realtime mode. Everything is well added, but when I try to get the entire list of nodes, I find not only my own nodes but, for example, 144 empty Null nodes. I thought these were my nodes, but my nodes are 366 and they are clean as day …

Maybe I don’t understand what, maybe it’s Cash search engine or something like that?
I’ll show you an example of adding nodes Unity 2019.3.7f1 and Latest A*

        private void AddNodes(List<Node> nodes)
        {
            _pathfinder.AddWorkItem(new AstarWorkItem(ctx =>
            {
                var graph = _pathfinder.data.pointGraph;
                if (graph != null)
                {                    
                    foreach (var node in nodes)
                    {
                        graph.AddNode(node, (Int3)node.Position);
                    }
                    graph.ConnectNodes();
                    graph.RebuildNodeLookup(); // researching what it is...
                }
            }));
        }

And the code with which I get null nodes :

    void Start()
    {
        //Delayed call because access may not have been initialized yet.
        DOVirtual.DelayedCall(1f, () => 
        {
            var pointnodes = AstarPath.active.data.pointGraph.nodes;
            var i = 0;
            foreach (var item in pointnodes)
            {
                Debug.LogError($" Node : {item}, Index : { i++}");
            }
        });
    }

Thanks for any help, this is not a huge problem, but it would be nice to get rid of empty objects - nodes.

Hello!
I changed the function for myself and the problem went away. Here is the code from PointGenerator. This is the source code :

		public T AddNode<T>(T node, Int3 position) where T : PointNode {
			if (nodes == null || nodeCount == nodes.Length) {
				var newNodes = new PointNode[nodes != null ? System.Math.Max(nodes.Length+4, nodes.Length*2) : 4];
				if (nodes != null) nodes.CopyTo(newNodes, 0);
				nodes = newNodes;
			}

			node.SetPosition(position);
			node.GraphIndex = graphIndex;
			node.Walkable = true;

			nodes[nodeCount] = node;
			nodeCount++;


			return node;
		}

Below is the code that I added for convenience using overloading, I made a few changes, mainly the way the array expands :

		public T[] AddNode<T>(T[] newNodes) where T : PointNode
		{
			if (nodes == null || nodeCount == nodes.Length)
			{
				var newArrayNodes = new PointNode[nodes != null ? nodes.Length + newNodes.Length : newNodes.Length];
				if (nodes != null) nodes.CopyTo(newArrayNodes, 0);
				nodes = newArrayNodes;
			}
            foreach (var newNode in newNodes)
			{
			    newNode.SetPosition(position);
			    newNode.Walkable = true;
				newNode.GraphIndex = graphIndex;
				nodes[nodeCount] = newNode;
				nodeCount++;
			}

			return newNodes;
		}