Tips 'n Tricks - Debugging LibGDX & Artemis in Eclipse

So, debugging’s always fun. It’s the part of programming where we either have to acknowledge we’re fallible humans after all, or we can rage on some unknown piece of code written by someone else which “just doesn’t do what it should, gosh-darnit”.

There are several ways you can make debugging a more friendly experience, and in this article I’ll spend a bit of time on two techniques you can use in Eclipse: Detail Formatters & Logical Structures.

Detail Formatters essentially are snippets of code you can supply to Eclipse which will be used to create a String in the value portion of your Debug view of a certain type of class. If you do not supply one, the value column will be populated by the toString() of the type.

Logical Structures are snippets of code you can use to alter how the Debug view displays certain types. It is primarily used to hide unnecessary aspects of a type and drill down to the important aspects and can be enabled in the Debug View by pressing “Show Logical Structure”. In the below example I opted to display a Map structure as an array containing its entries:

Debug View

Logical Structure

After enabling the Logical Structure the Map type is displayed concisely, showing only those aspects we’re interested in.

These snippets of code can be entered in the Java Preferences section of the Debug View, which you can find hidden under the dropdown icon in the Debug View:

Preferences

I end this article with a list of different Detail Formatters & Logical Structures I use, so you can easily import them:

Detail Formatters

Pro Tip: In the “Preferences” pane, for “Show the variable details (‘toString()’ value)” options, select “As the label for variables with detail formatters”. Then your custom toString() will used in the Value column for the debug view. This helps especially with compound datastructures such as a Map or Tuple.

*Map.Entry

return String.format("\"%s\" - %s", key, value);

Artemis

Entity

String output = "Entity ID: "+this.id + "\r

"; Bag b = this.getComponents(new Bag()); for (int i = 0; i < b.size(); i++) { output += "\t" + b.get(i).getClass().getSimpleName() + " :: "+b.get(i).toString() + "\r

"; } return output;

Logical Structures

Now for the definition of the logical structures we can use which are specific to LibGDX & Artemis.

LibGDX

*Array

return items;

Bag

We don’t just simply return data so we don’t clutter up the debug view with a lot of empty cells containing null.

Object[] output = new Object[size()]; System.arraycopy(data,0, output,0,size()); return output;

*Map

Array copy = new Array(); for (ObjectMap.Entry e : entries()) { ObjectMap.Entry entry = new ObjectMap.Entry(); entry.key = e.key; entry.value = e.value; copy.add(entry); } return copy.toArray(ObjectMap.Entry.class);

The construction of the Entry instances is because LibGDX re-uses the Entry given back by Entries.

*Map.Entry

key = return getKey(); value = return getValue();

Artemis

Entity

Entity can best be viewed as a logical structure with multiple variables:

ID

getID();

Components

getComponents(new Bag());

Registered Systems

Bag<EntitySystem> systems = new Bag<EntitySystem>(); BitSet inSystems = this.getSystemBits(); for(EntitySystem system : world.getSystems()){ if(system.getActives().contains(this)) systems.add(system); } return systems;

Results

Without

With Logical Structures

The end result is pretty self explanatory; with the normal Entity class you can’t even see which components it has or in which systems it is registered.

If you have any tips or code pieces of your own; please feel free to share them in the comments!