What is the correct way to handle cell data model with a tilemap game

Hello,

I’m learning about tilemap in Unity with tutorials and i’m wondering how to have a model for the cell on the map. For comparison, i think to Rimworld or Oxygen not included for an example of game that have game information for cell, altrough I don’t need that much complexity, i’m wondering how they manage this.

For instance in the RPG Creator Kit, there is no “data model” for the cell. The wall / doors and such are simply gameobject with collider.

Is that the good design for a 2D game based on cell driving the gameplay ? For instance, my characers should not be able to traverse a wall or a rock cell, but could also interact depending on the cell he’s facing, and cells themselves could also interact (if you think to an energy/power system or such).

Moreover, it is not possible to get cell information through tilemap because tile doesn’t hold game data (so i’m not able to save a map using the tiles themselves).

I think a better solution would be to hold a map matrix model, containing type of cell, and other information (what building is present, and such), modifying a cell of the matrix will change the corresponding tile in the tilemap.

I am doing right ? Do you have an advice for this type of cell based 2D game ?

Thank you

Here are two common strategies for getting per-cell data:

1. For any cell that needs data, use game objects. This gives you the full suite of per-cell properties and flexibility of attaching MonoBehaviours as needed. TileData has a slot for a gameobject so this is the most built-in option. This option may be more appropriate for games with pre-defined levels because you can control placing gameobjects so their number is reasonable. For a more dynamic game, unless you’re careful about what cells have g.o.s then it could become a problem if maps are large.

2. Keep an independent data store. This sounds like what you’re planning. Parallel to your tilemap, keep a collection (array for static maps, dictionary for dynamic) where you can look up per-cell gameplay data. This allows you to retrieve either the tile information or the gameplay information given a particular cell. When something is built you would write both to the tilemap and to the parallel collection. This collection is what you would serialize. When deserializing, you’d need to ensure that the tilemap has a way to be restored to its original state based on what you serialized. It is even more flexible if your gameplay data is agnostic about its visual representation: the tile could be represented by a tilemap tile, a gameobject, both or neither.

2 Likes