What role will data play in your game?

It doesn’t matter what type of application you’re building. Data will be important. You need to decide what parts will be driven by data and what will be driven by code.

Now, it would be possible to specify in code that a tree should be located at some location. Maybe the constructor for the tree class takes coordinates right from the beginning. There’s probably going to be lots of trees in the game. So the code could go through a loop and read coordinates from an array of data points specified in the code. This is using data but listen to the full episode for a better way. You can also read the full transcript below.

And if you like this podcast and want more, then you can select a reward level at Patreon. Every patron gets access to a special episode each month and you can even help choose the topic. Click the link at the top of the page to “Become a Patron.” Thank you!

Transcript

It doesn’t matter what type of application you’re building. Data will be important. You need to decide what parts will be driven by data and what will be driven by code.

But first, why is this important? I mean, you’re programming so shouldn’t everything be written in code? And what do I mean by having parts of your application driven by data?

Data by itself is just a bunch of numbers or maybe some strings, dates, and flags that can tell if something is true or false. You can’t build an application of any kind with just data. You wouldn’t be able to make any sense of it. There needs to be code.

But you don’t have to do everything in code. As an example, consider the maps that I described in the previous episode. These aren’t the map data structure but a map showing the location of towns, roads, trees, and mountains in a game.

Now, it would be possible to specify in code that a tree should be located at some location. Maybe the constructor for the tree class takes coordinates right from the beginning. There’s probably going to be lots of trees in the game. So the code could go through a loop and read coordinates from an array of data points specified in the code. This is using data but I’ll explain a better way in just a moment.

Another way using just pure code is to calculate the coordinates each time a tree is created. This could be done using a random number generator and you would end up with trees spread through your map as if you had manually specified the placement of each tree. The player probably won’t notice the difference. Unless, of course, you manually placed trees in a recognizable pattern. It would be hard to duplicate a specific pattern using only a random number generator.

If you want some trees to be lined up neatly in a row, then you’ll have to either place them there by specifying their coordinates or write code that takes a line and a spacing and drops trees onto the line at regular points.

The problem with all this is that code a lot more difficult to change than just changing some list of numbers. If your trees are placed in your world by code and you want to rearrange them, then you have to write new code just for that purpose. But instead, if your code just loops through a list of coordinates and reads the values where each tree should be placed, then it’s easier to rearrange the trees.

I mentioned just now that you could put this list of coordinates in your source code. You can declare an array of ints and give that array a bunch of values that you type right into your source code. This is certainly a lot better than writing thousands of methods like createTreeAtFrontGate, createTreeAtGardenCenter, createTreeAtGardenLeftEntrance, etc. Instead you have a method called createTrees that takes as one of its parameters an array of x and y coordinates. Each coordinate would be an integer number and you give the entire list of coordinates to the method and it just goes through each coordinate and creates a tree at that location.

Like I said, this is better, but not good enough. What we really want is to move that list of coordinates out of the source code completely. Why?

Well for a couple reasons at least. The first is that you can then have other people provide the coordinates of the trees who don’t need to know anything about your code. If you’re working on a large project with a team of people, then there will likely be some more artistically inclined people who are better able to arrange the trees than a programmer. Most programmers tend to be very logical. And trees don’t follow logic when they sprout from a seed.

The second reason is that the number and location of trees can be specified without needing to recompile your code. You don’t need to release a new version of your application executable just because you want to add or remove a tree. This means that your game can be modified if you follow this approach. Your players can create their own worlds. Normally, we think of modding a game as writing new components in script that the game will use. Scripting is related to moving data outside of the code. So while just moving data may not make your game fully moddable, it is a good start.

There’s a couple widely used methods you can follow when moving your data outside of the source code. There are others but these are widely used.

The first is to use configuration files. These can be either binary or text based. Now, if you’re like me, then editing a raw binary file is not an easy thing to do. You’ll need to use some other application to do this. There are programs designed just for arranging the terrain and other items found in a game world. These programs don’t let you play the game. Just create or modify the world definition. Sometimes, a program might write out text data that’s actually readable.

You’ll need to decide if you want to use some existing program to define information like this, or write your own utility application to create your data, or just write a text file by hand. The nice thing about a text file is that you can start out with your data created manually. Then as you want more and more data, or even just as a way to help reduce mistakes, you can write an application to help you produce your data.

If you use configuration files, you’ll usually be reading the entire file each time you start the game. The configuration file can be used to control basic behavior of your game or some high level information.

For the game that I’m working on, I plan to put the most zoomed out map information in a configuration file. This is pushing the limits of what a configuration file is normally used for but it seems like a good way to start.

Another popular method is to use a database. This is not something you’ll need to write yourself. Instead, you’ll need to choose a database to use and then write your code so that it requests the information it wants from the database. The database manages all the data in its own format. There’s no manual editing of database files. That doesn’t mean you can’t change the data outside of your application. But that you need to use whatever tools the database provides to work with your data.

I’ll explain how databases work in future episodes. For now, just understand that they allow you to store large amounts of data, sometimes more than will fit in your computer’s memory, and then you can add more information, read data from the database, delete data, and change it whenever you need to. The database does this quickly and accurately.

There’s different kinds of database engines. One common database uses SQL which stands for Structured Query Language.

If you have a lot of data to work with, then you can put it in a database so it’ll be available when you need it. You don’t need to read all the data when your application starts.

For the game that I’m working on, I decided to use a combination of data and code. The game world is huge and there’s no way that I would ever be able to place all the trees even after separating the tree location data from the code. And even if I could write out that much data, it would be too big. It would be too big to fit in memory for certain. And also probably too big sitting in database files.

So I’m working on a compromise where the top level map will specify the location of the forests and other terrain types. Then the code will use random numbers to place the majority of trees found in the world. Any trees that need to be special will be in the data. I’m just using trees as an example. But the same thing applies to bushes, plants, boulders. etc. The entire world will be a mix of some things specified through data and the rest will be placed through code.