In the last couple of posts, we discussed arrays and lists, both sequential collections of related items. In this post, we will discuss sets.
A set is an unordered collection of objects, that cannot contain duplicates. In other words, each item in the set is unique and can only occur once.
Order in Sets
We mentioned that a set is unordered. Recall that for lists and arrays, we could access items through their index, or position in the collection. Items in a set or array are sorted by insertion order, by default. Since sets have no guaranteed order, we cannot access set items the same way. Instead, you could go through each item in the set until you find the item you want.
However, some languages, such as C# and Java, have certain types of sets that do maintain a sorting order.
Checking for Items in a Set
Most languages have a way to check if an item is present in a set. For example, JavaScript provides a function:
const set = new Set(); set.add(1); // Add 1 to the set var check = set.has(5); // The result is false, because the set does not contain a 5
Java also has a method:
Set set = new HashSet(); set.add(1); // Add 1 to the set boolean check = set.contains(5); // The result is false, because the set does not contain a 5
Adding to a Set
In the code examples above, you’ve seen that we can add items to sets. As we mentioned, sets can only contain unique items. Therefore, if you try to add an item that is already in a set, it will not be added. For example, in the next code sample, we try to add an item twice to a Java set.
Set set = new HashSet(); set.add(4); // Add 4 to the set set.add(4); // Since 4 is already in the set, it will not be added again System.out.println("The size of the set is: " + set.size()); // This will print the size of the set as 1
The code would print out “The size of the set is: 1” because the set only contains one instance of the number 4. When we tried adding 4 the second time, the computer did not add it.
Possible Applications
Now that we’ve discussed what sets are, let’s think about how they could be useful in RPGs.
Unique Inventory Items
In some games, there are certain items that the player can only own one of at a time. For example, in World of Warcraft, some items are flagged as unique. If you wanted all of your inventory to be completely unique, you might use a set to implement it.
Party Members
Many party-based RPGs allow you to choose the characters in your party. Of course, you can only have each member in your party once. Perhaps the party selection screen could use a set for choosing the characters. Of course, you should still consider that the party likely has some order. Maybe you can use a sorted set, or some other ordered collection outset of the party selection screen.
Pathfinding
For non-playable characters (NPCs), pathfinding is an import component in artificial intelligence. The idea is that NPCs often must find a path from one spot to another. In each possible spot, there’s a number of adjacent spots that the NPC can take. All of the spots that lead from Point A to Point B make up the path.
At each spot, we can choose from among all the adjacent spots for the next move. But for each move, we only want to consider each adjacent spot only once. Therefore, we can put each adjacent spot in a set, to ensure that there are no repeats. Then we can loop through and test out each spot to see if it’s part of a valid path.
These are just some of the ways that you might use sets in an RPG. Hopefully now you have a better understanding of what sets are and when you might use them.
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.