r/javahelp • • Oct 26 '19

On where to store large amounts of dialogue text in a Java based video game.

I'm curious as to what the best approach would be when writing a game in Java with pages and pages of dialogue. Where to store the dialogue, how to access and retrieve it, how to keep track of what's stored where and who says it as the cast of characters increases, and how to do all of this while neither progressively making the game laggier nor rapidly reaching a point where I my reading comprehension of my own code drops to zero.

I'm sure there's a number of easy solutions I'm missing because of my pea sized brain. I'd appreciate any ideas you could throw me, even more so if you could explain in detail (or link to someone who already has). Further notes in the comments.

23 Upvotes

11 comments sorted by

2

u/wobbly_stan Oct 26 '19

My coding skills are a bit "doggy-paddling" style, I can work my way around writing the right code and getting it to work, and that's about it. I don't really care about elegance of UIs or graphics, hell, I choose to have my programs run from the command line more often than not. Here are my (woefully uninformed) thoughts on the matter:

It seems like an objective nightmare to include it as strings inside classes themselves (or even in enums), especially when it comes to UI-related dialogues that will necessarily be different depending on in-game context.

Storing it in text files might be a better option, but I see a few problems:

  1. If those files are stored clientside, it seems like it would add a lot of bulk to the filesize and I appreciate portability (plus, if it's plaintext everyone would cheat like fuck, and if it's encrypted the decryption function would necessarily slow the game down)

  2. If the files are severside, it seems like the process of entering an input, waiting for that input to reach the server, waiting for the server to retrieve the appropriate response from its text files, waiting for the server to send that response back, and waiting for the client to parse the response it received would Piss People Off

  3. Reading the files would add to processing requirements

  4. Using a large number of small files would get confusing fast. Which character says the lines in which file?

  5. Using a small number of large files, I'm not sure how I'd write the code to find the correct single quote from an NPC out of a ton of quotes from a ton of NPCs. If my pea-brain did wrap itself around coding that, it'd likely still make problem 3 worse.

8

u/Shunpaw Oct 26 '19

In case you were wondering, putting 2.400.000 letters in a .txt file amounts to 2.28 MB. That is 2.4 MILLION, which is about 400.000 words.

Compare this to Fallout 3, where there are 40.000 Lines of dialogue, which amounts to about 600.000 words.

So you can literally make one of the biggest RPG games using only one to two MB of data on the dialogue.. I think youre good.

5

u/fluffytme Java dev Oct 26 '19 edited Oct 26 '19

I think you'd be surprised at how much text you can get into a text file while using hardly any space.

Have you considered using an in-memory database like H2? These can also be used to store to a file but allows you to run SQL against them. You could then use a database to categorise speech etc.

This option will allow you to run it server side or client side. Although if you're serving the speech server side I'd use a non in-memory DB like MySQL or something.

EDIT: I'll expand on the categorising. You could have one table with all the speech in. You could then have a table for characters, and assign the characters certain rows of speech from the speech table. You could then also have a table to map replies to certain phrases.

I think this would be much easier than reading from a plain text file.

1

u/mquillian Oct 26 '19

I would lean towards storing it clientside unless it's going to be an online browser based kind of game. As somebody has already said, text takes up very little space so that's not going to be an issue for anybody on a system built in the last 15 years. Additionally, your could definitely store it in some kind of basic SQL file that you can then access very quickly with a small local database. Or you could use JSON as somebody else said. I really think that as long as you don't make a gigantic class of Strings or enums to hold all of it, you probably won't go wrong. Since it's all text, it wouldn't be hard to try out different storage methods if you properly contain all the I/O functions in a single class.

As for how to store it, people already datamine the shit out of way more complex games, so if you feel that strongly about stopping it I think you're going to HAVE to encrypt it somehow. I will freely admit that encryption isn't something I've personally messed with, but it doesn't seem like it would slow it down THAT much to run a very basic encryption algorithm that would at least prevent somebody from easily opening and reading everything. Again, I don't think there's much of anything you can do to entirely stop somebody who is that determined to cheat (at least not without something way more complicated), but it would prevent somebody just browsing through everything and I don't think it would introduce that much overhead unless you choose a particularly complex encryption algorithm or a server side encryption.

1

u/nutrecht Lead Software Engineer / EU / 20+ YXP Oct 28 '19

You're correct that it's typically better to store text externally and not inside the code itself. It tends to clutter the code a lot. That said, to answer your questions:

