Jump to content

  • Log In with Google      Sign In   
  • Create Account


CVar system (like quake3 console)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 codder88   Members   -  Reputation: 103

Like
0Likes
Like

Posted 19 October 2010 - 05:29 AM

I want to make a cvar system with registering functions, variables...
I want to know some ideas...

Sponsor:

#2 Aardvajk   Crossbones+   -  Reputation: 5257

Like
0Likes
Like

Posted 19 October 2010 - 05:43 AM

For variables, std::map<std::string,std::string> and convert the values to ints, floats whatever when you need the values.

Functions is a bit more tricky. Most conventional scripting languages I am aware of have a standard signature, e.g.:

class script_params { /* ... */ };

typedef int(*script_func)(script_params&);

And it is the responsibility of the native code to decode parameters from the supplied information.

Best way I guess would be to look at how something like Lua (or the more recent and pretty interesting IonScript I spotted on here the other day) handles it. That should give some inspiration.

#3 codder88   Members   -  Reputation: 103

Like
0Likes
Like

Posted 19 October 2010 - 05:48 AM

Quote:
Original post by Aardvajk
For variables, std::map<std::string,std::string> and convert the values to ints, floats whatever when you need the values.

Functions is a bit more tricky. Most conventional scripting languages I am aware of have a standard signature, e.g.:

class script_params { /* ... */ };

typedef int(*script_func)(script_params&);

And it is the responsibility of the native code to decode parameters from the supplied information.

Best way I guess would be to look at how something like Lua (or the more recent and pretty interesting IonScript I spotted on here the other day) handles it. That should give some inspiration.


Thanks :)

#4 KulSeran   Members   -  Reputation: 2152

Like
0Likes
Like

Posted 19 October 2010 - 05:49 AM

There are two major ways of doing this.
1) using a scripting language to handle much of it.
2) writing it all yourself.
I suggest 1.

For a scripting language, you need only register the functions and variables with the script engine. For LUA, stuff like ToLua++ can help make this process simple. This is the easy way to do this, as the scripting language takes care of all the hard parts for you. The script 'console' handles all the parsing for you and lets you make very complex expressions when needed.

For the do-it-yourself method, I suggest a templated register and get function that just wrap around a bunch of std::map< std::string, var_type * >. For the console you'll have to make a parser for the input string to break it up into symbols that you could then attempt to look up across all the maps to decide what the user wanted.


#5 Flimflam   Members   -  Reputation: 653

Like
0Likes
Like

Posted 20 October 2010 - 12:26 AM

I haven't used C++ in a while (been doing a lot of C# and using the .net compiler to create a scripting environment these days), but when I needed to have a variable or function system like an in-game console in C++, I used Lua to do it. It's safe, gives you one hell of an interpreter from the get-go and is fully featured. You can expose native functions to it and create a completely self-contained environment.

#6 Ectara   Crossbones+   -  Reputation: 2743

Like
0Likes
Like

Posted 20 October 2010 - 02:14 AM

Should you decide to write your own system, as I did(in C), the first thing you'll want to do is to make an unordered hash table or something of the sort along with a function to create hash keys from strings to quickly get variable nodes regardless of node count. After that, you only need to define what the node contains, and get and set functions containing conversions from the value string to the indicated value.

#7 Zipster   Crossbones+   -  Reputation: 545

Like
0Likes
Like

Posted 20 October 2010 - 03:18 AM

A full scripting language is probably overkill for a simple console command system (unless you're already using one somewhere else, in which case you'd leverage the same functionality in your console). Take something like this for example:

class ConsoleCommand {
public:
virtual void Execute(const string& params) = 0;
};

class EchoCommand : public ConsoleCommand {
public:
virtual void Execute(const string& params) {
cout << params << endl;
}
};

unordered_map<string,string> ConsoleVariables;

class SetVariableCommand : public ConsoleCommand {
public:
virtual void Execute(const string& params) {
vector<string> tokens = Tokenize(params);
assert(tokens.size() > 0);

ConsoleVariables[tokens[0]] = (tokens.size() > 1) ? tokens[1] : string();
}
};

unordered_map<string,ConsoleCommand*> ConsoleCommands;

template<typename T>
class ConsoleCommandFactory {
public:
ConsoleCommandFactory(const std::string& name) {
static T command;
ConsoleCommands[name] = &command;
}
};

void Register_Console_Commands() {
ConsoleCommandFactory<EchoCommand>("echo");
ConsoleCommandFactory<EchoCommand>("print");
ConsoleCommandFactory<SetVariableCommand>("set");
}



Not very complicated, but I've found it gets the job done for your typical console needs.

EDIT: Improved registration.

[Edited by - Zipster on October 20, 2010 10:18:51 AM]

#8 achild   Crossbones+   -  Reputation: 1588

Like
0Likes
Like

Posted 20 October 2010 - 03:22 AM

If you happen to use boost, std::map<std::string, boost::any> is pretty cool...




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS