C++ cvar system

Started by
5 comments, last by Washu 12 years, 1 month ago
Valve's Source engine and the Quake engine both provide the user with a cool looking console system. In a little project of mine, I've decided that it would be helpful to implement a cvar system so that I could do some realtime debugging. So how does one go about designing such a system and not kill his FPS? I'm thinking an std::unordered_map<std::string, DWORD> would be cool and all, but then console functions would not really exists.
Follow and support my game engine (still in very basic development)? Link
Advertisement
Since the source to quake has been released, you can look at that to see how they managed it.

A simple parser for basic commands should run quite quickly, and needs to be executed in only select circumstances that would likely never have any noticeable effect on your frame rate.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Also, if the CVAR code from the Source engine isn't available, you can always look at the CVAR code in it's predecessor Goldsrc instead.

These days though, I'd probably just be lazy and hook my console up to my Lua interpreter, and tell Lua to [font=courier new,courier,monospace]eval[/font] any console inputs wink.png
laziness ftw. anyways, i've decided to use a giant union of all supported types in vc++ to store cvar stuff because you really don't cvars for your directx renderer class.
Follow and support my game engine (still in very basic development)? Link

These days though, I'd probably just be lazy and hook my console up to my Lua interpreter, and tell Lua to eval any console inputs


I second this. I use this method currently and it works beautifully!
I've attempted this topic just recently myself. My solution came from making a map<string, cvar_t> where cvar_t is a struct that has pointer storage for a few basic types I needed, int*, double*, string*, a flag for the type of cvar it is (int, double, string) and finally the cvar's name. This sidesteps polymorphism but at only the cost of a few extra address variables. I then have functions that return the addresses of the cvar value itself, not the struct but the data type address in use based on the type flag, and the calling functions should know what type of cvar they're dealing with thus knowing which call to make in order to retrieve the proper address.

By returning the address of the cvar's variable, you can reference it in multiple places. If it is changed in one place, its instantly changed in all the others (this is the primary advantage I see in doing it this way). An example would be if you had a renderer that had an int representing whether or not to draw polygons as wireframe or solids, and you change that variable in the console (assuming you did things correctly) the renderer will instantly start drawing wireframes based upon your input instead of having to query the cvar system for your variable on each pass to see whether it should or shouldn't draw wireframes.

As for the processing from the console, I pass everything around as a string and convert to its actual datatype at time of assignment since the particular cvars have type flags. If I'm not mistaken, this is similar to how the quake engine handles cvars.
Its pretty easy to use boost::variant to come up with a typesafe cvar system.


#include <boost/variant.hpp>
#include <iostream>
#include <string>
#include <map>
#include <algorithm>

typedef boost::variant<int, double, std::string> variant;

struct cvar {
std::string name;
variant variable;
};

struct cvar_set_visitor : boost::static_visitor<> {
cvar_set_visitor(variant const& value) : value(value) {}

template<class T>
void operator()(T& i) const {
i = boost::get<T>(value);
}

variant value;
};

struct cvar_print_visitor : boost::static_visitor<> {
void operator()(int& i) const {
std::cout<<i<<std::endl;
}

void operator()(std::string const& s) const {
std::cout<<s<<std::endl;
}

void operator()(double d) const {
std::cout<<d<<std::endl;
}
};

void printer(std::pair<std::string, cvar> const& p) {
std::cout<<p.second.name<<": ";
boost::apply_visitor(cvar_print_visitor(), p.second.variable);
};

int main() {
std::map<std::string, cvar> vars;

cvar someIntVar = {"someIntVar", 1};
cvar someDoubleVar = {"someDoubleVar", 3.14};
cvar someStringVar = {"someStringVar", "Hello world"};

vars.insert(std::make_pair("someIntVar", someIntVar));
vars.insert(std::make_pair("someDoubleVar", someDoubleVar));
vars.insert(std::make_pair("someStringVar", someStringVar));

std::for_each(vars.begin(), vars.end(), printer);
boost::apply_visitor(cvar_set_visitor(2.1), vars["someDoubleVar"].variable);
boost::apply_visitor(cvar_print_visitor(), vars["someDoubleVar"].variable);

try {
boost::apply_visitor(cvar_set_visitor(2.1), vars["someStringVar"].variable);
} catch(std::exception& ex) {
std::cout<<ex.what()<<std::endl;
}

std::for_each(vars.begin(), vars.end(), printer);
}

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement