Text Adventure Time! (Loading Objects From Text Files)

Some time ago, when I was brushing off my Java, I wanted to get my hands dirty while making something fun. I LOVE text adventure RPGs, so that’s what I attempted to build.

This is the fifth post documenting my single user dungeon development adventure.

In my last Java Text Aventure post, I left off at commit 25d1c72, where I converted the Rooms and Exits from a 2D array to HashMaps.

Next: loading objects from files! It’s time to make the game slightly more flexible by separating out the object data from the code.

Here is the list of posts published on this project so far!

Check out the repo for this post and follow along!


Target commits:

Running the game:
Since I moved class files into more granular packages, we’ll now need to use the find tool to build all of our classes.

Loading objects from files

In these next commits, I created a lot of problems, but I solved one big one: the data for the objects is now separated from the source code. It also showed me a few things about handling files, which will be useful for managing game saves in the future.

Lastly, to get a little closer to the single responsibility principle, I broke the setting of items, creatures, and exits out into separate methods.

Generating the rooms HashMap from a file

I added entity data files to src/Resources/. CSV isn’t the best approach, but we can optimize later. I didn’t use a library, since I was trying to keep the project dependency-light. Instead, I rolled my own CSV parser using String.split().

I get the file contents with this hack, which creates a new file, then gets the path of that file, substrings it to remove the filename from the string, then appends the path to resources onto that. It’s a poor man’s resource loader!

Next, I use a Scanner to grab each line, which represents one Room. The line is split on , to get the parameters as an array, and the constructor is used to create the Room object.

There are a lot of problems with this approach.

  • There’s no exception handling on the file load
  • The filename is hardcoded
  • The resource path is determined in a somewhat hacky manner
  • There’s an arbitrary array length check with a sentinel value, since if the array has fewer than 3 elements, the constructor for the Room object will fail
  • Using the constructor in this manner requires the file data to match the order and number of parameters. There’s also no graceful way to handle optional elements. This is the main problem with using the CSV/constructor approach.

Additionally, while breaking the exit/item/creature generation out into separate methods is a good idea, the values for the parameters of each of these things are still hardcoded.

Generating items, creatures, and exits from files

Instead of fixing the above problems with the object building from the text/csv file, I just forged on ahead and duplicated the wonkiness. Shame on me.

The one improvement is the removal of the hardcoded data for exits, creatures, and items. This lets the game design occur in simple(ish) text files, and the code just glues it all together. Again, the drawback is that the constructor parameter sequences need to be respected when building the objects in the text files.

Adding an item in 316de76:

Adding an item in fc10baf:

Parameterizing the file reading method

This commit solves one of the above problems: the duplicated code for file loading. The buildRoomsFromFile method now no longer just builds rooms. It loads the correct file based on the filename passed in, then delegates to the appropriate method to add the desired object type.

As with the last post, the gameplay has not changed, but I’m a couple steps closer to an engine for building single user dungeons!

I’d like to end with a visit to our friend, platypus. He’s in the dungeon, two rooms west of the larder.

Matthew Odle

Indie dev, autodidactic polyglot, lover of programming, dad, musician, writer, mentor

3 thoughts on “Text Adventure Time! (Loading Objects From Text Files)

Join the Discussion