Search Unity

Saving array of a custom class (non-monobehaviour) from editormode to runtime?

Discussion in 'Scripting' started by Udyrfrykte, Jul 17, 2011.

  1. Udyrfrykte

    Udyrfrykte

    Joined:
    Feb 14, 2010
    Posts:
    156
    I don't quite understand the problem myself, so it might get confusing:

    The variables I assign as I generate while in editormode doesn't transfer to runtime. The datatype in question is a Node-class I created which does not derive from monobeheaviour.

    I have an A* script. I recently created a simple grid generator that runs in editormode. It generates objects of my "Node" class and stores them in a list.
    First Node derived from MonoBehaviour. However, I did not want to actually have to instantiate the nodes (rather use the new keyword) so I removed any derivations from the Node class and altered it slightly. Now it doesn't work anymore.

    I saved the nodes to a generic list and transfered them to the static variable "nodes" in the Pathfinding script at runtime start.

    Sorry of the mess, I have no idea how to fix this.


    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4.  
    5. public class Node
    6. {
    7.     public Node parent = null;
    8.     public Vector3 position;
    9.     public float gValue = 0;
    10.     public float hValue;
    11.     public float fValue;
    12.     public List<Node> neighbours = new List<Node>();
    13.  
    14.     public Node(Vector3 pos)
    15.     {
    16.         position = pos;
    17.     }
    18. }
    19.  
    The class containing the method which is called from the editor
    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Slave : MonoBehaviour
    7. {
    8.     const float minNodeDistance = 8.5f;
    9.     public List<Node> nodesKeeper;
    10.    
    11.  
    12.     void Start()
    13.     {
    14.         // Do this because I seemingly can't store to a static lists before runtime
    15.         Pathfinding.nodes = nodesKeeper;
    16.     }
    17.  
    18.  
    19.     public void CreateGrid(int size)
    20.     {
    21.         List<Node> nodes = new List<Node>();
    22.  
    23.         for (int i = 0; i < size; i++)
    24.         {
    25.             nodes.Add(new Node(new Vector3((float) i*5, 0f, 0f)));
    26.            
    27.             for (int j=1; j < size; j++)
    28.             {
    29.                 nodes.Add(new Node(new Vector3((float)i * 5, 0f, j*5)));
    30.             }
    31.         }
    32.  
    33.  
    34.         // Determine neighbours
    35.         foreach (Node node in nodes)
    36.         {
    37.  
    38.             foreach (Node assessedNode in nodes)
    39.             {
    40.                 if (assessedNode == node)
    41.                     continue;
    42.  
    43.                 float dist = Vector3.Distance(assessedNode.position, node.position);
    44.  
    45.                 // Checking only for neighbours within maxNodeDistance
    46.                 if (dist < minNodeDistance)
    47.                 {
    48.                     node.neighbours.Add(assessedNode);
    49.                 }
    50.             }
    51.         }
    52.        
    53.         // We toss the nodes in a list so we can feed the static path
    54.         nodesKeeper = nodes;
    55.     }
    56. }
    57.  
     
    Last edited: Jul 17, 2011
  2. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    I faced this problem before too. I was adviced to extend my class from ScriptableObject if i wanted to carry variables to runtime. So, should you.

    Although, i do create my nodes, grid and everything else in the awake function of a controller monobehaviour. That makes things easier for me now.
     
  3. Udyrfrykte

    Udyrfrykte

    Joined:
    Feb 14, 2010
    Posts:
    156
    Thanks for the answer!

    ScriptableObject gave me some troubles with me not finding how to set parameters in its constructor, but that's fine! Works like a charm, many a thanks :)
    I thought about doing everything in awake on a monobehaviour too, but I'm stubborn :\
    I started with a different approach where I write everything to XML and load it at runtime.
     
    Last edited: Jul 17, 2011