Creating new graphs with C#

Hello. I have to mention, I did check the procedural example (with the spider and perlin noise), but still don’t know how.

Ok, this is what I want to do. I have some platforms with flat ground and I just want to place GridGraph on each of those platforms with code (why do it with hand, if code is 100x faster, right?), scan and that is.

I know how to set propreties, but I don’t know how to properly Create Graph.

This code runs in ‘Editor’ extended class…, platform is MonoBehavior.
`

public void GenerateGraph(AstarPath astarPath, Platform platform) {

    // previous graph
    if (platform.graph != null)
        astarPath.astarData.RemoveGraph(platform.graph);

    //creating graph
    var gridGraph = new GridGraph();
        
    astarPath.astarData.AddGraph(gridGraph);

    var platformGround = platform.platformGround;
   
    // this is mesh collider on which NPCs will walk
    var collider = platformGround.GetComponent<MeshCollider>();
    
    // and I want the graph to be this wide
    var bounds = collider.bounds;

    var max = bounds.max;
    var min = bounds.min;

    var center = bounds.center;

    gridGraph.center = new Vector3(
        center.x,
        max.y+1,
        center.z);

    var nodeSize = 0.5f;

    gridGraph.nodeSize = nodeSize;

    //calculating resolution
    var countX = bounds.size.x / nodeSize;
    var countY = bounds.size.y / nodeSize;

    gridGraph.width = (int)countX;
    gridGraph.depth = (int)countY;

    gridGraph.ScanInternal();

    platform.graph = gridGraph;
}`

It doesn’t actually. It messes up with AstarPath class, and I get bunch of NullReferenceErrors from it.

I don’t know how to insert and remove graphs properly from AstarPath class.

Also I know this was kind of quickly written post, if you do not understand it clearly just state so, I am in kind of rush and maybe missed details or two… Thanks!!

I think I found it… missed the documentation stuff… Gonna try now :slight_smile:

Amm… Well, figured out? Can you show an example?

@Acterix

Here’s an example of how to add and scan a new grid graph

var graph = AstarPath.active.astarData.AddGraph(typeof(GridGraph)) as GridGraph;
int width = 10;
int depth = 10;
float nodeSize = 1f;
graph.center = Vector3.zero;
graph.SetDimensions(width, depth, nodeSize);
AstarPath.active.Scan();
1 Like

limiting the array of grids to 255. What are the problems with this? Can I work around it?

Hi

The number of graphs is limited to 255 mostly for memory reasons. The graph indices are stored for each node in a bitpacked field such that it only uses 8 bits of memory instead of e.g 32 bits for an int.
In the majority of cases people only use a single graph, and it is extremely rare to use more than 10.
Why do you need that many graphs?

It is possible to work around this though and create as many graphs as you want, it just requires you to change a few constants in the code (I can show you which ones if you need to do it).

My world (on server) contains 475 rooms in 7 levels, in every room there are obstacles. if i create grid for lvl, then many nodes not used. I think it’s worth removing the restriction, if there are no other suggestions.

Ok.

Do yo have every room loaded at the same time?

In any case. If you want to expand the number of graphs you need to change a few things. First open the GraphNode.cs file and change this line

public const uint MaxGraphIndex = FlagsGraphMask >> FlagsGraphOffset;

to

public const uint MaxGraphIndex = (uint)(int.MaxValue - 1);

then change this code

public uint GraphIndex {
	get {
		return (flags & FlagsGraphMask) >> FlagsGraphOffset;
	}
	set {
		flags = flags & ~FlagsGraphMask | value << FlagsGraphOffset;
	}
}

to

public uint GraphIndex {get; set;}

I think that should be enough. It should now be possible to add up to approximately 2 billion graphs (in theory).

1 Like

Randomly generated world in server when he launched and every day at the certain time.

Thank you for answer. Now my soul is calm. )