Posted on 30-04-2018
Whenever I start a new project, it’s inevitable that eventually I’ll be writing dirty hacks in my code so it’ll be easier to test stuff. Method calls like ‘if space is pressed set health to 10′ are just all too common. Although ugly this is all fine and dandy, until it gets to the point where there’s more hacks than keys on the keyboard because I need to send variable parameters as well. After forgetting which keys are which for the millionth time, I decided enough was enough and figured to create a Console window, inspired by the likes of Minecraft. In this Console window Commands can be written, with variables given as input as well.
Commands are not much more than a collection of Pieces, which allow for inputs like ‘Give player 10 gold’. Pieces have a string array called options, which determine what information the Piece holds. The Piece ‘Fruits’ could have the options ‘Apple, Banana, Mango’, for example. Every time the Console window is toggled all Pieces are initialized which allows for dynamic information. The Piece ‘Players’ could check which players are online when initialized, and add these to the options array.
Every time the user presses tab, the Console gets the current text in the input field and checks which Commands are viable. This is done by matching a Commands current Piece with the current argument. For a Piece to be viable, only one of it’s options needs to be a match. For example: Given the input ‘Give banana’, a Command with the Pieces ‘Give’ and ‘Apple/Banana/Mango’ is viable.
All viable Commands are added to a temporary list, so the user can cycle through them by pressing tab. Not only does it allow for cycling through commands, it will also autocomplete text. If the current argument is empty, all viable Piece options are added to this list. If the current argument isn’t empty, all viable Piece options starting with the current argument are added. For example: With the current argument being ‘st’, only the Pieces ‘Start’ and ‘Stop’ are added.
When the user submits the input, a loop runs through all Commands to see which one is viable with the given input. When a viable Command is found, a delegate is called with the given arguments.
I’ve posted this project on Reddit where I got a ton of positive feedback, which persuades me to open source it.