Search Unity

[SOLVED] Best way to store tile data?

Discussion in 'Scripting' started by Comafly, Nov 27, 2015.

  1. Comafly

    Comafly

    Joined:
    May 30, 2014
    Posts:
    87
    Hey guys, I have a bunch of tile data I need to store and reference, but I am not sure on the best way to do it.

    Each tile has a string for the name, a Vector3 for it's rotation, and an X and Y position. So each tile needs to be associated with 4 different variables that I need access to.

    What is the most efficient way to store and retrieve this amount of data for a 15x9 grid of tiles?
     
  2. apsdsm

    apsdsm

    Joined:
    Sep 26, 2013
    Posts:
    56
    As a first step you should look into the scriptable object class. This will keep the data serialised for the Unity runtime, but also give you good access to it via the inspector.

    If you just want to temporarily keep the data around and don't care about making it an asset, I would define a class that holds all the data for a single tile, then I'd allocate them all to an array.
     
    Comafly and Kiwasi like this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    'Best' collection types really depend on how you want to access the data. If you want to access it by name then a dictionary is likely better. If you want to access it by position then I would suggest a 2D array. If the amount of tiles will change up and down then you might want a list.

     
    Comafly likes this.
  4. DeathQuake

    DeathQuake

    Joined:
    Oct 24, 2012
    Posts:
    64
    My Approch for this would be to create a separate serializable class that i can use in my main class

    Code (CSharp):
    1. [System.Serializable]
    2. public class TileData
    3. {
    4.     public string        _TileName;
    5.     public Vector3    _TileRot;
    6.     public float           _X;
    7.     public float          _Y;
    8. }
    9.  
    10. public class MyGameClass
    11. {
    12.     public TileData[][] = new TileData[15][9];
    13. }
     
    Comafly and Kiwasi like this.
  5. Comafly

    Comafly

    Joined:
    May 30, 2014
    Posts:
    87
    Ohhh, that looks far more manageable than anything I was thinking of haha. Thank you!

    How do I go about assigning the variables in the array?
    I attempted this:
    Code (CSharp):
    1. TileData [0,0].TileName = "Wall";
    But I keep getting the following error:
    NullReferenceException: Object reference not set to an instance of an object
     
  6. DeathQuake

    DeathQuake

    Joined:
    Oct 24, 2012
    Posts:
    64
    Hi,
    Yea.. I made a mistake there.. i did not have a variable name for the array.

    Code (CSharp):
    1. public TileData[][] mTiles = new TileData[15][9];
    2.  
    3. void Start()
    4. {
    5.      mTiles[0][0]._TileName = "Tile1";
    6. }
    just make sure you initialize the array.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You probably want a 2D array instead of a jagged array. Small difference, but it will likely reduce problems later.
     
  8. Comafly

    Comafly

    Joined:
    May 30, 2014
    Posts:
    87
    Hey guys, sorry for the late reply, I haven't been able to work on things the last week.
    I am still having some issues with this.

    So I have my separate TileData class:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [System.Serializable]
    6. public class TileData{
    7.     public string TileType;
    8.     public Vector3 TileRot;
    9.     public float TileX;
    10.     public float TileY;
    11. }
    12.  
    And then in my main game class I create a new array of TileData, and it works fine - I can log the length correctly and the variables show up in the intellisense lst.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. public class MainGame: MonoBehaviour {
    7.  
    8.     public TileData[,] tileData;
    9.  
    10.     void SetTiles(){
    11.         tileData = new TileData[15,9];
    12.         tileData [0,0].TileType = "Wall";
    13.     }
    14. }
    15.  
    But I keep getting thrown that same error whenever I try to access the TileData variables. (Line 12 in the quoted code above.)

    Any ideas?
     
  9. Comafly

    Comafly

    Joined:
    May 30, 2014
    Posts:
    87
    OKAY! I just did a bunch of fiddling around, and I thought to myself that maybe it has to do with the fact that despite the fact I am creating an array of TileData, that I might need to individually set each entry in the array as a new TileData, and that solved it! So for anyone who might come across this, here is what I did:

    Code (csharp):
    1.  
    2. public class Room : MonoBehaviour {
    3.     //Create a new array of the TileData class
    4.     public TileData[,] tileData;
    5.  
    6.     void SetTiles(){
    7.         //Set the size of the array
    8.         tileData = new TileData[15,9];
    9.         //Set the very first entry in the array as a new TileData
    10.         tileData [0,0] = new TileData();
    11.         //Set the very first entry's TileType as "Wall"
    12.         tileData [0,0].TileType = "Wall";
    13.     }
    14. }
    15.  
    Obviously setting the [0,0] entry is just for testing purposes, you can set up a for loop to cycle through all of the entries and create new TileData for all of your entries.

    Hope that helps!
     
    gooby429 and Kiwasi like this.