Over the last few posts, we’ve discussed lists and list-like data structures, such as sets and arrays. All of these are collections of items.
In this post, we’ll be talking about a new type of collection: associative arrays, also known as maps or dictionaries, among other names. Associative arrays store key/value pairs, like real dictionaries.
For example, in a language dictionary, you look up entries by word. The word would be the key in the key/value pair. The word’s definition is the value in the key/value pair. In fact, associative arrays are also called dictionaries in some programming languages, because they work the same way as real dictionaries.
So, associative arrays:
- Store elements as key/value pairs
- Primarily retrieve/search for elements by their keys
- Might also be sorted by keys, depending on the language’s implementation
Names in Different Languages
As we mentioned, associative arrays have different names in different languages.
In many programming languages, the keys in associative arrays must be unique. In such languages, if you try to add a key that already exists to the structure, it will overwrite the old key/value pair already in the structure.
When updating the array, the key will generally remain the same, and you would update the associated value (as you’ll see in some of the application examples).
Applications in RPGs
You can probably imagine several different ways that you could use associative arrays in an RPG.
There are several ways you could possibly use associative arrays for RPG inventories. For example, many inventories store each item as a name and a quantity. Classic Japanese RPGs such as Chrono Trigger and the Final Fantasy series use systems like this. Such an inventory might look something like this:
Potion: 10 Ether: 25 Iron Sword: 2
In this inventory, you have 10 potions, 25 ethers, and 2 iron swords. In an inventory like this, the program would update the quantity (the value), while the item name (the key) stays the same.
Another possible application might be in a game that displays item descriptions to the player. For example:
Potion: Restores 150 hit points. Ether: Restores 100 magic points. Iron Sword: Basic beginner weapon.
In this case, the key is the item name (again), and the value is the item description.
High Scores and Leaderboards
Some RPGs, particularly multiplayer games such as World of Warcraft, might feature leaderboards, highlighting players with the highest scores. You could implement such a feature with an associative array. In the key/value pair, the player’s name would be the key, and the score would be the value. Depending on the language, you could pair the associative array with the language’s sorting functionality to display the scores in order.
As you can see, associative arrays, no matter the name, can be very useful data structures. These examples are just a few ways that you might use them in an RPG. See if you can think of or find more examples, and feel free to share them in the comments.
As a reminder, I am putting together an RPG course. If you’d like to provide some feedback about what you’d like to learn, and get updated when it’s ready, please fill out this form. And don’t forget to check the page on developing RPGs for more learning.