# How should I store and load structures for 2d tile based game?

I am working on structure generation for my 2d tile based game. Structures can be anything from houses, castles, dungeons, mazes etc.

Structures are pre-defined, meaning I defined the width/height of the structure and wrote every single tile that should be placed for each coordinate.

Structure "template":

public class Structure
{
private int width;
private int height;

private int[,] element;

public Structure(int _w, int _h, int[,] _e)
{
width = _w;
height = _h;
element = _e;
}
}


Creating a new structure:

// 0 = air

// 1 = wall

// 2 = door

Structure house = new Structure(4, 4,
{

{1, 1, 1, 1},

{1, 0, 0, 1},

{1, 0, 0, 1},

{1, 2, 1, 1}

});


This works fine for small amounts of structures. However, having tens/hundreds of structures in code is not only mess, but taking memory unnecessarily since structure generation gets called rarily. So how should I store that data?

1) Should I make a file for each structure and load from it?

2) Should I dump all structures in 1 file?

3) What file format should I use for this?

4) Does Unity provide some data containers that can be easily accessed and read from?

5) How is this usually done?

Maps don't belong into code. They belong into files which get loaded at runtime.

What you need is a WYSIWYG map editor which allows you to edit maps on the fly. If you build it right into your game, you can use the C# serialization system to turn the data in your class Structure to a file. Alternatively, you can use the method AssetDatabase.CreateAsset to turn an object in the running game into a persistent prefab asset which you can then use in your project. But keep in mind that the AssetDatabase class is in the UnityEditor namespace, so you can only use it while testing your game in the editor. It won't work if you build your game.

If you would like to edit your structure files with an external map editor like tiled or just use a text editor, then you need to invent your own file format. There isn't really a standard for this.

This would likely depend on how detailed you want your structures. Is it just going to be the tile positioning? Or are you going to want things like name, type and the like for what I presume to be your world generator to know about?

If just the tile positioning, then I would just dump to and from a csv file, and write your own import/export code (export because it is a hell of a lot easier to create in unity itself then dump to file, than define each structure manually, especially if the structure is 100 X 100 castle or cathedral). Each line is a row, and then store the data split by commas (or other separating character). String.Split method is what to use here.

Alternatively, there is JSON, which Unity has an implementation for serializing. There is one problem with this; Unity JSON does not support multidimensional arrays. Which means you'll need to figure out a different way of storing tile data, and write code to convert it to the array. A possible way to do this could be (note, untested):

[Serializable]
public class Structure
{
public int Width;

public int Height;

public string Name;

public List<Tile> Tiles;

public int GetTile(int x, int y)
{
var tile = Tiles.FirstOrDefault(x => x.Location == new Vector2(x, y);
if (tile != null)
return tile.Type;

return 0;
}
}

[Serializable]
public class Tile
{
public Vector2 Location;

public int Type;
}


An advantage with the above implementation is you don't need to store air tiles in the files.

Anything with [Serializable] can in theory be serialized, and also displayed in the editor. In reality, anything that cannot be displayed in the editor cannot be displayed.