CustomEditor, not saveing changes

This is really anoying me, working on some tools to help streamline stuff and its resisting all efforts.

Every time I create a grid of nodes it wipes them out if a script compiles, I hit play or i reload the editor.

EditorUtility.SetDirty(target); Should be saveing the changes but for some reason it is not.

Have also tryed all types of [SerializeField] and [System.Serializable] but nothing changes.

This is the customEditor im trying to get to work.

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;

[CustomEditor(typeof(PathFinderTwo))]
public class EditorPathFinder : Editor
{


    public override void OnInspectorGUI()
    {
        PathFinderTwo script = (PathFinderTwo)target;
        EditorGUILayout.BeginHorizontal();
        if (GUILayout.Button("Make NodeGrid"))
        {
            Place_Grid(script);
            EditorUtility.SetDirty(target);
            EditorUtility.SetDirty(this);
        }
        EditorGUILayout.EndHorizontal();
        // Show default inspector property editor
        DrawDefaultInspector();
    }

    public void Place_Grid(PathFinderTwo script)
    {
        Debug.Log("******************************");
        script.nodeList = new PathFinderTwo.Node[script.gridSizeX / script.gridSpaceing, script.gridSizeZ / script.gridSpaceing]; // Make the array

        for (int x = 0; x < script.gridSizeX / script.gridSpaceing; x++)
        {
            for (int z = 0; z < script.gridSizeZ / script.gridSpaceing; z++)
            {
                EditorUtility.DisplayProgressBar("Working", "# " + x + " Of " + script.gridSizeX, x / script.gridSizeX);
                //Create a new node
                PathFinderTwo.Node node = new PathFinderTwo.Node();
                //Get the posistion of the new node
                Vector3 nodeposistion = new Vector3(x * script.gridSpaceing, 0, z * script.gridSpaceing);

                RaycastHit hit = new RaycastHit();
                if (Physics.Raycast(new Vector3(nodeposistion.x,200, nodeposistion.z), -Vector3.up, out hit))
                {
                    nodeposistion.y = hit.point.y + 0.5F;
                }

                node.nodePosistion = nodeposistion;
                script.nodeList[x, z] = node;
                Debug.Log("Node: "+x + "," + z);
            }
        }
        EditorUtility.ClearProgressBar();
        script.numberOfNodes = script.nodeList.Length;
        EditorUtility.SetDirty(target);
    }
}

This is part of the script it changes.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PathFinderTwo : MonoBehaviour {

    //public float costForEachNode = 1;
    public bool showIcons;

    public int gridSizeX;
    public int gridSizeZ;
    public int gridSpaceing; //Distance of nodes from each other.

    public int numberOfNodes;
    
    [System.Serializable]
    public struct Node
    {
        public Vector3 nodePosistion;
       // public Node connection; //For parenting and returning path
        public int connectionKey;
        public bool open; //Is this node passable
    }
    [SerializeField]
    public Node[,] nodeList;

    void Start()
    {
    }

    void OnDrawGizmos()
    {
        if (showIcons == true)
         {
             foreach (Node node in nodeList)
             {
                 Gizmos.DrawIcon(node.nodePosistion, "NavNode.psd");
             }
        }
    }


}

Narrowed it down to “public Node[,] nodeList;”.

It looks like setdirty() doesnt work on 2d arrays, since I set it to a normal array and it works fine.

Now I am trying to get a jagged array working but im having issuses geting the nested array initiated.

Array and Node Setup.

    [System.Serializable]
    public class Node
    {
        public Vector3 nodePosistion;
        public bool open; //Is this node passable
    }

    [System.Serializable]
    public class NodeArray
    {
        public Node[] node;

       public Node this[int index]
        {
            get
            {
                return node[index];
            }
            set
            {
                node[index] = value;
           }
        }
    }
    public NodeArray[] nodeArray;

And this to initiate the arrays.

        void Start()
        {
        nodeArray = new NodeArray[8];
        for (int x = 0; x < nodeArray.Length; x++)
        {
            nodeArray[x].node = new Node[8];
        }
        }

Keeps saying out of range.

I don’t have a definite answer for you regarding the serialization issue, but here’s something you might try. Rather than storing a 2-d array (or some variation thereof), store a 1-d array and provide accessors for 2-d indexing.

This is a common solution to the multi-dimensional array problem in general, and in this case might have the added advantage of solving your serialization problem.

Solved it by useing generic List's.
Wanted the 2D array so that it would have the built in function of knowing what nodes are next to each other, [x+1][z] is the one to the left of x,z and so forth.

You can do that with a wrapped 1-d array as well. In fact, pretty much anything you can do with a 2-d array you can do with a 1-d array. (This is how, for example, multi_array from the Boost libraries works.)

Anyway, since you’ve already found a solution, I suppose it’s academic :slight_smile:

This is interesting, what is/are the boost libraries and what do they do?

Always looking for better ways of doing stuff.

The 2d type array was just makeing stuff bug out so I scraped that and made it a single list and made a lookup function that looks at the class.indexNumbers I added to them and returns the one with the x,z I want. Works better and seems cleaner.

The whole reason I did all this is I used to have all the nodes as empty gameobjects with the node script on them but unity was crashing once I got over 800+ of them. This new way its all in code and I can store 10,000+(Tested) without issuses. Also instead of pointers to setup connections I use the index number of the List<> as a pointer.

It’s a set of C++ libraries, so it wouldn’t be of much use with Unity unless you’re writing C++ plugins. (I was just mentioning it as an example.)

Yeah, that sounds more or less like what I was suggesting :slight_smile: