What is the best way to store all the Text from your project??

For example all the questions and answers of a Quiz game, all the dialogues of an adventure game, etc.

I have been looking into this topic the whole day but I still have doubts about how to do it.

I found that the best way to do it is to save the text in a .json file and then Deserialize it (or use XML). However, I don’t understand why.
Another way I can think of would be to save the text directly in a script using Lists or Arrays ( List myList = new List() {“text1”, “text2”, “text3”}; ).

What’s the difference between both methods?? Is the first one faster, less memory consuming, …??

Thanks!!

1 Like

The main advantage of storing your text in a separate file is that it can be edited in a simple text editor and updated without recompiling your game. This is handy if you change it frequently, or if your writer is not a programmer, or if you want to make the text end-user-moddable, or if you want to have the text translated into other languages. But if none of those things applies to your project, putting it in source code is probably fine (and might be the best choice for a quick prototype regardless).

Once you are putting the text in a separate file, whether you store it as JSON or XML or CSV or something else is mostly a stylistic choice. I wouldn’t worry about performance considerations except in very unusual cases.

Stylistically…I recommend not XML.

1 Like

“Friends don’t let friends XML.”

and,

“XML… not even once.”

Seriously, everything AntiStone says above is fine.

If you do put the text in code to get started quickly, at least put it in a class that has a few methods such as:

public int GetPuzzleCount();
public PuzzleData GetPuzzle( int n);
// etc., anything else you need from a "puzzle database"

That way if you decide to change how you store it in the future, you just change that one class and everything else in your game Just Works™.

So I guess that storing the text in a separate file (not XML :smile:) is the “correct” way to do it. By “correct” I mean that it is the standard method to do it, and therefore the one I will learn to use.

An unusual case would be a mobile text based game, for example?? In this case you would strongly recommend using .json to reduce memory consumption in the final build of the game, right??

I was thinking more like “this game has literally millions of strings in it” or “this is a game server for a hugely popular online game serving millions of players at once so we need every iota of performance we can get” or “this is some kind of hyper-specialized embedded system with weird hardware requirements”.

Even if your game needs to worry about memory, trying to reduce the memory used by your text is still going to be a waste of time in 99.9% of cases. Text is tiny compared to most common game assets (and, therefore, a text-based game probably doesn’t have to worry about memory in the first place). If you are having memory problems, you want to look first at your textures, and then probably at sound.

If you somehow are the one guy on the whole planet who genuinely needs to reduce the amount of memory used for text, putting it into an external file won’t inherently help. But you might be able to save memory if you split the text into several different files and only kept one of them loaded at a time (e.g. maybe a separate file for each level of your game).

But again, if you are doing anything remotely normal, this should not be a concern. If you are really worried, run a profiler on your game to measure it’s actual performance and identify your biggest costs before you waste time optimizing a guess.

2 Likes

This kind of job would lend itself pretty well to just a simple csv file.

1 Like

This is what I was looking for, thanks so much for taking your time to answer!!!

I’ll just stick to Lists and Array in the source code since it’s the most comfortable way to do it for me. Thanks again!!

2 Likes