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?
Game Command Console
What problems do you encounter exactly? Is it about parsing the console arguments, interpreting the result, or just keyboard input?
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.
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.
There are ten kinds of people in this world: those who understand binary and those who don't.
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.