Search Unity

Struct variable of type struct array initializing

Discussion in 'Scripting' started by blackHoundStudios, Oct 27, 2017.

  1. blackHoundStudios

    blackHoundStudios

    Joined:
    Nov 24, 2016
    Posts:
    10
    Im having an issue with my struct. Im working on a implementation of the Monte Carlo Tree Search and i
    created a struct called Node which has specific variables.
    Now i need to store parent as well as child objects of type node. Since a node can be a parent for other nodes, but also child of a node this is important.

    Code (csharp):
    1.  
    2. System.Serializable]
    3.     public struct Node
    4.     {
    5.  
    6.         public Transform[,] CurrentField_V;
    7.         public Transform[,] CurrentField_H;
    8.         public Transform[,] CurrentField_Boxes;
    9.         public Node [] nodeChildren;
    10.         public Node [] parent;
    11.         public int result;
    12.         public bool isTerminal;
    13.         public bool alreadyChecked;
    14.         public int y;
    15.         public int x;
    16.         public int visitTimes;
    17.  
    18.  
    19.  
    20. //THIS IS WERE THE ISSUE IS
    21.  
    22.  public Node Expand(Node v)
    23.     {
    24.        
    25.         Debug.Log("Start EXPANSION");
    26.         //create a new node
    27.         Node newNode = new Node();
    28.         newNode.parent[0] = v; //---------> NULL REFERENCE EXCEPTION
    29.         newNode.CurrentField_V = v.CurrentField_V;
    30.         newNode.CurrentField_H = v.CurrentField_H;
    31.         v.nodeChildren[counter] = newNode;//---------> NULL REFERENCE EXCEPTION
    32.         newNode.visitTimes++;
    33.  
    34.  
    35.     }
    36.  
    37.  

    does anyone have a clue how i can solve this issue?
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    A few thoughts:
    - Why would you use a struct for a Node instead of a class? (With a class you can you inheritance to force and implementation of Expand. That way you can decouple the MCTS logic from the actual search space.)
    - Why use an array for parent? There should only be one parent.
    - Expand should have no parameters and (optionally) no return type. It should expand itself into nodeChildren. (And optionally return that array.)
    - Make things easier for yourself by using a List<Node> for the children instead of an array.