H3XED

ActionScript 3 - Using References to Improve Game Performance

Jun 24, 2011   Programming   Nick Vogt   Comments
Please note that this post is over a year old and may contain outdated information.
I couldn't think of an easy way to explain references (or pointers), so I'm going to jump right in with examples.

Create two array variables, but only assign values to one. Then assign the second one to the first:

var array1:Array = ['hello', 'world'];
var array2:Array;

array2 = array1;

What you've just done is created two array variables, array1 and array2, that both reference (point to) the same array. You can use either variable to access and edit the same array. Here is a test:

array2[1] = 'universe';
trace(array1[1]); // Output is 'universe' and not 'world'


array1 and array2 are references to the actual array object that exists in memory.

Let's take the example one step further. Create another array and assign array1 to it:

var array3:Array = ['asdf', 23];

array1 = array3;

What you've done here is made array1 point to the second array you created, and no longer point to its original array. The first array can still be accessed through array2 variable though. Test it out:

trace(array1[0]); // Output is 'asdf' and not 'hello'
trace(array2); // Output is 'hello, universe'


In ActionScript 3, Arrays, Objects, Sprites, and most other variable types are always references. The exceptions are primitive types, which are the int, Number, Boolean, and String types.

If you assign a primitive variable to another, you are creating a copy of the actual data in memory, and not creating a reference. For example:

var int1:int = 5;
var int2:int;
int2 = int1;
int2 = 3;
trace(int1); // Output is still 5
trace(int2); // Output is 3


Now that you have a brief rundown on references, you can leverage them to improve the performance of your game.

Take a look at this example:

var mygame:Object = {};
mygame.badguys = [new BadGuy(), new BadGuy(), new BadGuy()];

for(i = 0; i < 3; i ++)
{
   mygame.badguys[i].move();
}

Assuming you had a custom badguy class (and you had instantiated the i variable), this example would loop through all of the badguy classes in the badguys array of the mygame object and run their move method.

The problem with the above example is that for every iteration of the loop, the program has to go through the mygame object first before it can access the badguys array. So why not just create a reference directly to the badguys array?

var mygame:Object = {};
mygame.badguys = [new BadGuy(), new BadGuy(), new BadGuy()];
var badguys_ref:Array = mygame.badguys;

for(i = 0; i < 3; i ++)
{
   badguys_ref[i].move();
}

Now the program can access the badguys array directly. This is because both "mygame.badguys" and "badguys_ref" point to the same array and access and edit the same information.

This is the key to improving the performance of your game. Avoid going through multiple objects, arrays, or classes to gain access to something.

You can take the above example one step further:

var mygame:Object = {};
mygame.badguys = [new BadGuy(), new BadGuy(), new BadGuy()];
var badguys_ref:Array = mygame.badguys;
var badguy:BadGuy;

for(i = 0; i < 3; i ++)
{
   badguy = badguys_ref[i];
   badguy.move();
   badguy.attack();
   badguy.flee();
}

You can create reference variables to your own custom classes as well. Each iteration of the loop, it assigns "badguy" to the BadGuy that is being worked with that iteration.
Share This Post
Twitter

Comments (0)

Share This Post
Twitter
H3XED © Nick Vogt   RSS   Policies   Twitter