VBfx / Tile tutorial / Step 4

Level map

Now that we have tiles list we need a map to store the indices. The map should contain some information about the map (like width and height) and of course the map data. In code this would look something like this:

Public Type tMap
    W as Long 'W stands for width
    H as Long 'H stands for height
    Data() as Long
End Type

Since we're running for clear code put this declaration in a new Module called mMap.

Data() is the array where we store our map data while W and H describe it's size. Note that it's nothing more than a list of numbers which are indices to the corresponding tile! In this tutorial I will teach you how to use a 1-dimensional array to store the map data, even if the map has 2 dimensions (x and y). Therefore we will use (W * H) to calculate the number of elements in Data. It's just important that we have enough memory to hold the map data, not how it's stored.

The following illustration shows you where the array indices are located in 2D space. Note that this has nothing to do with the tile indices mentioned before!

Index map

If you watched carefully you may have noticed that 4x4 would be 16 but the last index is 15, that's because the first index is 0. If you care about this wasted tile you can use (W * H) - 1 instead to resize the array.

Again here, we just declared the Type which describes a map but is not a variable you can use. Slightly different than before we only need one single map and not a list of maps, the declaration therefore is getting quite easy:

Public Map as tMap


Now that we have both, tiles and map, we can begin coding functions to add, access, save and load all data. What we've done is building the core requirements for our tile engine. You should always spend some time thinking about your needs before writing the Types or Classes. As mentioned before there's a number of properties you can declare and this also goes with the level map. Eg. you could include the map name and a short description that will pop up if the user enters this area.

You should be sure about your Types before you start programming the manager functions. If you tend to add more properties later in coding you might get into problems when reading data from older files. This is not important for small projects but as soon as you need to keep existing data this will produce you a lot of work. However you can get around this problem by storing a version number in each file (details about this technique will follow).