Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help with multidimension arrays for tile based game

Discussion in 'Scripting' started by ShottyMonsta, Aug 14, 2014.

  1. ShottyMonsta

    ShottyMonsta

    Joined:
    May 23, 2014
    Posts:
    25
    First off; yes I've done a search and am aware of multiple threads on this issue but I am still struggling.

    Basically I want to declare a 2D array for each row on a board.

    Then I want another array which stores each row (in other words a column array).

    I then want to iterate through the column array to pull each row out, then iterate through that row to find the values contained within each tile within that row.

    Here is my code so far:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class pathFinding : MonoBehaviour {
    5.  
    6.     //Declare array to store each row
    7.     ArrayList mapArray = new ArrayList[4];
    8.  
    9.     //Declare bool arrays for each row, true = passable, false = unpassable
    10.     bool [,] rowOne = new bool[8, 1] { {true}, {true}, {true}, {true}, {true}, {true}, {true}, {true} };
    11.     bool [,] rowTwo = new bool[8, 1] { {true}, {true}, {true}, {false}, {false}, {true}, {true}, {true} };
    12.     bool [,] rowThree = new bool[8, 1] { {true}, {true}, {true}, {false}, {false}, {true}, {true}, {true} };
    13.     bool [,] rowFour = new bool[8, 1] { {true}, {true}, {true}, {true}, {true}, {true}, {true}, {true} };
    14.  
    15.     //Put each row into the mapArray
    16.     mapArray[0] = rowOne;
    17.     mapArray[1] = rowOne;
    18.     mapArray[2] = rowOne;
    19.     mapArray[3] = rowOne;
    20.  
    21.  
    22.     //Use this for initialization
    23.     void Start () {
    24.         //Iterate each row in map array
    25.             //Iterate through each tile in row
    26.                 //Return value stored in tile
    27.     }
    28. }
    29.  
    I get the errors:

    "(16,9): error CS0178: Invalid rank specifier: expected `,' or `]'"
    "(16,21): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration"
    "(16,29): error CS1519: Unexpected symbol `;' in class, struct, or interface member declaration"

    I am obviously totally misunderstanding how arrays work, I've only ever used 1D arrays before and my head is starting to melt, can anyone help me?
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
  3. ShottyMonsta

    ShottyMonsta

    Joined:
    May 23, 2014
    Posts:
    25

    I know how to use 1D arrays as I said, it's more like I need a 3D array I guess.

    Like imagine a chessboard, it is 2D but it has a 3rd dimension in that each square can have some value assigned (like passable or not). This is what I'm trying to do :)
     
  4. ShottyMonsta

    ShottyMonsta

    Joined:
    May 23, 2014
    Posts:
    25
    Solved:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class pathFinding : MonoBehaviour {
    5.  
    6.     static bool[] rowOne = {true, true, true, true, true, true, true, true };
    7.     static bool[] rowTwo = {true, true, true, false, false, true, true, true };
    8.     static bool[] rowThree = {true, true, true, false, false, true, true, true };
    9.     static bool[] rowFour = {true, true, true, true, true, true, true, true };
    10.  
    11.     ArrayList mapArray = new ArrayList ();
    12.  
    13.     //Use this for initialization
    14.     void Start () {
    15.         mapArray.Add (rowOne);
    16.         mapArray.Add (rowTwo);
    17.         mapArray.Add (rowThree);
    18.         mapArray.Add (rowFour);
    19.  
    20.         object[] tempArray = mapArray.ToArray ();
    21.         foreach (bool[] x in tempArray) {
    22.             foreach (bool y in x) {
    23.                 Debug.Log (y);
    24.             }
    25.         }
    26.  
    27.     }
    28. }
    29.  
     
  5. Graph

    Graph

    Joined:
    Jun 8, 2014
    Posts:
    153
    youre trying to explicitly define the length of the 2nd layer you cant do that
    for nested arrays you can only define the depth
     
    ShottyMonsta likes this.
  6. ShottyMonsta

    ShottyMonsta

    Joined:
    May 23, 2014
    Posts:
    25
    Nice, cheers for the knowledge :)
     
  7. Graph

    Graph

    Joined:
    Jun 8, 2014
    Posts:
    153
    i should probably add that if you declare them with content the depth and length are implicit so to speak i.e.: array of jagged multiD arrays:

    Code (CSharp):
    1. int[][,] arr = new int[3][,]
    2.   {
    3.       new int[,] { {1,3}, {5,7} },
    4.       new int[,] { {0,2}, {4,6}, {8,10} },
    5.       new int[,] { {11,22}, {99,88}, {0,9} }
    6.   };
    7.  
    mix match and nest as you see fit :D
     
  8. ShottyMonsta

    ShottyMonsta

    Joined:
    May 23, 2014
    Posts:
    25
    Where "jagged" means a dynamic array? Or does jagged refer to the array of arrays where each child array can be of different length?
     
  9. ShottyMonsta

    ShottyMonsta

    Joined:
    May 23, 2014
    Posts:
    25
    Do you have any idea how I would pull the first array out of this 2D array?


    Code (CSharp):
    1. static object[,] rowOne = new object[2,4] { {1, 1, true, true}, {2, 1, true, false} };
    2.  
    3.  
     
  10. Graph

    Graph

    Joined:
    Jun 8, 2014
    Posts:
    153
  11. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    Why not just create your own class structure and just put it into a list or a dictionary with the key of a Coordinate of a tile. ** Dictionary will not be visible in Inspector List can.

    You could have as much data about a tile as you want then and it would make more sense via reading.

    Code (csharp):
    1.  
    2.  
    3. public Dictionary<Vector2, TileData> myTileData = new Dictionary<Vector2, TileData>();
    4. [System.Serializable]
    5. public class TileData
    6. {
    7.   public bool isWalkable;
    8.   public bool hasEnemy;
    9.   public bol isTrap; //etc..
    10. }
    11.  
     
    ShottyMonsta likes this.