0
\$\begingroup\$

I have recently been working on a small game project and decided to implement settings loading at game startup. The problem is that my method seems to be too hard coded and not scalable without having a huge method:

public String loadSettings(String path) {
    String[] file = loadFileAsString(path);

    isFullscreen = Boolean.parseBoolean(file[0]);
    windowWidth = Integer.parseInt(file[1]);
    windowHeight = Integer.parseInt(file[2]);
    ...
}

As you can see, this method will get very large after a while and is at risk of crashing if someone edits the file incorrectly so what is a better way of loading this data?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

risk of crashing if someone edits the file incorrectly

If the configuration is invalid, then it should not work. You should verify if the data on the file is valid (e.g., It has all the fields, it does not have letters where numbers are expected, etc...) and either notify the user, load default settings or whatever is appropriate for your game.

so what is a better way of loading this data?

If you want the user to be able to edit the configuration file by hand - and you do not want to worry about parsing the file - you should be using some well-known format.

Consider to use INI, JSON, YAML, XML or similar. There are libraries that will handle loading the file for you.

Which one to use is opinion based. You will have to pick considering, for example:

  • Is there a library available your language and platform?
  • How hard will it be to edit configuration by hand?
  • How hard is to handle versioning the settings (e.g. loading a configuration file from an old version of the software that used different fields)?
  • How fast is to parse the file?
  • How easy is to validate the data types of the fields?
  • How resilient is the format to corruption?

Alternatively, any other criteria relevant to your case.

If using a well-known format does not work for you, you could create a proprietary tool to edit the configuration file and use a binary format. It will read faster and will discourage messing with the file directly.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for the answer, I think that the best method for me would be to load default settings and ask the user if they wanted to reset the settings file. As for parsing, I think using a binary format would be a good idea as it prevents "casual hacking" and Java has the ability to serialise objects anyway. \$\endgroup\$
    – user93747
    Commented Mar 14, 2017 at 21:59
  • \$\begingroup\$ @Gigas Why are you afraid of people "casually hacking" their config files? A human-readable config file has many advantages. It allows advanced gamers to tweak their config further than your options menu allows and it allows your community to find workarounds for technical problems until you get around patching them (for example when a user managed to screw up their options in a way which prevent them from accessing the options menu). It's also easier for you as a developer, because you have a much easier time experimenting with setting without having to add a UI for them. \$\endgroup\$
    – Philipp
    Commented Mar 27, 2017 at 14:41

You must log in to answer this question.