External graph settings file

Hi there!

I have multiple graphs in scenes, and multiple scenes, and I’m finding maintaining them a bit cumbersome. I had the idea that it’d be awesome if I could create a GraphSettings ScriptableObject to assign to the Astar Path MonoBehavior that contains the common aspects of my graphs (node side, physics, tags etc.) so creating a new Astar Path in a scene is as simple as adding the component, dragging in the settings object and then setting the position, width and depth.

Obviously I use the same settings across all of my graphs, where some people might have one grid, one NavMesh etc., so that presents some challenges. But it was just an idea I had.

Thanks!

1 Like

This is a great suggestion.
For our project I ended up hiding the not used graph types.
And modified the default values for the graphs we did use :slight_smile:

1 Like

Hi

You could emulate this from a script. Something like

public TextAsset commonGraphSettings;
public int width = 100;
public int depth = 100;
public float nodeSize = 1;
...

public void Start () {
     AstarPath.active.DeserializeGraphs(commonGraphSettings.bytes);
     var gg = AstarPath.active.data.gridGraph;
     gg.SetDimensions(width, depth, nodeSize);
     AstarPath.active.Scan();
}

See also https://arongranberg.com/astar/docs/saveloadgraphs.html

Thanks Aron. Unfortunately this prevents working in the editor for sizing, which is a big part of my game.

Ok.
What you can do then is to save your common settings to a file (see https://arongranberg.com/astar/docs/saveloadgraphs.html) and then when you want to create a new level, load the graph (which can be done from the A* inspector) in the new scene and just adjust the settings slightly.

Thanks Aron. Apologies for the massively delayed response, hehe. So the biggest issue is that I have varying amounts of graphs per scene, and I’m more concerned about if I need to update all graphs in the future. What I’ve ended up doing is creating this:

using System.Linq;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using Sirenix.OdinInspector;
using Pathfinding;
using TearThrough.Managers;

namespace TearThrough.Maps {
    public class AstarPathSettings : MonoBehaviour {
#if UNITY_EDITOR
        [Button]
        public void UpdateSettings() {
            AstarPath astarPath = GetComponent<AstarPath>();

            if (astarPath == null) {
                Debug.LogError("No Astar Path component found on this GameObject.");

                return;
            }

            transform.position = Vector3.zero;
            transform.rotation = Quaternion.identity;

            Undo.RecordObject(astarPath, "Undo Astar Path modifications");

            foreach (GridGraph graph in astarPath.graphs.Cast<GridGraph>()) {
                graph.nodeSize = 2;

                graph.neighbours = NumNeighbours.Eight;
                graph.cutCorners = true;
                graph.erodeIterations = 0;

                graph.collision.use2D = true;
                graph.collision.collisionCheck = true;
                graph.collision.type = ColliderType.Sphere;
                graph.collision.diameter = 5.3f;
                graph.collision.mask = LayerManager.Masks.WATER | LayerManager.Masks.WALLS | LayerManager.Masks.COLLISION;
            }
        }
#endif
    }
}

The only setting I can’t seem to modify properly is the first “2D” setting. I went through your editor code and tried setting the rotation manually, however I got wonky results doing this for some reason. Can you see any issue with setting the other properties manually and subverting the editor? I tried to find if there was any behind the scenes magic occuring with those properties when changed in the inspector, but couldn’t see any myself.

Hi

In the current beta you can edit that using the GridGraph.SetGridShape method.

In case you are not using the beta, here is the source code for that function:

/** Changes the grid shape.
	* This is equivalent to changing the 'shape' dropdown in the grid graph inspector.
	*
	* Calling this method will set #isometricAngle, #aspectRatio, #uniformEdgeCosts and #neighbours
	* to appropriate values for that shape.
	*
	* \note Setting the shape to #InspectorGridMode.Advanced does not do anything except set the #inspectorGridMode field.
	*
	* \see #inspectorHexagonSizeMode
	*/
public void SetGridShape (InspectorGridMode shape) {
	switch (shape) {
	case InspectorGridMode.Grid:
		isometricAngle = 0;
		aspectRatio = 1;
		uniformEdgeCosts = false;
		if (neighbours == NumNeighbours.Six) neighbours = NumNeighbours.Eight;
		break;
	case InspectorGridMode.Hexagonal:
		isometricAngle = StandardIsometricAngle;
		aspectRatio = 1;
		uniformEdgeCosts = true;
		neighbours = NumNeighbours.Six;
		break;
	case InspectorGridMode.IsometricGrid:
		uniformEdgeCosts = false;
		if (neighbours == NumNeighbours.Six) neighbours = NumNeighbours.Eight;
		isometricAngle = StandardIsometricAngle;
		break;
	case InspectorGridMode.Advanced:
	default:
		break;
	}
	inspectorGridMode = shape;
}

Thanks Aron, however I mean the actual 2D checkbox that sets the grid’s rotation.

Oh, I misread your question :stuck_out_tongue:

This should be all

var rotation2D = 0;
graph.rotation = new Vector3(rotation2D - 90, 270, 90)