Many famous game such as Quake or Half-Life use CVars to allow easy and flexible game parameter manipulations.
You can simply open up the console and write something like
sv_cheats 1
to enable cheat codes.
CVars are used intensively during the game - if they were set or retrieved by string name lookup there would certainly be a performance hit. Therefore, I'm assuming CVars are somehow cached.
How would this kind of system be implemented? I thought about storing pointers to the real values in a map having the CVar names, and then use the real values in the game. Is this a viable approach? Do the AAA games have a better one?
Example:
struct Game
{
std::map<std::string, int*> cVars;
int volume, sensitivity; // Example game parameters
Game()
{
cVars.emplace("volume", &volume);
cVars.emplace("sensitivity", &sensitivity);
}
void setCVar(std::string mName, int mValue)
{
*cVars[mName] = mValue;
}
};