• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Sepiantum

C++ cvar system

6 posts in this topic

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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
Also, if the CVAR code from the Source engine isn't available, you can always look at the CVAR code in it's predecessor [url="http://www.google.com.au/search?q=half%20life%20sdk"]Goldsrc[/url] 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 [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]
2

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1328831053' post='4911484']
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
[/quote]

I second this. I use this method currently and it works beautifully!
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
Its pretty easy to use boost::variant to come up with a typesafe cvar system.
[code]

#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);
}
[/code]
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0