Grid Generation

I am developing a pathfinding system for a game. It works, it’s clean, and fast. It’s works of a grid. Now I am going back to try and speed up my grid generation. As now I am instantiating a number of nodes and placing them. I need it this way so I can select individual nodes. Problem is if I generate a grid bigger than 32x32 unity hangs, a grid that is 100x100 pauses unity for at least 2 minutes. Anyway I can generate a grid of that size with little to no slow up, or at least unity not hanging until it is done.

for(var y : int = 0; y < gridSizeZ; y++){
        for(var x : int = 0; x < gridSizeX; x++){
            pos = Vector3(parentObj.transform.position.x + x,parentObj.transform.position.y,parentObj.transform.position.z + y) * gridSpacing;
            clone = Instantiate(nodeObject.gameObject,pos,parentObj.transform.rotation).GetComponent(PathNode);
            clone.SetRadius(nodeRadius);
            graphNodes.Add(clone);
            clone.graphID = this.GetInstanceID(); //Tell the node which graph it belongs to
            clone.iD = this.GetInstanceID()+clone.GetInstanceID(); //Give Node an ID
            clone.gameObject.name = clone.iD.ToString();
			clone.transform.parent = parentObj.transform;
			//Check Right
    		clone.CheckForNeighbor(Vector3.right,(gridSpacing *2));
    		//Check Left
    		clone.CheckForNeighbor(-Vector3.right,(gridSpacing *2));
    		//Check Up
    		clone.CheckForNeighbor(Vector3.forward,(gridSpacing *2));
    		//Check Down
	    	clone.CheckForNeighbor(-Vector3.forward,(gridSpacing *2));
	    	//Diagonals
	    	clone.CheckForNeighbor(Vector3(1,0,1),(gridSpacing *2));
	    	clone.CheckForNeighbor(Vector3(-1,0,-1),(gridSpacing *2));
	    	clone.CheckForNeighbor(Vector3(-1,0,1),(gridSpacing *2));
	    	clone.CheckForNeighbor(Vector3(1,0,-1),(gridSpacing *2));
        }
        scanProgress += progressPer;
	}

//SET RADIUS FUNCTION
    function SetRadius(rd : float){
    	transform.GetComponent(SphereCollider).radius = rd; size = rd*.5;	
    }
//Check Neighbor
    function CheckForNeighbor(direction : Vector3,length : float){
    	var hitObj : RaycastHit;
    	var nodeObj : PathNode;
    	if(Physics.Raycast(transform.position,direction,hitObj,length,1 << 17)){ // Only Certain Path Layer
        	nodeObj = hitObj.transform.GetComponent(PathNode);
        	if(nodeObj.graphID == this.graphID){//Node we found is in our graph 
        		AddNeighbor(nodeObj); nodeObj.AddNeighbor(this);
        	}
        }
    }

Under normal circumstances, using a grid for A* you wouldn’t need to create objects. I would use either a position or a Rect instead.
But maybe you need actual grid with objects in your project?

Just generally looking at the code, I feel that the “CheckForNeighbor”-function is unecessary. If you want to know which neighbours a node has, you can more easily check it using the knowledge how long and wide the list is.

ex.

var gridSizeX = 10;
var gridSizeZ = 10;
var totalGridSize = (gridSizeX * gridSizeZ);

for(var y : int = 0; y < gridSizeZ; y++)
{
    for(var x : int = 0; x < gridSizeX; x++)
    {
        // Create clone and set its properties and whatnots...
        
        // Get node neighbours     
        var thisNodeIndex = (y * gridSizeX) + x;
        
        var downNeighbourIndex  = (thisNodeIndex - gridSizeX);
        var upNeighbourIndex    = (thisNodeIndex + gridSizeX);
        var leftNeighbourIndex  = (thisNodeIndex - 1);
        var rightNeighbourIndex = (thisNodeIndex + 1);
        
        if(upNeighbourIndex >= 0)
        {
            // save neighbour index or object
        }
        if(downNeighbourIndex < totalGridSize)
        {
            // save neighbour index or object
        }
        if(leftNeighbourIndex >= 0)
        {
            // save neighbour index or object
        }
        if(rightNeighbourIndex < totalGridSize)
        {
            // save neighbour index or object
        }    
        
        // add created clone to list...
    }
}

Obviously some indices/objects doesn’t exist when doing the check since they arent created (yet), so thats why I would save the index instead of the actual object.

This is just a example code I wrote fast without testing or optimizing, so it might not be functional, but it gives an idea of what I mean. Hopefully :slight_smile:

Good luck!

i’m not trying to be dispiriting but is there a reason your not using the established pathfinding solutions?

NavMesh A* etc?

I mean fast pathfinding code isn’t exactly something you stumble apon. :stuck_out_tongue:

If your doing it because you’d rather have your own I guess so it’s just that I mean i’ve no idea how to create unity or nividia phsyx or a number of other highly technical specialized set of code functions. That’s why I leave it to the experts. :stuck_out_tongue:

You could spend the next month coding that and it’d be way slower than the implementations already available out there with less functionality.