Storing Data with the Tile Map

I’ve already spoken with @keely about this, but I have some additional thoughts I will share here…

I want to share a concept of storing data that applies to all tiles in the game, and storing data that applies for the specific tile in the cell, in case it gives you any additional ideas.

I will use my game, Super Mario Bros. Crossover, as an example. Let’s say you are placing an item block on a tile map. An item block can contain 14 different items. If you had no way to store data on the tile map, you’d have to create 14 different tiles. But Bricks can also contain the same items, so again, you’d have to create 14 versions of the brick tile without being able to store data. If you are storing data on the map, this would take up 14 bits, and you’d only need one tile each for brick and item block.

However, this data is specific to the brick/item block tile. The game may have another kind of data, as mine does. In SMBC, different tiles appear or disappear depending on what difficulty you are playing on and what character you are playing as. So we would also need to be able to set flags on the cells that say whether or not this tile should appear on easy mode or hard mode, etc. These flags need to apply to all tiles.

So using these examples, I think you would at the very least want two ints to store data. One could be used for storing global data for all tiles (i.e. hiding a tile on hard mode), and the other would would be used for storing data specific to the current tile (i.e. the item an item block contains). You could even call them globalFlags and instanceFlags, but you probably shouldn’t because I don’t know if everyone would use them that way.

Anyway, this is how I would use the data for this game, but I could see myself using the same setup for other games. With one 64-bit int, I could still do it this way by reserving the first 32 bits for instanceFlags and the last 32 bits for globalFlags. It really doesn’t matter how the bits are split up. I’d recommend an absolute minimum of 64 bits, but I think 128 bits would be better and should be enough for most people.

Maybe someone else will come up with a reason for needing more data than just flags, but I haven’t thought of one yet.

I think you can create a general game object for that tile square and use data on that game object to specify what is inside.

Is there a solution for this problem? I need to add different data to individual tiles of the same type as well (e.g. a storage tile, which has an object reference to a class, which has a collection of items that are in the storage).

It is possible to create a GameObject for a tile in the assets, but once it is instantiated, it is the same GameObject instance for all tiles of the same type and it can’t be changed in the editor (but I can change it with a script).

I’ve just bought Super Tilemap Editor, and it allows to store prefabs with a tile, which you can even edit later in the scene when instantiated. So if someone needs to write a tilemap game, I can recommend this asset, much more mature and useful than what you get in the 2D Unity preview, and production ready. Unfortunately it doesn’t support hexagons or isometric tiles so far.

I have also tried the Super Tilemap Editor, but it doesn’t support multiple tilesets per tilemap. This is ok, if there aren’t that many tiles in the tileset or if the game doesn’t support modding. But when you have to draw multiple tilesets on the same tilemap, this is currently not possible with Super Tilemap Editor. Which is why I am actually taking a look at the Unity one to see if it is any good. It seems like the tool and the usability is still a bit early stage, but ability to paint the sprites from multiple atlas source is certainly a plus. At lease content addtional modding seems to be possible with Unity one. Another advantage of Unity editor is that we can let modders to use Unity to build assets for the game and then export as bundles so the game can load them. Otherwise, the game itself needs to provide its own runtime modding tools. If a third party tool such as Super Tilemap Editor is used, then modders can’t use Unity because of the license issue.

The other asset from the same author, RPG Map Editor, supports multiple tilesets per map (you can either have up to 16 tilesets per map or dynamically switch out the entire tileset via code). Yes though, having it natively supported through some data function would be great.

For our game we’ve worked around this by creating a special PrefabTile which instantiates editable objects, that contain data tied to tiles in our object layer (which gets cleaned up on game start so only the objects remain). You can take a look here:

I guess it will work if you do all your level editing in Unity. I am aiming to create sandbox game so I had to store data in runtime in memory and serialize it to save on the disk.