In the last article, we looked at the array data structure. This time, we will discuss lists.
In other languages, including Java and C#, lists are a separate data type. They are similar to arrays, but usually have some distinct advantages and differences.
Advantages: Lists vs. Arrays
One of the biggest advantages is that you can dynamically change list sizes. Remember, we said that when we create arrays, we must specify the size. Once we create the array, we cannot change the maximum capacity. We would need to create a new array if we wanted a bigger one. With lists, we can change the size as needed.
In our discussion on arrays, we mentioned that in certain situations, you might have an array that has many empty slots. For example, if you use an array to store an inventory, at times the number of items that your player has might not fill up the capacity.
With a list, you can change the inventory size to only use as much storage as you need at the item. You make the list bigger when you add items, and smaller when you remove items.
Another advantage for lists is that many languages have more functionality for lists than they do for arrays. For example, in Java, lists have methods to remove items from any part of the list. Java will automatically shift and update the position index for every other item in the list, to reflect the smaller size. Arrays don’t have a good way to remove items, without using an additional array, or a list. Even if you set an element in the array to null or empty, you’d still have an empty gap in the array.
Many languages also have more sorting functions for lists (though even Java has some for arrays). Depending on the language, other functionality might include:
- Checking to see if a list contains a give item
- Finding the position/index of an item in the list
- Clearing all the items from a list
So what’s the best way to use lists in an RPG?
As we mentioned, lists can be very useful for inventories. In particular, they are a great choice for inventories that have no set or maximum length. Think about games such as the Final Fantasy series. Games with inventory limits on weight, such as the Fallout or Elder Scrolls series, are also good candidates for list-based inventories. In such games, rather than a limit on the size of an inventory, players can only carry a certain weight. Each item has its own weight, and the total inventory weight must be below a certain amount. Such an inventory does not have a set size, and the number of items will vary.
Lists are also good for save slots in games that allow unlimited save files (though of course, the computer’s memory does in reality impose a limit). I’ve read about some players who have hundreds of save files in games like Skyrim!
Some RPGs also provide players with a list of current side quests and objectives that are in progress. If the player can have an unlimited log of objectives, then a list is a great approach for implementing the log.
Hopefully, this short article gave you a basic understanding of when lists might be appropriate in an RPG. Understanding when to use arrays versus lists can help you to implement many of the features you might want in your own RPG.
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.
Pingback: Data Structures in RPGs: Sets - Cloudy Heaven Games