Console class for AS3 or Haxe

I made a very usefull console class. There is an AS3 version and a Haxe + NME version. Is simple to use and has interesting features you can’t find in other console tools.

The visual representation is similar to consoles that you find in games, such as Quake.

Features:

  • Write debug messages and errors to the console. Those messages will be easy to find, and nicely hidden away from regular users. This can speed up the development process.
  • Change the state of your application at runtime, by adding commands to the console.
  • Add “live fields” when you want to display information with a very high time frequency without adding a new line of text on every update, for example when you want to display a number on every frame.
  • Measure the execution time of a piece of code very easily. Results get displayed on the console as a “live field” or as a new text line.
  • Browse the history of previous commands using “up” and “down” cursor keys (like on the OS consoles). The history remains even if you close your application and compile a new version.
  • When you trace an Array, Object/Hash and XML you can see the content, for example: If you write Console.trace(myArray), or trace(myArray) on Haxe, instead of showing “array” it will display the content of the array.
  • All the methods of the class are static, so it’s easy and clean to acess the console anywhere on your code.
    In Haxe the console overrides the native trace method, so you don’t need to import the Console class everywhere if you only need to use the trace.

There are other small features not listed here and new ones comming, you can check the updaets and fixes list here.

How to use it:

  • Starting:

Haxe:

Console.create();

AS3:

Console.create(stage);

By default “Ctrl + 1” will dislpay the console, you can change that with the parameters of the create() method. If you want to show it by default call show() after the creation:

Console.create();
Console.show();

  • Writing something:

Haxe:

trace(“Hello planet!”);

AS3:

Console.trace(“Hello planet!”);

  • Creating a command:

Console.addCommand(“sayhello”, callback);

Example:

Console.addCommand("sayhello", function(parameters:String):void
{
    trace("hello! parameters of the command: "+ parameters);
});

The “patameters” string contains all the user writes after the command, for example if the user write “sayhello hey ho” the string will be “hey ho”. If you write only “sayhello” the string will be null. Use that string to change your application at runtime.

  • Creating a live field:

Console.setLiveDataField(“my data”, myData);

Example:

var frameNum:int = 0;
stage.addEventListener(Event.ENTER_FRAME, function(e:Event):void
{
    Console.setLiveDataField("frames rendered", frameNum);
    frameNum++;
});

The first parameter (“my data” in the first example) works as an ID of the live data field.  Creates a live data field if never was created and updates if already exists.
You can create as many live fields as yo want, until you get aut of screen space.

  • Measuring code execution time:

Console.measureTime(“my loop time”);

Example:

Console.measureTime("my loop time");
for (element in myObject)
{
   var createThis:String = "hello";
   doSomthingElese(element);
}
Console.measureTime("my loop time");

Note the measureTime method is called 2 times, the first time starts measuring time and the second time stops and displays the time elapsed.
The parameter works as an ID, you get the results when you set the same string in both calls.
You can repeat this process with the same ID or a different one on the same runtime.

Download:

IMPORTANT FOR HAXE: Version 3.3.2 (or superior) of NME is required, previous versions doesn’t work properly on C++ platform. HTML5 platform is not supported for the moment due to the NME/jeash problem with textField.maxScrollV on HTML5.

This is part of a pack of tools I’m publishing called “fermmmtools”, you can find this class on the “debug” package:

https://code.google.com/p/fermmmtools/downloads/list

Direct links:

As3 version
Haxe version


Advertisements