Archive for November, 2009

Faster Javascript loops

Consider this very standard construct:

//assume arr is an array
for (var i = 0, i < arr.length; i++)
{
...
}

This is extemely inefficient because the "length" property needs to be looked up on each pass through the loop. One easy fix is to first define a variable for the length, like this:

var l = arr.length;
for (var i = 0; i < l; i++)

This is an improvement, but this particular loop is so simple that it could be further simplified into a while loop, which will run much faster again. Since we don't really care whether we go through the array from its beginning to end or vice versa:

var i = arr.length;
while (i--)
{
...
}

This little tip applies to Javascript in general but will be highly effective with DXStudio scripting. Also, I cannot take credit for this discovery. I read it on Sitepoint.

Leave a Comment

Debug Print [with no lingering effects]

DXStudio has implemented Javascript as its control language. Of course there are a few departures from the pure ECMA Javascript implementation. Many are to directly control the 2D and 3D assets in your project. A few were added to aid in development.

The “print()” command is extremely useful. If you have properly enabled it in your Document Properties (see image), the ‘console’ can display useful debug information.

DX Document Properties

DX Document Properties


In the scripting code you can reveal the value of variables (and objects) by sending output to the console with the “print()” command. The trouble comes when you wish to release your work to the public. Even if you are testing or sharing with a small group of fellow developers, numerous print commands can produce a very annoying result by displaying lots of superfluous information. The console obscures part of the display, often times when you are trying to SEE something important.
Removing, or adding remarks to disable, the many “print()” commands is tedious and troublesome. Remembering where they all exist can be a tremendous task in itself on a large project that includes many scenes and/or objects with script in them.
By substituting the internal “print()” command in DXStudio with a simple function, you can toggle the debug display by modifying ONE value in ONE place in your code! Here’s how to do it.
In the Document script add the lines shown. The DEBUG constant is the key to this. Changing it will affect all instances of a console print command at once.

Document Script commands

Document Script commands

Then anywhere in your scripts that you wish to output DEBUG information to the console display.

Example of DEBUG output - in place of a simple 'print()' command

Example of DEBUG output - in place of a simple 'print()' command

But, whenever you wish to suppress the DEBUG output, simply change the value of the constant “DEBUG” to false!

There are many enhancements that could be made to this simple idea. Even an independent class could be created that adds some other useful “debug” operations. I will leave those enhancements up to the creative and adventuresome reader. But PLEASE send me your favorite changes and they will appear here in an update to this post.

Leave a Comment