Before we can set up a little test map it's useful to have some functions for editing data. There are 3 kinds of common functions for arrays:
Reseting can be used to set up variables like the Count that has to be -1 instead of 0 after running. This is again because 0 would still mean 1 item while the array doesn't even exist at the beginning. You could handle this value 1-based if you like but I prefer the count matching the last existing Index of an array. The function also clears your array which is used in 2 situations: Whenever you need to clear it and when the program is closed. You should always release all the resources allocated by your program, this will help error trapping in more complex projects!
The item functions should be clear, Add resizes the array and creates a new item and Remove can be used to remove items from the array. Note that in case of a tile list we can't just remove items because all following indices would be decreased by 1, thus making the whole map invalid. Deleting tiles from a tile set is even harder if you're working with map files that share the same tile set - you'd have to correct each map and decrease the invalid indices by 1! I'll show you some ways to get around this when we need it. At the moment the easiest way to get around is to simply disallow that function.
In the following I'll show you the commented VB code, it's not that much so no long explanation needed here.
Public Sub Reset() 'Reset tiles TileCount = -1 Erase Tile 'Reset map Map.W = 0 Map.H = 0 Erase Map.Data End Sub
So this function resets all counts (in case of the map it's the width and height properties) and erases both arrays, Tile and Data from tMap. It should be called when starting the program and again when closing it. Copy it to the Module named mMap. In larger projects I recommend you to split this function up, the first part then goes into mTile and the second into mMap (I already did this in the sample project you can download at the end of this tutorial).
Public Sub AddTile( iDC as Long, iWalkable as Boolean ) 'Allocate memory TileCount = TileCount + 1 ReDim Preserve Tile( TileCount ) 'Set up new tile Tile( TileCount ).DC = iDC Tile( TileCount ).Walkable = iWalkable End Sub
In the first part the array size is increased by 1 and then the parameters iDC and iWalkable are applied to the new item. Since this function is a part of the tile manager copy it into the mTiles module. Don't be scared of that DC property, we'll get to this later. The DC is nothing more than a reference to a picture so just think of it being the tile image we want to set for this tile.
I already mentioned that we won't support removing tiles in this tutorial. Besides, removing items is one of the most complicated functions when writing data managers so we'll spare this one for later. However, for those who want to try it, here's the article: