I have implemented my devconsole like this (I don’t have the code around)
class CVar {
public:
// getters and setter
String GetName() const;
String GetDescription() const;
String GetValue() const;
String GetDefaultValue() const;
int GetInt() const;
float GetFloat() const;
private:
String name_;
String value_;
String default_value_;
String description_;
int flags_;
CvarManager* manager_;
CVar (const String& name,
const String& value,
int flags,
const String& description,
float valueMin,
float valueMax,
CvarManager* owner);
}
// in the cvar manager:
class CVarManager {
public:
...
// Register a Cvar with the manager.
CvarPtr AddCvar(const std::string& name, const std::string& value,
const std::string& description, int flags = CVAR_ARCHIVE,
bool has_min = false, bool has_max = false,
float valueMin = 0.0f, float valueMax = 0.0f);
const std::string& GetString(const std::string& name);
int GetInt(const std::string& name);
float GetFloat(const std::string& name);
bool GetBool(const std::string& name);
void SetString(const std::string& name, const std::string& value, int flags = 0);
void SetBool(const std::string& name, const bool& value, int flags = 0);
void SetInt(const std::string& name, const int& value, int flags = 0);
void SetFloat(const std::string& name, const float& value, int flags = 0);
}
Anywhere in the code:
cvarMaxFps = cvar()->AddCvar("r_maxfps", "60",
"Frame rate limit", CVAR_ARCHIVE, true, false, -1.0f);
The cvars themselves don’t do much, they just store the data and have getters and setters, but the setters actually call the parent cvar manager’s code. That way you can track changing cvars at a single place and save them as config file on change.
Using code from Urho3D it can be done much easier using Variant and VariantMap.