Implementing a Default Config

How could we implement a default configuration that is used if the config file is missing or invalid?

To use a default configuration when the config file is missing or invalid, we can add a method to our Config class to set default values and modify the Load() method to use these defaults if the file can't be loaded.

Setting Default Values

First, let's add a SetDefaults() method to our Config class:

#include <SDL.h>
#include <iostream>
#include <string>
#include <vector>

class Config {
 public:
  std::string WindowTitle;
  int WindowWidth;
  int WindowHeight;
  std::vector<int> Levels;

  void SetDefaults() {
    WindowTitle = "Default Window Title";
    WindowWidth = 800;
    WindowHeight = 600;
    Levels = {1, 2, 3};
  }

}; int main() { Config MyConfig; MyConfig.Load("missing.txt"); std::cout << MyConfig.WindowTitle << "\n"; std::cout << MyConfig.WindowWidth << "\n"; std::cout << MyConfig.WindowHeight << "\n"; }
Failed to open config file: Couldn't open missing.txt
Default Window Title
800
600

Modifying Load()

Now, we modify the Load() method to call SetDefaults() if ReadFile() returns an empty string (indicating the file was missing or couldn't be read):

void Load(const std::string& Path) {
  std::string Content{ReadFile(Path)};
  if (Content.empty()) {
    // Use default values if the file
    // is not found or invalid
    SetDefaults();
    return;
  }
  ParseConfig(Content);
}

Explanation

  1. SetDefaults(): This method assigns sensible default values to each member of the Config class. You can customize these defaults to whatever makes sense for your game.
  2. Load() Modification: In the Load() method, we check if Content is empty after calling ReadFile(). If it is, we call SetDefaults() to initialize the Config object with the default values.

Usage

Now, if you try to load a config file that doesn't exist or is invalid, your Config object will be populated with the default values. This ensures your game can still start with reasonable settings even if the configuration file is missing.

Parsing Data using std::string

Learn how to read, parse, and use config files in your game using std::string and SDL_RWops

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Moving Past Delimiters
Why do we use Start = End + 1 to move to the next position after finding a delimiter (like \n or ,)?
Trimming Extra Spaces
What if a key in our config file has multiple spaces before or after it, such as " KEY : VALUE"? How can we trim leading/trailing spaces from the key and value?
Config from Network Stream
What changes would be needed to load the config from a different source, like a network stream, instead of a file?
Saving Config Changes
How could we extend the Config class to allow writing the configuration back to a file, effectively saving changes made in-game?
Error Line Numbers
When we encounter an error parsing a file, how can we output an error message that includes where the error occurred - for example, the line number within the file?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant