«

»

Asserting the Basics – Part I

Although it’s not the hardest language to learn, Actionscript3 does provide a couple of traps that may lead an inexperienced Padawan astray. In a series of blog entries I would like to describe my own first steps on these new grounds, hoping that I can provide some aid for other programmers who also want to join the scripted side of the action.

Usually, the first thing I do in a new language is to ensure to have at least the following things at hand:

  • a possibility to print out text
  • an assert mechanism
  • and unit tests

These ingredients provide at least a minimum of safety while progressing through the uncharted territories of a new language and this is what I want to talk about. Let’s start on the first topic:

The beauty of text output

My very first project as a professional programmer was a racing game in C++ for Symbian OS 6 (an operating system on Nokia cellphones). Back then, the Symbian SDK consisted pretty much only of compilers and packagers, … and one funny little emulator. This bad boy did not have a text console to print to, and, even worse, there was no support for a real-time debugger. I pretty much felt like being blindfolded. It was then, when I realized how important a simple text console can be! Traumatized by this experience, I started our game engine by writing a Debug class with its first method being an output function for text messages.

Some may say: “Heck, why didn’t he just use trace() everywhere”. This has several reasons: For one, trace() simply does not work with non-debug Flash players and secondly I wanted to have one interface for all the debug messages. This way, implementing the possibility to switch off printing of messages is a piece of cake, and later adding a logger for all messages would be just the piece of cake next to the one I have just mentioned.

Now, this is what our very first method(s) looks like:

    <span style="color: blue;">private</span> <span style="color: blue;">static</span> var m_sendDebugInfo:Boolean = true;

    <span style="color: blue;">public</span> <span style="color: blue;">static</span> function EnableDebugInfo(_debugInfo: Boolean) : <span style="color: blue;">void</span>
    {
        m_sendDebugInfo = _debugInfo;
    }

    <span style="color: green;">/**
     * Sends a message to the console of flash develop
     * @param    _msg message to send
     */</span>
    <span style="color: blue;">public</span> <span style="color: blue;">static</span> function Out(_msg:String): <span style="color: blue;">void</span>
    {
        <span style="color: blue;">if</span> (m_sendDebugInfo == true)
        {
            fscommand(<span style="color: maroon;">"trace"</span>, _msg); <span style="color: green;">// for normal player</span>
            trace(_msg); <span style="color: green;">// for debug player</span>
        }
    }

Alright, this is pretty basic stuff, I know. Anyway, I did have to fiddle around with the text console to get it working in all players, especially with the non-debug player. So I thought, why not starting simple? I can always get more complicated later … and maybe this info can save some of somebody else’s time.

So, we just reached the end of the first entry of my series. Next time, I’m going to discuss the topic of assertions. So stay tuned for another episode of “Asserting the Basics”.

Happy coding,  Manuel

Popularity: 3% [?]

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">