Should text in a C ++ text game be in code or in external files?

I create a text game using C ++ for a school project, the game works, allowing the user to choose from a list of options in each scene; similar to how the games conducted by "Game Selection" work . As a result of this, I have a large amount of text that should be displayed in my game, however, I am not sure about the correct agreements when working with a large amount of text in the program. Should I just use std :: cout and write the text directly to the code, or do I need to write to the text files used by std :: ifstream to read the text.

My only serious problem with using files to store text is that each user choice produces results in a different paragraph that appears, and as a result, I believe that I will need to create a text file for each paragraph that looks like that this will lead to more problems (for example, using the wrong file name or typo of my code leading to reading the game from the wrong file) than writing text directly to the code. If there is a way to read certain sections of a text file, then it would be useful to know, however, I currently do not know about any such method. However, I am new to C ++, and I’m sure that I still have to learn, so I won’t be surprised if such a method existed.

Any help is appreciated, whether just telling me whether I should enter text in my code or in files to tell me if there is a way to read text from specific sections of a text file. And again, I am very grateful for any help you can provide.

+4
source share
4 answers

I recommend using external files. This greatly facilitates the change of content and does not require recompilation of the entire program for a simple typo.

You can use one file and just separate each paragraph with an empty line. Capturing "all text between empty lines" at this point is trivial.

If the selection causes the paragraph to be moved around the file, you can give them identifiers and load them on the fly by linearly viewing the file for that identifier.

- EDIT -

As requested, an algorithm or two is provided here:

Algorithm 1:

  • Give each paragraph an identifier, usually a prime number in a line, directly above the paragraph.
  • Separate each pair of numbers with blank lines.
  • Parse the file one at a time, looking for a string containing only a number.
  • From now on, you find the paragraph you are looking for, all lines, until the next space becomes the contents of this paragraph.
  • Show to user.

Algorithm 2 (recommended):

+3
source

Please do not put the displayed text in the code. This is an antipattern. You must recompile your game for every minor change to the text, such as typos, and for major changes, such as translation into other languages.

The convention for most programming languages ​​is to put all displayed text in (several) resource files or property files as key-value pairs, where the code refers only to the key of the displayed paragraph and the value will be loaded from this external file. (Usually once at startup.) You do not need to use one file for each paragraph, but kv pairs should be analyzed. There will be utilities for reuse.

+10
source

If you do not plan to translate the game into other languages, you yourself, both approaches have their pros and cons:

  • text in the source: easy to write, the text is near the place where it is used.
  • text in resource files: it is easier to remove duplicate lines, makes it better to structure the text data.

If you just imagine that your application can be translated, you should put all the text in the ressource files. You can even find a framework that will help your translations as a Gnu gettext , but you can find others, for example qt has its own translation tools.

+3
source

Saving text in program files is not a good coding practice. This will lead to unnecessary bloating of the code (not even the code) and the need to recompile if you need to change the text.

A simple solution would be to create a text file with careful formatting, such as line numbers or spaces that allow you to pull out the text you want.

A more elegant solution would be to put the necessary text in xml or json files and, if necessary, read them into your program. That would be a great choice.

+3
source

All Articles