Skip to content
damios edited this page Mar 30, 2020 · 7 revisions

LibGdx In-Game Console Tutorial

This guide will concisely explain how to use the Console so that you can get right into the swift development environment that the Console provides.

Notes

  • This guide assumes you already know how to make a basic game with either Scene2D or basic Gdx inputs.

Creating the Console

Creating a console for your game could not get any simpler! All you have to do is instantiate a Console object:

Console console = new GUIConsole();

Then add a CommandExecutor to it:

console.setCommandExecutor(new MyCommandExecutor());.

Make sure to remember to draw the console in your draw phase!

console.draw().

You should draw the console last to ensure that it is always visible. Also, the console does not need to be drawn within your game's batch beginning and end; it uses its own batch.

Creating the CommandExecutor

The CommandExecutor is the class that contains all the methods that you will be able to use in the Console's window. It is also mandatory to add one to the console otherwise it will not function!

To create the CommandExecutor simply create a new class (it can be an inner class, if you want, but it cannot be private) that extends the base class, CommandExecutor:

public class MyCommandExecutor extends CommandExecutor

Then, all you have to do is start creating public methods within that class! Remember to add it to your console!

Please review Default Methods.

Input Detection

If your game uses Stages, then just look at Input Processing.

Otherwise, what you will have to do is check whether or not any touches that occurred hit the console and only have your game react if they did not. This can be easily done by calling:

console.hitsConsole(screenX, screenY);

This method returns a boolean. Make sure to give the console the screen coordinates and not any adjusted coordinates.

Using the Console

Using the console is almost as simple as creating it! Built-in Javadoc descriptions should be sufficient for understanding the Console's methods, but here are some quick demonstrations.

Changing the Key

To change the key that opens the Console call:

console.setDisplayKeyID(Input.Keys.someKey);

You can also get the current key ID with:

console.getDisplayKeyID();

Disabling the Console

Say you have a settings menu in your game where you would like to have a setting the enables/disables the console or you just want to disabled the console when distributing your application to clients. To do so just call:

console.setDisabled(true); //or false, of course

You can also check if the console is currently disabled with:

console.isDisabled();

Logging

Whether you want to log information for live-debugging purposes or to confirm that a console method executed correctly, logging information to the console is very easy. You can use two methods to log to the console:

  1. console.log("info");
  2. console.log("info", Console.LogLevel.DEFAULT)

The former of the two simply calls the latter with the default LogLevel. LogLevels only serve as an indicator of the type of log an entry is. The available LogLevels are:

  1. DEFAULT - Prints in white. No indicator in a logs file.
  2. ERROR - Prints in red. Error: indicator in a logs file.
  3. SUCCESS - Prints in green. Success! indicator in a logs file.
  4. COMMAND - This should not be used. It is only intended to be used by the console itself.

More information can be found in their Javadoc descriptions.

You can also print the logs to a file either from within the console itself or from within your code:

console.printLogToFile(localPath) //you can also pass a FileHandle object

Disposing

Make sure to dispose of the console when you are finished using it (usually when the app is closed). To do so, call:

console.dispose();

Clone this wiki locally