- Home /
How to store tile data for placing tiles
I'm making a game similar to the game Terraria and ran into an issue on how to store the data of tile types. Each tile has an integer called tileType and this dictates all the other attributes of the tile like hardness, texture, etc. The tile itself is not a gameobject but instead a class in an array called Tiles located in each chunk (the chunks are the gameobjects). My issue is storing all the data of the tile types and have resorted to doing a switch statement for every possible tile type but this feels extremely slow because I plan of making tiles destructible. Is there any way to store the tile type data in a more efficient way?
Answer by Alanisaac · May 22, 2018 at 01:29 AM
Often times when you want to model storing data in an object-oriented program, a good path to take is making a class! I'd recommend making a TileType class that has all the data about a tile type. You can reference this type from your tile object to populate various properties. If you structure things this way, you can also load all your tile types from a file. Here's a really simple example, where the data would come from a json file. For more on how to read JSON, see this article:
Code Files
[Serializable]
public class TileType
{
public int id;
public string name;
public double hardness;
public static TileType CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<TileType>(jsonString);
}
}
public class TileTypeLoader
{
public IEnumerable<TileType> LoadTileTypes()
{
var tileTypes = new List<TileType>();
var tileTypeStrings = File.ReadAllLines("C:/path/to/tile/type/file.json");
foreach(var tileTypeString in tileTypeStrings)
{
var tileType = TileType.CreateFromJSON(tileTypeString);
tileTypes.Add(tileType);
}
return tileTypes;
}
}
A JSON File
{"id": 1, "name": "Dirt", "hardness": 1}
{"id": 2, "name": "Stone", "hardness": 2.5}
{"id": 3, "name": "Grass", "hardness": 1.5}
Thanks for the support, I do use C# so if you have a C# example that would be nice but I understand the concept. Thanks again for the answer that fits perfectly
The given code is in C#. JSON is only the name of this specific way to format data but has nothing to do with the program$$anonymous$$g language.
I have worked with the script and have found it very useful but I still don't understand how to get the TileType List out of the IEnumerable. On line 26 where does it return the list to? @Alanisaac
It depends on how your code is structured. But let's say you have some sort of Tile$$anonymous$$anager class that controls the creation of tiles. In that class, you could load the tile types in the Awake call like so:
public class Tile$$anonymous$$anager : $$anonymous$$onoBehaviour
{
private List<TileType> tileTypes;
void Awake()
{
var tileLoader = new TileLoader();
var tiles = tileLoader.LoadTiles();
tileTypes = tiles.ToList();
}
}
Answer by allencoded · Jun 03, 2018 at 06:01 PM
I created a tutorial on medium, which basically is close the answer posted here.
https://medium.com/@allencoded/unity-tilemaps-and-storing-individual-tile-data-8b95d87e9f32
Thanks so much! I am just wondering how to access the bool IsExplored in your script?