Unable to set gridgraph height and width from code

hi, im working on a project with randomly generated mazes. to do this i instantiate a prefab “cell” (with a floor and 4 walls ) in a grid (example 10x10) and then use DFS to breakdown the walls between to generate the maze. all of this works fine and i am able to generate random mazes at runtime.

what i want to do next is to add pathfinding to an object and move it around the maze. since i intend the maze to be generated at runtime, and the size of the maze to be dynamic, i want to setup the gridgraph using code. which is where i run into problems.

i found some code to do this on this forum and while i am able to get the grid graph generated, it does not accept the size of the maze i want to use and instead sets the grid.width and grid.depth to 10.

using the inspector at runtime and manually changing the values and hitting scan gives me what looks like a good gridgraph.

the code im using is as follows.

`

void start() {

UnityEngine.Object temp;
for (int y = 1; y <= height; y++) {
	for (int x = 1; x <= width; x++) {
		Vector3 pos = new Vector3(x, 0, y) * spacing;
		temp = Instantiate(prefab, pos, Quaternion.identity);
		temp.name = "Cell_" + (((y*width)-width)+x);    // More convinient names for each cell instance.
	}
}

// DFS CODE HERE

makepath(100); // height * width

}

void makepath(int msize){

GridGraph g = (GridGraph)AstarPath.active.astarData.CreateGraph(typeof(GridGraph));
g.neighbours = NumNeighbours.Four;
g.width = msize;
g.depth = msize;
g.center = transform.position + (Vector3.right * 10f) + (Vector3.forward * 10f);
g.bounds = new Bounds(g.center, new Vector3(16, 0.3f, 16));

Matrix4x4 m = g.matrix;
g.GenerateMatrix();
g.RelocateNodes (m, g.matrix);

AstarPath.active.astarData.AddGraph( g );
GridGraph gridNow = (GridGraph)AstarPath.active.astarData.graphs[0];
//AstarPath.active.UpdateGraphs(g.bounds);
AstarPath.active.Scan();
}

`

being new to both unity and the a* project i dont know where im going wrong. i apologize in advance if this is a simple mistake i am making.

p.s also i would like to know if there is a simpler way to achieve what i want to do.

thank you in advance.

The best way to do this is pass the X and Y of your maze and update(scan) the scene at run time~ But sadly im not sure how to scan with a script… But I bet Aaron could tell you!

The quick and dirty way would be to use GraphUpdateScene though… If you want to do something in the mean time…

thanks for the quick reply burdock. i have done what i think is passing the maze size by using g.depth and g.width, but the problem is that the graph size does not change at runtime.

i have also tried to use the GraphUpdateScene component however the problem there is that the graph updates on start when the maze hasnt been generated and therefore all i get are the red error nodes.

i would prefer to use code since the size of the maze wont be the same and using GraphUpdateScene restricts me to using the same graph size.

any help would be appreciated.

When I next get a chance I will ask Aaron how to scan the graph via, a script… Or he will just drop by and answer it in the next few days…

http://arongranberg.com/astar/docs/graph-updates.php

You should be able to call GraphUpdateScene within a script, read this for more info!!

hi, thanks for taking the time to reply. i have read that article and tried to use graphupdatescene from script without any positive results. i think this is because GetComponent().Apply (); only initiates the scan on startup but as the width and depth values are defaulting to 10 it doesnt graph the entire space i need.

i also notice that in the inspector that the depth and width values have a little lock icon next to them. does this matter? do i need to toggle some value to unlock them so they will recognize the new values i want?

hopefully arron will post a reply to this soon cauz im running out of other stuff to do till i can get this fixed.

Hi

Yes, setting width and depth from script is a bit counter intuitive right now I am afraid.
The thing is that width and depth is not got from the width and depth variables, it is got from the unclampedSize and center members.

However, there is method called UpdateSizeFromWidthDepth which will recalculate those values based on the current width and depth.

graph.center = transform.position; (or whatever) graph.width = 50; graph.depth = 50; graph.UpdateSizeFromWidthDepth (); graph.Scan();

`
/** Updates #size from #width, #depth and #nodeSize values. Also \link GenerateMatrix generates a new matrix \endlink.


  • ote This does not rescan the graph, that must be done with Scan */
    UpdateSizeFromWidthDepth ()`

wow, thanks a lot for replying. that clears up a lot things.
it looks like i need to update the bound values for the graph matrix from code also apart from the width and depth, but once i did it works great.

thanks again :slight_smile: