Data Structures in RPGs: Arrays

In RPGs (and any game), data structures are vital components of the game code.  A data structure is a scheme or construct for storing, organizing, and accessing data in a game.

There are many different types of data structures in programming languages.  Some of them include arrays, lists, sets, and maps/dictionaries.

In this post, we’ll be looking at arrays, a common feature in many programming languages.  This will be the first in a series of posts on data structures in RPGs.

* Note: In these discussions, I am only suggesting ways that we could possibly implement game functionality using certain programming languages and features.  In reality, many of the real game examples are likely done differently, in a different language.  For example, I frequently mention older games for consoles like Super Nintendo.  Many of them came out before certain languages, such as Java, even existed.

Overview of Arrays

Storing Different Types of Elements

An array is an ordered grouping or storage container of related items.  The exact details vary depending on the programming language, but in many object-oriented programming languages, such as Java, C++, and C#, all items in an array must be of the same type.  For example, you could have an array of high scores that only contains integers (whole numbers).  Or perhaps you have an array of player names that can only hold strings (text).  In such languages, trying to add a different type of element to the array will cause an error.

Other languages, such as JavaScript, allow you to store any type of object you want in an array.

Dynamic Versus Fixed-Length

In many languages, including Java, arrays are fixed-length.  When you create them, you must specify how many elements it will contain, and that size cannot change.  For example, refer back to the code we went over in the lottery mini-game tutorial, which we did in Java.

Recall that we created an array to store lottery ticket numbers, and we gave it a size equal to the number of tickets:

int numTickets = 2;
int[] tickets = new int[numTickets];

We created a new array, containing only integers, and made it the size of the numTickets variable, equal to 2.

In Java, this array that we’ve defined can have no more than 2 elements, but some other languages, such as JavaScript, allow you to dynamically change the array size.  Make sure you check the usage details for your specific language.

Array Index

Each element in an array has a numerical index, or place number.  In many languages, the first element’s index is 0, and then the index increases by 1 for each subsequent element.  So the second element is at index 1, the third is at 2, and so on.

Many languages allow you to access elements by their index.   We saw examples of this in the Java lottery tutorial.  For instance, let’s say we wanted to get the first lottery ticket number:

int firstNumber = tickets[0];
System.out.println(“First lottery ticket number is: “ + firstNumber);

We can also use this approach in many languages to set or change the value of the first number:

tickets[0] = 45;

Most programming languages also have methods and functions to sort arrays and do other operations.  As I said earlier, the way you use arrays will depend on the language you use, so make sure you familiarize yourself with them.

How Does this Apply to RPGs?

So how could we possibly apply this data structure to an RPG?  We’ve already discussed the example of the lottery prizes in the Yo-kai Watch games.  Recall that we used an array to store the prize names.  As we mentioned, each lottery has a predetermined number of prizes.  Thus we can create an array that has just enough space to hold all the items, or less.

Fixed-size arrays are great if you know how your collection of items will be, and if it will stay at a constant size.  Here are some common examples of such collections:

  • Fixed-Size Inventories: If you know that your characters have a limit on how many items they can carry, a fixed-size array could be a good option.
  • Limited Parties: Many RPGs allow you to create a party of characters.  There’s usually a limit to how many party members you can have at one time.  In languages with fixed-size arrays, such an array might be a good fit for the list of party members.
  • Game Save Slots: In some RPGs, particularly those on console and older games, have a limited number of saved game slots.  Here again, fixed-size arrays would be a suitable option.

However, even if you know the maximum size you’ll need, fixed-size arrays have a possibly significant drawback.  If you do not fill up the array to its maximum capacity, you waste the extra time and memory.

In languages such as JavaScript, in which arrays can change size, you can avoid some of these drawbacks.  Other languages, such as C# and Java, have alternative data structures that are similar to arrays, with changeable sizes and useful operations, which we will discuss in a future post.

A lot of the benefits and drawbacks of arrays are related to memory, performance, and what operations you can perform on them.  I won’t get into the details of that here, especially as the discussion depends so much on specific languages.  But hopefully this short overview gave you a better understanding of the array data type overall.

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.

Sign up to receive updates about new tutorials, game news, and educational opportunities.
We hate spam. Your email address will not be sold or shared with anyone else.

Share This:

Tagged , , , , , . Bookmark the permalink.

2 Responses to Data Structures in RPGs: Arrays

  1. Pingback: Data Structures in RPGs: Lists - Cloudy Heaven Games

  2. Pingback: Data Structures in RPGs: Sets - Cloudy Heaven Games

Leave a Reply

Your email address will not be published.