Creating a Cheat Console in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if i were to say the words rosebud or god mode on to you you probably would have an image of the video games and memories you associate them with after all cheat codes are one of the most appealing things about playing games on pc for me i always love that freedom to break the game and feel like you had total control over the virtual world you were in power fantasies aside as it turns out those cheat codes were often just a small selection of incredibly useful tools the developers of those games would use to make the process of testing and debugging a lot easier i think now we've all experienced the pain of booting up our own projects playing the game hastily clicking through every single frame just to reach the point that we're actually working on testing one thing stopping the game to make a change and then feeling the sheer tedium and dread of needing to repeat the process over and over again at some point you have to ask yourself surely there's an easier way an in-game debug console can make some of that laborious work much easier to deal with by creating cheats that help you skip past some of the stuff you're not interested in or generate specific events that may usually be gated behind a ton of content you'd need to get through first hi there i'm matt and welcome to gamedev guide in this video we're going to build an in-game console that we can use to create development sheets to help when debugging our game we're going to build a console using unity's default gui code as well as a flexible command system that will allow us to add new cheats to the console easily as our project grows in scale finally we'll look at adding some additional functionality to the system to make it a bit more user-friendly and easier to navigate especially when there's lots of commands available so let's get started i've got a small little environment here for demo purposes i've put a very basic scene together where you spend some money and spawn a character that randomly walks around the player can spawn about 10 folk before running out of money now in the editor we can just go in and hack the amount of money in the inspector we can also easily clear out all of our characters from the screen however if we were testing this in a build there's no way we could access this and if i'm just testing the build for our characters on screen and i don't want to play the rest of the game i'm going to need a way to handle this one solution is to just hard code some cheat keys in to run these functions but it's kind of annoying and awkward this is where a debug console can come in handy as it will allow us to enter commands to perform specific functions such as spawning a new character or changing how much gold we have let's create a new script called debug controller this script will hold the logic for drawing our debug console and handling our debug commands so first things first let's start by allowing the user to bring up a debug console on screen at the top of our script let's create a boolean called show console i'm using the new input actions here and i've mapped the key to the little backwards apostrophe thing which in making this video i looked at the proper name for and discovered it's called the grave accent so that's what the key is i suppose and so when this key is pressed we will toggle the show console variable okay so now we need to actually draw the console whenever show console is true so in our on gui method let's check to see if it is true and if it is let's draw a box at the top of the screen so now we have our box toggle let's create a string and a text field for our user to type into all right our command input is ready let's look at building out the logic for commands themselves we're going to build a structure for commands that is mostly agnostic and acts as a dummy to just hold command data the controller itself will handle what the command does and how it passes the data from the text input to do this let's create a new script called debug command in here let's create a new class called debug command base here we'll add three strings that will be required by all of our debug commands a command id which will be used to call the command a command description which will explain what the command does and then a command format string that will tell the user how to actually format the command parameters with that it's time to set up our first debug command class which will extend from this base class the idea with these debug command classes is for them to be as generic as possible so that we can use the class for multiple actions at the start of the class we'll create an action property called command and in our constructor we'll pass in our base parameters and also a command to invoke we'll then create an invoke method which can invoke the command so now let's take a look at how we can use this to create a debug command in our controller that removes everyone on screen in our controller script let's create a new static debug command called kill all and a list of objects called command list and then in the awake method i'll assign the command and add it to the command list the action we're passing here is just a method in my controller script called kill all heroes when a hero is created their game object is added to the list so this method simply destroys any game object in that list when it's called and for all time's sake let's also add a debug command called rosebud that adds a thousand gold whenever it's called and then let's add that to our command list too so now we have two commands we can use which means that we need to tell our controller to read the text input and when it sees the command kill all or rosebud to invoke the right action from the list let's create a method here called handle input i've also added an on return method to the input actions so that when the return key is pressed and the console is showing it can actually trigger the handle input method and then to handle our input we need to actually pass the input string so let's iterate over our command list and get the debug command base object from each entry then let's check to see if the input string contains the command id and if it does we'll cast the command into a normal debug command and then invoke it essentially we're testing to see if the type of the object fits the cast here and then if it does we cast the object back to its original form and then invoke it back in unity we should now be able to type the commands into our debug window and have our actions invoked if i type in rosebud i gain some money and if i type in the kill all command all of the heroes get destroyed this is pretty much the base point from here it's easy enough to create all sorts of cheap keywords with specific actions involved for instance we could create another keyword called spawn that adds enough money and spawns a hero as long as we define it and add it to the list the handle input method will automatically process it currently our system is limited however because it requires a fixed keyword to perform a fixed function but some actions might need to be a bit more specific for instance what if we want to actively set how much gold we have let's take a look at how we can extend our system to handle that we can actually implement a version of the debug command to take in a generic type as part of the action like this so now we have an alternative class type that can take any type as a parameter to the action back in our controller let's create a new debug command with an int called set gold and define it like so final thing we need to do is set up our handle input method to be able to manage this type of command we can do that by creating a string array and splitting the array wherever there's a space now we can use the set gold command to define how much gold we have you should be able to see here that we're using generic typing to control what our string parameters should be passed as but we're using the command itself to handle what to do with those parameters it's an incredibly dynamic system that allows us to pass through all sorts of data from the console there's one final thing to do though because trust me as your game grows you probably won't remember all of these commands let's add a help option to display a list of all the available commands we'll create a new ball at the top of our controller here called show help then let's add a command for it this will simply set show help to true when called next in our gui method let's check to see if show help is true and then draw a box to print our help messages okay that's good now we need to actually print the messages in our script let's use a scroll view to fit and print each message so now whenever we use the help command all of our available commands are printed in our window alongside the descriptions of what they do valuable for players or members on the team who are looking for a quick hack without needing to dig into the code the only thing they probably need to know is to type help in the first place but i'll leave that one for you to figure out so that's about it for this video obviously there's a lot more you could do with the system to improve it for instance a nice ux improvement could be to add a search feature as the user starts typing to predict the command they're looking for and then printing suggestions to them but i think this will do for now it's a pretty flexible system with a lot of extendability which hopefully you found useful be sure to let me know your thoughts down below if you've enjoyed the video give it a thumbs up and if you're new to the channel hit that subscribe button additionally if you're interested in more videos like this why not take a look at one of the suggestions on screen now as always thank you very much for watching and i'll see you again next time
Info
Channel: Game Dev Guide
Views: 70,624
Rating: undefined out of 5
Keywords: unity, learn, games, gamedev, how to, making games in unity, tutorial, unity tutorial, learning unity, unity3d tutorial, programming, making games
Id: VzOEM-4A2OM
Channel Id: undefined
Length: 9min 45sec (585 seconds)
Published: Mon Sep 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.