Command System

Pages: 12
How can I make a "command system" like in CMD for my game?

Something like
spawn musketmen 10
That kind of depends on how your game is set up. If you specifically want to use the console then perhaps you could keep a separate thread running waiting for input from console.
I want it through a command line I will program into my game. No need for separate threads because I cannot give them arguments besides a void pointer.
So you bind a key to open the console and redirect keyboard input to the console. When the enter key is pressed parse whatever has been input and call the correct corresponding functions. Easier said then done, but what part are you having trouble with?
Well basically the command system itself. Lol.

I want to break the string up and parse it like that. Kind of hard to explain.

Kind of like minecraft's time functionality which is in this format

/set time <interger that represents time>
Last edited on by Fredbill30
No need for separate threads because I cannot give them arguments besides a void pointer.
Please elaborate. You can run asynchronous functions with any argument you like.

DO you have any experience of working with command line arguments? (int argc, char* argv[])? It basically the same.
Last edited on
If I understand your minecraft idea then all you do is have the first word be the function that the user will type and the following words are its parameters. For example:
/set time <interger that represents time>

the set is a function you define that two arguments: a variable that can be set by the user (in this case time) and a value that it can be set to (in this case an integer). You could make use of the stringstream class to help you.
I'm starting to get this but how would I incorporate this? I'm kind of messed up right now because a night at applebee's.
Well how you would bring up a text box will depend on what lib you are using. If you can get a text box, then you just pass the text once the user presses Enter. You then parse the text as I explained above, and you code the methods.
The more commonly used term is "scripting support". For example,

Reading in text is trivial; you have something visible, where the user can type. The hard part is the parsing of that input. That is why many use python or other library, which do most of the boiler plate stuff for you.
1) I suggest to look at command string as at nested commands.
set time 0
^    ^   ^
|    |   subcommand parameter
|    subcommand (or command argument)

So you could do something like:
//Command processer: bool operator()(istream& input)
std::string command, argument;
input >> command;
std::getline(input, argument);
//std::unordered_map<std::string, std::function<bool (std::istream&)>> commands;
if (commands.find(command) == commands.end())
    console::output << "cannot find command " << command;

//time command function inherited from command processor
//Leaf node (time_set) will overload operator() or just be another function or functor not inherited from anything. 
Last edited on
Alright so I have to learn std::maps then?
Wrong attitude. You make it ("have to") sound so negative. It should be positive: "Maps! Something new and exciting!. Cool! Why haven't I studied those yet? Hmm, what else might I learn while I'm at it?"
You can simply use...
if (com001==spawn zombie )
  //your command here 

Umm... This was a simple Example soo... you dont have to get angry and report me
Last edited on
15 mobs, "spawn", "info", "killall", "disguise" commands each takes mob as an argument = 60 combinations. And there will be more as your program grows. Good luck with copy-pasting it. And you will know hat hell for programmers is like, when you need to change something significant.
Use Java If You Wish To Start A Game
Java is bad choice for perfomance-critical (read: with good graphics/complex calculations) games.
Minecraft compiled in native code with Excelsior JET works WAY faster than in Java enviroment (Up to 30% fps increase and less lag when generating chunks)
I still didn't see things like Dwarf Fortress written in Java too. Just because Java is too slow for this.
Ibra I see 2 things wrong with your post. I was actually thinking of doing that but then Command.cpp would be like 100 pages long.

Oh yea, Pro Code I will never use Java to make a game because 1 I don't like the way it feels and 2 I don't like the limits of it.
Last edited on by Fredbill30
I didint say it wase the short way... just the easy way
Not really, Because I'd be near impossible trying to change one thing. The source file itseld would be a few mbs.
Pages: 12