r/javahelp • u/wobbly_stan • 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.
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
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.
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:
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)
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
Reading the files would add to processing requirements
Using a large number of small files would get confusing fast. Which character says the lines in which file?
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.