In the previous instalment of our Excel RPG series, we made our game considerably easier to start. We introduced an automated main menu, created navigation controls directly through VBA and allowed the player to begin an adventure without manually running macros or assigning buttons. Our adventurer could move around Old Fort Approach, encounter impassable terrain and receive appropriate messages when attempting to walk through walls or squeeze between obstacles.
But there was something rather conspicuous missing from our world: everyone else. An abandoned fort can certainly be atmospheric, but even the most devoted explorer will eventually notice that there are no creatures to encounter, no enemies to avoid and nobody particularly interested in preventing them from wandering wherever they please.
Today, we're going to change that. We'll introduce a proper character system, give our adventurer health and damage statistics, and populate the starting location with a few monsters. More importantly, we'll establish the distinction between a character type and an individual character, allowing multiple creatures of the same type to exist independently.
We'll also improve our underlying VBA architecture. Some of our earlier modules still contain repeated worksheet names, display ranges and screen coordinates. As our RPG becomes more sophisticated, maintaining those references in several different places would quickly become inconvenient. We'll consolidate them into a shared module and adapt our map renderer accordingly.
By the end of this instalment, Old Fort Approach will contain two goblins and a wolf, each with an independent identity, position and statistics. Our adventurer will be able to walk around them but will no longer be able to occupy the same tile as a monster. The creatures won't move or attack yet. Those mechanics require a turn scheduler and a combat system, which deserve their own development stages. For now, we're going to establish the foundation on which those systems will eventually operate.
Until now, our player character has been rather simple. The modPlayer module stores the character's X and Y coordinates, along with a Boolean variable indicating whether the player has been initialised. When the character moves, the module updates those coordinates, restores the terrain beneath the old position and draws the @ symbol in the new cell.
That's a perfectly reasonable approach when there is only one character in the entire game. It becomes less suitable when we introduce monsters.
Suppose we want two goblins to occupy different positions on the map. Both should have the same basic characteristics: a name, a symbol, a maximum amount of health and an attack damage value. However, they must remain separate individuals. If the first goblin loses three HP, the second goblin shouldn't suffer the same damage simply because both belong to the same monster type. If one goblin moves, the other should remain where it is.
This brings us to an important distinction in game development: definitions versus instances. A definition describes what a particular type of creature is. A goblin definition might specify a maximum of eight HP, two points of damage and the letter g as its map symbol. An instance represents one particular goblin existing in the game world. It has its own identifier, current HP and location, while drawing its initial properties from the shared definition. We can think of the definition as a template and an instance as an individual object created from that template.
We're going to store our definitions in Excel Tables, as we've already done with terrain. Our individual creatures will exist as VBA objects created when a game session begins. This will also apply to the player. Rather than maintaining entirely separate sets of variables for adventurers and monsters, we'll represent both using the same basic character structure.
Before introducing creatures, we need to address a growing maintenance issue. Our existing code refers to several worksheet names and screen locations directly. The map begins at B4, the visible map area occupies B4:AG19, the navigation controls occupy Y22:AG26, and various modules refer independently to the Screen, Maps and GameData worksheets. There is nothing inherently wrong with using fixed ranges. In fact, for our current design, where an entire location must fit on the screen without scrolling, having a defined display area makes good sense. The problem is repeating those definitions throughout the application. If we eventually decide to make the map wider, move the status panel or reorganise the game interface, we shouldn't have to search through every module for hard-coded worksheet addresses.
We'll therefore introduce a standard module named modGlobals. Despite its name, we won't use it to make every variable in the application global. Temporary variables such as loop counters, worksheet references and the currently processed shape should generally remain local to the procedures using them. Instead, modGlobals will contain information that genuinely needs to be shared: worksheet identifiers, interface locations, starting coordinates and reusable functions that retrieve the relevant worksheet or range.
Create a new standard module, name it modGlobals, and enter the following code.
Our worksheet names are now defined in one place. The same applies to the map area, navigation area, status location and message location. The functions provide a small but useful convenience. Instead of repeatedly writing ThisWorkbook.Worksheets("Screen"), another module can simply call GameScreen().
We could store a worksheet object in a global variable and initialise it once, but that would create an additional dependency on the variable having been initialised correctly. VBA object references can also be reset when the project state is reset. An accessor function avoids that problem. It retrieves the appropriate worksheet whenever it is needed, while ensuring that the worksheet's actual name is defined centrally.
We now need to use these functions in our existing modules. In modInterface, the BuildGameInterface procedure should obtain its worksheet and layout areas using GameScreen(), NavigationArea() and MapArea().
One naming detail is important here. If a procedure already declares a local variable named mapArea, rename that variable to mapDisplayArea before assigning the result of MapArea(). VBA does not distinguish uppercase and lowercase letters in identifiers, so a local variable named mapArea would conflict with the function named MapArea.
The relevant assignments in BuildGameInterface should therefore be:
The New Game button's positioning calculations should then use mapDisplayArea.Left, mapDisplayArea.Top, mapDisplayArea.Width and mapDisplayArea.Height. The SetGameplayControlsVisible procedure can retrieve the screen using GameScreen(), while ShowMainMenu in modGame can use the same function and clear the map through With MapArea(). We can make equivalent changes in modMapEngine, using GameDataSheet() and MapsSheet() for table references.
This is a relatively small refactoring, but it establishes an important principle: modules should work with shared configuration through clear interfaces, rather than each maintaining their own independent copies of that configuration.
The original map renderer understood both the logical map and the worksheet location where that map was displayed. It needed to know where the first map cell appeared on the Screen worksheet, how many columns were available and how many rows could be displayed. Now that we've centralised the map area, we can simplify this relationship.
A logical map coordinate of (1, 1) should correspond to the first cell of MapArea(), regardless of whether the visible map starts at B4, D6 or somewhere else. This lets us replace calculations involving absolute worksheet rows and columns with calculations relative to the map display range.
In modMapEngine, replace the existing GetScreenCell function with:
Remember that Excel's Cells collection uses row-first indexing, whereas our logical game coordinates are expressed as X and Y. That is why the arguments appear as (y, x).
The map-size validation inside LoadMap should likewise compare the loaded dimensions against MapArea().Columns.Count and MapArea().Rows.Count. We no longer need separate constants for the first display row, first display column or maximum dimensions, because the range itself supplies that information. Our rendering algorithm, however, remains largely unchanged. We still validate terrain IDs, prepare an array of display characters, write those characters to Excel in one operation and apply terrain formatting in horizontal runs.
Here is the complete updated DrawMap procedure.
Although the procedure is relatively long, the changes made during this milestone are limited. The most important one is that all worksheet positioning now starts with displayArea, obtained from MapArea(). We retain our original validation and horizontal-run formatting, both of which were introduced to make the map renderer more reliable and efficient.
The size validation in LoadMap should also refer to the same shared range:
Once these modifications are in place, the old constants describing the first screen row, first screen column and maximum display dimensions can be removed from modMapEngine, provided no remaining procedures refer to them.
At this point, we should compile the VBA project and briefly test our existing map and movement controls. They should behave exactly as before. We've changed how the renderer determines the worksheet location, not how it interprets terrain or moves the player.
Now we can turn our attention to the creatures themselves. We'll continue using the GameData worksheet as the central repository of game definitions. Rather than defining every goblin's characteristics directly in VBA, we'll create an Excel Table containing reusable character types.
Create a structured table named tblActorTypes with the columns TypeID, Name, Glyph, MaxHP, Damage and Foreground. For the initial test, enter three character definitions. The first has TypeID PLAYER, the name Adventurer, the symbol @, maximum HP of 24, damage of 4 and foreground colour #72D8F4. The second has TypeID GOBLIN, the name Goblin, the symbol g, maximum HP of 8, damage of 2 and foreground colour #E58B72. The third has TypeID WOLF, the name Wolf, the symbol w, maximum HP of 11, damage of 3 and foreground colour #C3B6A0.
These statistics aren't intended to represent our final game balance. They're simply different enough to make the initial character system easy to test. The TypeID field provides a stable reference to each definition. We can change a creature's display name or characteristics later without having to change the identifiers used by the rest of the game. The Foreground field uses the same hexadecimal colour format we've already adopted for terrain. We'll reuse our existing ColorFromHex function to convert these values into colours Excel can display. To allow the actor module to call that function, change its declaration in modMapEngine from Private Function ColorFromHex to Public Function ColorFromHex. The implementation itself does not need to change.
Next, create another table on GameData named tblMonsterSpawns, containing MapID, InstanceID, TypeID, X and Y. We'll use three records for our starting location. The first is a goblin with instance ID GOB01, type GOBLIN, on map FORT01 at (17, 8). The second is another goblin, GOB02, also of type GOBLIN, at (20, 8). The third is a wolf, WOLF01, of type WOLF, at (18, 5). These positions place our test monsters on walkable terrain in Old Fort Approach.
It's important that tblMonsterSpawns does not contain the monsters' current HP. The table only defines where those creatures initially appear when a new game is created. Their actual current HP and position will belong to the individual character instances created by VBA. This distinction will become essential when monsters begin moving, taking damage or dying. We don't want a creature's changing gameplay state to overwrite the original definition of where it should spawn in a new game.
Until this point, we've primarily used standard VBA modules. They're excellent for organising procedures, but they're not ideal for representing several independent objects that all share the same structure. For that, we'll introduce our first class module.
In the VBA editor, select Insert → Class Module and change its name in the Properties window to clsActor. Leave the class's Instancing property at its default value of 1 - Private. Our characters only need to be accessed within this workbook's VBA project, so there's no reason to expose the class to other projects.
Enter the following code:
A class defines the structure of an object. When we create a new instance of clsActor, VBA allocates a separate set of these fields to that particular character. This means a goblin at (17, 8) and another at (20, 8) can both begin with eight HP while remaining independent objects. The TypeID connects a creature to its reusable definition, while InstanceID distinguishes one individual from another.
We're using public fields at this stage to keep the class straightforward. Later, we might introduce private fields and property procedures to validate changes to HP, positions and other statistics. For now, we primarily need a reliable way to store character data.
An important design choice is that our class does not attempt to perform every action a character might eventually take. We're not giving it combat resolution, AI, inventory management or rendering responsibilities yet. Those systems have not been designed, and introducing them now would make this milestone unnecessarily complicated. Instead, clsActor is a deliberately simple representation of an individual character. The module we're about to create will manage the active collection of those objects.
Our new class defines what an individual actor looks like, but we still need something responsible for creating and managing those actors. We'll introduce a standard module named modActors. This module will maintain three important pieces of information in memory: the first is a dictionary of shared actor definitions, loaded from tblActorTypes, the second is a reference to the active player character, and the third is a collection containing the individual monsters currently present in the active location.
We'll keep these variables private to the module. Other parts of the game will access actors through procedures and functions provided by modActors, rather than directly modifying the collection.
Create modActors and start with the following code:
The first procedure should look familiar - it's similar to our earlier terrain-definition loader. We locate the required Excel Table by name, identify its columns by heading and load the contents into a VBA array. We then populate a Scripting.Dictionary, using each actor type's ID as its key. Using the column names rather than fixed numerical positions makes the code less sensitive to someone rearranging the columns of tblActorTypes. The dictionary stores the base characteristics of each type in an array.
For example, the definition belonging to GOBLIN contains the name, glyph, maximum HP, damage and foreground colour of a goblin. The CreateActor function then uses that definition to construct an individual object.
The crucial statement is Set actor = New clsActor. This creates a fresh instance of our class. We then populate its fields using the values from the shared definition and the identity and coordinates supplied by the caller.
Notice that the character's current HP is initially set equal to its maximum HP. Those two properties are then independent. Once combat is implemented, an individual character can lose health without changing the maximum health defined for its type.
This also demonstrates another useful form of encapsulation. The rest of the game won't need to know the order in which values are stored inside our definition arrays. It will ask modActors to create or retrieve characters.
Next, we'll add the procedures that create an active game session. The player is constructed using the same CreateActor function as the monsters. The only difference is that the player's starting position is supplied directly by the game startup sequence, while monsters are loaded from tblMonsterSpawns.
Add the following code beneath the existing procedures in modActors:
The InitialiseActorSession procedure coordinates the creation of our active characters. It starts by clearing any previous actor state and loading the current definitions. It then checks that the player's requested starting position is walkable before creating the player object.
We next initialise a Collection for monsters and call LoadMonsterSpawns. This procedure reads our spawn table, filters its records by MapID and creates an actor for each matching entry. It also checks for invalid placements. We don't want to spawn a goblin inside a wall, place a monster on top of the player or assign two monsters to the same tile.
These checks are particularly useful in a data-driven game because an incorrect value in an Excel Table should not silently produce an inconsistent world. The monsters are stored in a collection, rather than in individual variables such as goblin1, goblin2 and wolf1. That means our game can eventually support an arbitrary number of creatures without requiring new VBA variables for each one. The collection contains references to individual clsActor objects, so every monster retains its own position and current statistics.
For the moment, this is only the active location's monster collection. When we implement multiple active and inactive map states, we'll expand the way character instances are preserved across locations.
The last part of modActors deals with locating creatures and displaying them on the map. In the first two instalments, our drawing model was very simple: the map engine rendered terrain, then the player module placed @ over the appropriate cell. When the player moved away, RedrawTerrainTile restored the underlying terrain. Now we have two kinds of character occupying map tiles, and eventually there will be items, traps and other objects too. We therefore need to distinguish the actual world state from the visual representation displayed in a worksheet cell.
The terrain remains stored in our map array. Monsters exist as separate actor objects. The visible cell simply displays whichever object has the highest rendering priority. For this milestone, monsters are drawn over terrain and the player is drawn last.
Add the remaining code to modActors:
The PlayerActor and MonsterAt functions give the rest of the game controlled ways to retrieve characters. MonsterAt searches the active monster collection for a living creature at a particular coordinate. It returns that actor if one exists; otherwise, it returns Nothing. This will be useful for several systems. Player movement can check whether a destination is occupied; monster AI will eventually need to make similar decisions. Combat will need to identify the target of an attack. For now, the search is a simple loop through the collection. That is more than adequate for our small test map. If we later introduce hundreds of creatures on a much larger map, we can consider more efficient indexing without changing the general idea.
The rendering procedures are equally important. DrawActor is responsible for placing a single character's symbol and foreground colour into its corresponding worksheet cell. DrawAllActors iterates through the active monster collection and draws each living creature, then draws the player. RedrawWorldTile provides a more general replacement for the terrain-only restoration technique we used previously. It first restores the terrain, then overlays a living monster if one occupies the tile, and finally overlays the player if appropriate.
This is the beginning of our layered world renderer. Later, when we introduce items and traps, we'll extend the drawing order without needing to make terrain itself responsible for storing those objects.
The InspectActors procedure is included to help us verify the implementation. It prints each active character's identity, coordinates, HP, maximum HP and damage in the VBA Immediate Window. This gives us a way to inspect the actual character objects independently of their appearance on the worksheet.
We now have two different systems that could potentially store the player's position. Our existing modPlayer maintains private variables for the player's coordinates and readiness state, while our new clsActor object also contains coordinates and health information - but we shouldn't retain both. If two separate systems maintain the same information, sooner or later they'll disagree. One module might believe the player is at (10, 8) while another thinks the player has moved to (11, 8). We therefore need to establish a single source of truth.
For now, the active player object managed by modActors will own the player's identity, coordinates and statistics. modPlayer will remain responsible for processing movement commands, validating requested moves and generating messages, but it will retrieve the character object through PlayerActor(). Remove the original private player-coordinate and readiness declarations from modPlayer. We can also remove the old DrawPlayer procedure, because character drawing is now handled by modActors.
Replace the existing InitialisePlayer, ResetPlayer, UpdatePlayerStatus and MovePlayer procedures with the following code. The movement-direction wrappers introduced in Part 2 can remain unchanged.
The basic movement algorithm should be familiar from Part 2. We still calculate a destination using the requested X and Y offsets and we still check the destination terrain and prevent diagonal squeezing between two blocked neighbouring cells. The major difference is that the current coordinates now come from the player object rather than private variables in modPlayer.
We've also introduced a new occupancy check. After confirming that the destination terrain is passable, we call MonsterAt(newX, newY) to determine whether a living monster occupies that tile. If one does, movement is cancelled and the player receives a message such as "A goblin blocks your way."
This is deliberately temporary behaviour. In many traditional roguelikes, attempting to move into an enemy's occupied tile initiates a melee attack. We'll eventually implement that, but we don't yet have a combat system capable of resolving the action. For now, preventing the player from overlapping a monster allows us to test the occupancy rules without introducing unfinished combat mechanics.
When movement succeeds, we update the player's actual coordinates and call RedrawWorldTile twice: once for the old position and once for the new one. This ensures that both cells are reconstructed from the underlying terrain and actor state.
The status display has also changed: instead of showing only coordinates, it now includes the player's current HP, maximum HP and damage. For our initial adventurer, it should display values equivalent to HP 24/24 D4 (10,8) at the starting position.
The eight public directional procedures that connect to our navigation buttons don't need to change. They already call MovePlayer with the appropriate offsets. The Wait procedure remains a placeholder. We're not going to introduce artificial time advancement here, because the next milestone will establish a proper turn scheduler that can accommodate multiple actors and different action speeds.
We now need to make sure the actor session is initialised when a new game begins. Fortunately, the work we did in Part 3 means that very little needs to change. The StartNewGame procedure in modGame already calls ShowMap, which handles the starting location. Previously, ShowMap loaded the terrain, rendered the map and called InitialisePlayer to place the @ symbol. Our new InitialisePlayer does something more substantial. It calls InitialiseActorSession, which creates both the player and all monsters belonging to the starting map.
The relevant part of ShowMap should now follow this sequence:
Keep any existing procedure structure surrounding these instructions; we're only changing the references and initialisation sequence.
The order is important. First, we load and draw the terrain. Only after the underlying map is available do we create the character instances and draw them above it. We don't need to change the automated navigation controls or the main menu behaviour established in Part 3. When the workbook opens, Auto_Open still displays the main menu. When New Game is clicked, ShowMap prepares the location and the actor session, and the navigation controls become visible. Returning to the main menu calls ResetPlayer, which now clears the active actor session through ResetActorSession.
This also demonstrates why we separated application startup from gameplay in the previous milestone. We can substantially expand what happens during New Game without redesigning the interface responsible for initiating it.
With the new tables, class and modules in place, compile the project using Debug → Compile VBAProject, then save the workbook, close Excel and reopen it with macros enabled. The main menu should appear as before, with the New Game button centered in the map display area and the navigation controls hidden.
Click New Game. Old Fort Approach should appear, and our adventurer should be located at (10, 8). The player will now have health and damage statistics displayed in the status area. You should also see two goblins represented by g symbols and one wolf represented by w. Their positions should correspond to the coordinates entered in tblMonsterSpawns. The monsters won't move or attack, regardless of how many times the player moves or waits. That's expected at this stage.
Try moving the player towards one of the goblins. If you attempt to enter its tile, the move should be rejected with the message "A goblin blocks your way." The player should remain in the previous cell, and the goblin should remain visible.
Movement into ordinary walkable terrain should still succeed. Walls, trees and other obstacles should retain the contextual messages introduced earlier, and blocked diagonal movement should continue to work correctly. It's particularly important that the existing terrain doesn't become corrupted when the character moves away from a cell. The revised rendering logic should restore the underlying terrain correctly and keep stationary monsters visible.
To inspect the actual objects rather than just their on-screen symbols, open the VBA Immediate Window using Ctrl+G and run InspectActors. The output should contain one adventurer, two goblins and one wolf, each with its own identifier, coordinates and statistics. For our initial data, the player should begin with 24 HP and four damage, both goblins with eight HP and two damage, and the wolf with eleven HP and three damage.
Although the two goblins share the same definition, they're stored as separate clsActor instances. This will matter once we begin changing individual HP values during combat.
Next, return to the main menu and start another game. The same starting creatures should appear, without additional duplicate monsters from the previous session.
Finally, close and reopen the workbook to confirm that the automatic startup system still functions correctly after our refactoring.
These tests exercise several different parts of the application at once: our data tables, class construction, actor collections, map rendering, occupancy checks, player movement and startup sequence. If all of them work, we have a reliable foundation for introducing independent monster actions.
At this point, we already have many of the values needed for basic combat. The adventurer has HP and damage, and so do the monsters. We can locate a creature on the map and distinguish its instance from other creatures of the same type. It might therefore be tempting to implement attacking immediately.
However, a roguelike's combat system is closely connected to its turn system. When the player attacks, how much time should that action consume? Does the monster get an opportunity to retaliate? Can a quick monster move or attack more frequently than a slow one? What happens when the player is affected by haste or slowing? These are not questions we want to answer through unrelated counters placed in different movement and attack procedures. Instead, we'll introduce a central turn scheduler in the next milestone. That scheduler will determine which actor can perform an action, how much game time the action consumes and when another actor becomes eligible to act.
Our current actor structure already gives us a useful starting point: every participant in the game has its own instance and state. We haven't yet introduced a speed property because we want to establish exactly how speed will interact with our scheduling model rather than adding a statistic whose meaning hasn't been defined. Once time and actions work, monsters can begin moving independently of the player. We can then introduce attack resolution and damage with a consistent understanding of how gameplay advances.
This development order also prepares us for eventual field-of-vision and fog-of-war mechanics. Testing visibility will be much more meaningful when the world contains independent creatures that can move, items that can be collected or dropped, and objects whose state can change outside the player's sight.
Our RPG now contains something it didn't have before: independent entities. A goblin is no longer merely a letter that we might draw on the worksheet. It is an object with its own identity, location and statistics, created from a reusable definition stored in Excel. The same underlying structure represents our player character.
We've also established a clearer division of responsibilities. modGlobals provides shared configuration and worksheet access. modMapEngine remains responsible for terrain. modActors manages character definitions, instances and rendering. modPlayer handles the player's movement commands. modInterface and modGame continue managing the user interface and application startup. Our map is still fully visible, and the monsters are still stationary, but the engine is becoming considerably more capable.
We've also laid the groundwork for future inventory and equipment systems. When we introduce items, they'll follow a similar principle: shared item definitions will describe the type of object, while individual item instances will represent actual swords, potions and other possessions existing in the world. Those objects can then be placed on terrain, carried by characters or assigned to equipment slots without requiring the terrain itself to store their properties. For now, however, our little band of monsters will remain patiently in place.
In the next instalment, we'll begin changing that by introducing a proper turn system. Our objective will be to give the game its own concept of time, allowing the player and monsters to perform actions according to a central schedule. We'll also lay the foundation for different actor speeds, haste and slowing, rather than assuming every creature must always perform exactly one action whenever the player moves.
Once that system is established, Old Fort Approach will no longer be populated by creatures that merely stand around waiting to be noticed. They'll finally have something to do. And perhaps our adventurer will begin to appreciate just how peaceful the fort was before we started adding goblins.
This article is Part 4 of the Building an RPG in Excel series. The accompanying VBA modules and development workbook allow readers to follow the implementation themselves, and they can be downloaded here.
Cat On A Spreadsheet