I’ve always been a big fan of the Quake CVARs. They allow you to quickly experiment and prototype with values until you find the sweet spot, right there within the game. So, now that my game is going further in Urho, i was trying to find a good way to handle them.
Right now i have a global std::unordered_map<string, float> that i call from everywhere. I’m pretty sure that globals are evil and so on, but I have no idea how to do this properly. Any suggestions or is this good enough?
Would using attributes do the trick? That would allow you to modify variables by name.
Most classes that inherit from Serializable have code samples of how to register attributes in their RegisterObject method.
One another thing you can do - when you obtain CVAR somewhere in your code, instead of returning value, you can return a pointer to that value, so when that value changes, your pointer will be pointing to already changed value with no overhead. AFAIK, CVARs in Quake are working the same way
Can you really have such union? I cannot compile it because of string’s non-trivial constructor or something like that.
Not necessarily you need map with pointer values, you can have it like map<string, *cvar_t>, and obtain values like
auto iterator = cvars.find(“qwe”);
cvar_t* var = &iterator->second;
So, when cvar in map changes later, pointer var will be pointing to that changed value.
Nah, that was just pseudo-code in my head. I’m using only int now, but i will need a union in the future. But hey, cross that bridge when you get there, right?
Why the iterator though? Is it faster than cvars[“qwe”]?
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.
This is exactly how it works. You register (or declare) a cvar once, then use the resulting pointer to the cvar to read the value from the cvar, so you don’t have to do lookups in the unordered_map everytime. I got so used to that quake style console, I use it in almost every of my projects, even those not having to do anything with graphics