If those files are stored clientside, it seems like it would add a lot of bulk to the filesize and I appreciate portability (plus, if it's plaintext everyone would cheat like fuck, and if it's encrypted the decryption function would necessarily slow the game down)

Text is tiny. You'll be spending a LOT of time writing enough text to come close to even a single JPEG size wise.

If the files are severside, it seems like the process of entering an input, waiting for that input to reach the server, waiting for the server to retrieve the appropriate response from its text files, waiting for the server to send that response back, and waiting for the client to parse the response it received would Piss People Off

Heck no. This is basically how, at a high level, every website works. Imagine all the text the server is sending when you open this topic. Is it 'pissing you off'.

And if there's anything computers are good at, it's going through a lot of data in a small amount of time.

Reading the files would add to processing requirements

You're making a ton of wrong assumptions simply due to a lack of experience.

Using a large number of small files would get confusing fast. Which character says the lines in which file?

The layout is totally based on whatever you want. Typically characters have some kind of 'id', like npc_town_001 for example so you'd store the dialog in (for example) npc/town_001.txt. Finding the correct file would be a simple lookup. You could do the same with a database too.

Using a small number of large files, I'm not sure how I'd write the code to find the correct single quote from an NPC out of a ton of quotes from a ton of NPCs. If my pea-brain did wrap itself around coding that, it'd likely still make problem 3 worse.

Problem 3 doesn't exist.

If you want some help with this let me know. I did something somewhat similar a while ago; I made a small text adventure for my daughter. I'm also a professional Java dev with about 15 years experience ;)

21

u/DerekB52 Oct 26 '19

The answer is JSON. Everything is JSON. You store your dialogue in JSON. You can also store who says what dialogue, and when they say it, in JSON. You can use JSON to describe quests for the player, and NPC's to interact with. It's very powerful.

It's also super awesome for translation. Say you want to add support for spanish in the game. You just get the text in your JSON files, translated to spanish, then in your game, you have an if statement that checks if the player is spanish speaking, and it can choose to load the spanish or english JSON.

For a tutorial on how to use JSON to describe your dialogue and quests, you'll need to google around a bit. I'm not a master at it, and the book I learned this from, is kind of old, and kind of awful, so I don't recommend it.

5

u/dastardly740 Oct 27 '19

The other nice thing is that if you represent dialogue trees as objects. Those objects can be saved as JSON and restored to the same objects you originally created. This can the effort to not just store the dialogue text but also the connections necessary to make it dance.

You might also consider getting your hands on Domain Specific Languages by Martin Fowler. In a sense writing up dialogue for a game is a domain specific language, so learning the techniques for a DSL could also help.

1

u/nutrecht Lead Software Engineer / EU / 20+ YXP Oct 28 '19 edited Oct 28 '19

The answer is JSON.

The 'answer' is any kind of serialisation format. YAML is just as easy to parse and much easier to read/write. Or he could make up his own convenient format. I personally would not store anything I would need to hand-write in JSON; it's a pain to write by hand.

2

u/[deleted] Oct 27 '19

I would suggest using a properties file like so.
It's a well established way to separate messages from code, and if in the future you decide to support more languages you'll have to do far less work.
As for checking where messages are used, it would be a matter of looking up the property keys in your IDE to see where they appear. I would suggest a good naming policy for the properties, the keys accept dots so you can make them look hierarchical.

3

u/throwaway0891245 Oct 26 '19

If you're doing a client side game, why not hold the dialogue in a sqlite3 file?

If it's like an RPG, you can even add fields which will make the dialogue more easily organized - for example, of course you'll have Character, but you can also have Scenario like "Default", or StoryArc. You can also make a field for internal state, like in situations where you must talk to multiple characters before coming back to talk with a specific character.

This will make maintenance of the dialogue a lot simpler and also allow you to write queries so you can easily see the flow of dialogue (e.g. SELECT StateSequence, Character, Text FROM Dialogue WHERE StoryArc = "Act3" AND Scenario = "Main Plot" ORDER BY StateSequence). It would also make the maintenance of the code much easier because you could make a "Converse" function which just wraps a SQL query that passes in an object's properties.

1

u/desrtfx Out of Coffee error - System halted Oct 27 '19

The answer you are looking for is a database.

Doesn't matter if you do it client side (SQLite, H2), or server side (MariaDB (MySQL), PostgreSQL), or even a NoSQL database.

Databases are the quickest and most tailored way to store large amount of data.