Sign in to follow this  

Game Command Console

This topic is 683 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So while working away at making my current game project i thought it'd be useful to create a game console. one for inputting commands and having that trigger events in the game. Part of the reason i want it is i'd like to have the console act as part of the gameplay, to have the console pretend to be an npc in game, it seemed like something that could be fun and if done right, make devoping the game/levels go slightly faster. like if i added a spawn command, and other helpful stuff like that. however im having implementation problems. I couldn't find almost anything online on the subject, not sure if im just using the wrong keywords or what. But i was looking to find some good example code for this sort of system to look at. Does anyone have any recommendations?

Share this post


Link to post
Share on other sites

A basic console is
 

while true:
    line = input_line("?")
    args = line.split(" ") # Dumb splitting on space
    command = find_command(args[0]) # Find command to execute on the first argument
    command.doit(args[1:]) # Run the command, taking the other arguments as parameters.

To improve splitting, you may want to add escape option by quotes ("stuff with spaces") or backslash (stuff\ with\ spaces), so you can have spaces in your arguments.

 

Another possible source for you is text adventure parsing, although that usually worries about resolving references with "it" or "she" in eg "give bottle to woman and ask if she can fill it". Parsers there also bother a lot about aliases "n", "north", "go north", and so on.

Share this post


Link to post
Share on other sites

Parsing strings isnt really a problem.

Im having a hard time putting this into words so bear with me. So i wanna keep the code for my console as separate as possible from the game specific code; so i can use it on another project down the road. My problem arises when i want to link say for example, the spawn entity command was typed in. I've parsed it, i know its that command, but im not sure how to invoke that function without giving the console access to practically every system in the game, or creating really weird and sketchy access to some of the really higher up systems. Nothing i can think of seems like an answer im happy with.

 

For some context, i guess i should mention im making a top down 2D rpg in c++ using the SFML library. And ive designed it using an entity component system.

Edited by madgod_zhar

Share this post


Link to post
Share on other sites
Send messages. You don't need to tightly couple the console, if that's what you're worried about. Just add some message-handling to your systems and make a dispatcher. With that in place the console can just be tied directly to the dispatcher.

Share this post


Link to post
Share on other sites

I have a similar system set up. I use fastdelegates for hooking up executable functions to console commands (you can just as easily use std::functions if you want). A very simple system would look like:

public:

  typedef FastDelegate0<void> CommandCallback;

  void registerCommand(const std::string& commandName, const CommandCallback& cbck)
  {
    // Add to map here...
  }

private:

  std::map<std::string, CommandCallback> mRegisteredCommands;

After parsing a command you just look up the correct callback in the mRegisteredCommands map and execute it. You can even support passing arguments to the callbacks. If you use fastdelegates you can save all mementos to the same map regardless of the number and types of arguments. You just need a way to recreate the proper callback before doing the actual call.

 

Another way (and the way I am doing it) is have all registered functions be FastDelegate0<void>:s, ie. parameterless functions, but allowing (and indeed requiring) the functions that are registered with arguments to pop them off a temp storage stack that is filled by the console during the command parsing phase.

Edited by GuyWithBeard

Share this post


Link to post
Share on other sites

This topic is 683 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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