Skip to main content

Adding data

Before we get started using TilemapExtended, let's look at names and basic tool structure for a moment.

Naming overview

Classes and objects where named in order to:

  1. distinguish Unity's own data structures and components (Grid) from the ones used in this API (GridExtended),
  2. distinguish between visual representation (Tiles) and data (Cells).

naming-overview

Defining CellData

At this point it's finally time to define some data to store, retrieve and modify later.

For this example scene, I'm thinking of a base builder game and I want to know the following from every tile:

  • The type of terrain
  • If the tile is walkable
  • The movement speed multiplier
  • If the tile can be build on
  • The fertility of the tile

To store these values, let's create a new C# script called MyCellData. In order to be stored, MyCellData needs to inherit from TExtended.CellData. The script should look like this:

using TExtended;
using UnityEngine;

public class MyCellData : CellData {
//Insert data here
}

Handling the movement data is easy - CellData already contains the fields MovementMultiplier and IsObstacle. Consequently, MyCellData inherits those field and we don't have to worry about them at this stage.

For the type of terrain, I created a TerrainType enum in a separate C# file:

public enum TerrainType {
Grass,
Sand,
Ocean,
Foliage,
}

The final action is to create an asset menu, since data will be stored in a scriptable object which needs to be created in the editor. So with this in mind, MyCellData could look something likes this:

using TExtended;
using UnityEngine;

[CreateAssetMenu(menuName = "CellData/MyCellData", fileName = "New MyCellData")]
public class MyCellData : CellData {
public TerrainType terrainType;
public bool canBuildOn;
public int fertility;
}

Let's create an instance of MyCellData in the editor to modify values:

Right click > Create > CellData > MyCellData

asset menu

Next, we will look at modifying data objects and assign Tiles by creating rules.