Skill Tree Activated

The quest to become an indie game developer

#100DaysOfCode#100DaysOfGameDevC#Illogical BaconPanic RoomScript-writingUnityVisual Studio

Hashtables

#PanicRoom Day 097

#100DaysOfGameDev R2D62
#100DaysOfCode R2D62

Working with Hashtable

Working with splitting room blocks and making room assignments was showing me a pressing need to have a way to not only define individual rooms, but store the data that tells me exactly where they are built so I can work with it later on when I know I’ll need to decide what a room is for and how to set it up.

Initially, I thought this would be accomplished with an Array, but I quickly saw this wouldn’t work due to having to predefine the size of an array or go through a lot of effort later to reconstruct a new one later when it needed to increase in size due to the number of rooms being created.

person hands woman pen
Photo by energepic.com on Pexels.com

My next thought was to use a List, as these can be added to or modified on the fly but, unfortunately, are somewhat limited in scope and I know I have a certain amount of data I need to store related to any given room.

This led me to the discovery of Hashtables. A Hashtable is a type of Dictionary that uses string value keys to access data instead of index value access as with an Array. And like Lists, a Hashtable can easily be added to or subracted from as data needs to change. It’s like having all the benefits of an Array and a List all in one plus Hashtables can also store mixed data types, which makes them very versatile.

my Hashtable key elements

I set up my Hashtable to store four key elements of room assignment: the starting X position of the room, the starting Y position of the room, the Width of the room, and the Height of the room. Those elements will allow me to know where any assigned room is located and what its size is at any given time. As rooms get split into smaller rooms all I have to do is change the key elements to reflect the changes or start a new room assignment key set. This is going to help me out so much down the road when I need to take my rooms and set them up based on what type of room they will be. Plus, I can add new key elements at any time to reflect new data about a room that I will need to track, such as the room type I just mentioned.

setting up the Hashtable to track rooms Aa and Ab

Leave a Reply

Your email address will not be published. Required fields are marked *