Skip to main content
gamedev.net
🔒 Locked

[solved]debug/command console

Started by kozlojak May 24, 2009 at 6:16 PM 4 replies 900+ views
Original Post
kozlojak
kozlojak
I want to add a debug/command console to my game in the style of quake/doom3. what I would like to be able to do is add commands with a pointer to a function dynamicly without having to make any changes to the console class every time I want to add a command. I thought about function pointers but there is no way to make generic function pointer that will take any type of functions. is there any way to do this? if someone could even point me in the right direction, that would be great. thanks [Edited by - kozlojak on May 24, 2009 7:50:55 PM]
LessBread
LessBread
You could restrict your command functions to a specific function signature. You could also "go meta" on your command functions. That approach is described here.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
xissburg
xissburg
If you're using object oriented languages (C++ perhaps?) you can use polymorphism. Declare an interface like the following:

class Command{public:    virtual void Execute(std::string args[]) =0;}


Then you create derived classes from this that implements the Execute method, which has the logic of that command implemented. Then what you'll need is to create a std::map, where the string is the command name and the Command is the command with that name and fill this map with all commands you want. Then the only thing you have to do is to parse the command line and use the first substring as the name of the command and the others as its parameters (check whether the parameters for that specific command are valid inside the Execute method).
.
oliii
oliii
hmmm maybe with template trickery. But how would you pass arbitrary arguments back to the function?

I'd have to say, I'd do what quake and al. seems to do. A argument parser, a function dispatch and call the parent function with the right arguments.

http://icculus.org/~phaethon/q3/cmdproc/qcmdproc.html

EDIT : basically what xissburg says.

[Edited by - oliii on May 25, 2009 8:38:13 AM]
Everything is better with Metal.
Antheus
Antheus
struct Command {  virtual void parse(const std::string & s) = 0;};template < class P1 >struct Command1 {  virtual void parse(const std::string & s) {    Reader r(s); // something that will parse the string into types    P1 p1 = r.read<P1>();    fp(p1);  }};template < class P1, class P2 >struct Command2 {  ....};


You abstract over string, parsing for different number of parameters is done with template specialization, fp is your function pointer of choice which gets passed concrete parameters.

Actual implementation left as exercise to reader.

It's also possible to parse the string before passing to handler, it depends on syntax.
kozlojak
kozlojak
Thanks for the help guys, I actually found a great article on this site

http://www.gamedev.net/reference/programming/features/consoleSTL1/default.asp

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.