Thursday, August 26, 2010

The Stupidity of IT

This is an off-topic post about something that I witnessed not too long ago at my job.  

I happened to be walking by one of our conference rooms and noticed something really peculiar. There were about 20 overdressed guys in suits, all sitting around two tables that they had joined together.  The projector was displaying something on the screen, yet all of the conference room lights were on (insert "here's your sign").  Everyone was shaking their heads in what seemed like a synchronized bow of approval, gawking at what was on the screen in front of them.


Given this mass reaction, I half expected to see something that would be mind-blowing, or a cure for some disease...I even would've been OK to see some new software that a company was soullessly trying to sell to us for thousands of dollars.  But, alas, I saw one of the dumbest things I have ever seen in my entire life - please look below:


No, I didn't leave out any additional text that might make this grid make any more sense.  It was really this stupid.  I don't know what they could've been talking about, but this, regardless of the conversation, is just plain asinine.  

Do you ever question your job?  

I did that day, and I have been laughing (and crying) about this ever since.

Wednesday, August 11, 2010

Level Loader Tutorial in AS3 using Function Pointers

In a game you often come across the need to change levels.  In my case, while making a mini-golf game, I obviously have the need to go from hole to hole as the player makes their shots.  Since I load my level content through a method call, such as loadLevelOne(), I needed a way to be able to make those calls without explicitly knowing what order to call them during runtime.


AS3 has a very easy way to do this that I think is worthy of a quick tutorial.  Since a function that you declare in a class is of type function, you can pass them around like objects or, as in my case, store them in an array that you can cycle through.  You only need a couple of variables and a few utility methods to do this.


Let's say you have two levels to load for your game and you have defined them somewhere in your code, as follows:


private function loadLevelOne():void
{
...
}


private function loadLevelTwo():void
{
...
}


As members of your class, you can make the following variables:


// Levels array that stores Functions
private var _levels:Array;

// A variable to keep track of where you are in the array
private var _levelIndex:int; 


In the constructor of your class, or in a separate method, make sure to initialize your variables, and then "load" the functions in to the array:


// Initialize the levels array
_levels = [];


// Set the level index
_levelIndex = 0;


// Load the method calls into the array
_levels.push(loadLevelOne);
_levels.push(loadLevelTwo);


You could also do this more explicitly if you wanted by creating a variable of type Function, setting it to the name of the function, and pushing that into the array, like this:


// Function pointer variable
var functionPointer:Function;


// Load levels 
functionPointer = loadLevelOne;
_levels.push(functionPointer);


If you didn't know the exact order you wanted to load your levels, or if your situation for loading levels occurred in a more dynamic nature than in my mini-golf game, then the preceding example may be a better fit for you.  In order to get through the levels and actually call them, I created the following utility methods:



// Runs the current level
private function runLevel():void
{
   // Check to make sure that your levels array has data
   if (_levels.length > 0)
   {
      // Get the function pointer out of the array
  var currentLevel:Function = _levels[_levelCounter];
  
      // Call the method
  currentLevel.call();
  }
}

// Switches to the next level
private function switchLevel():void
{
// Check that you are still in bounds if you switch levels
  if (++_levelCounter < _levels.length)
  {
      // Get the function pointer
  var currentLevel:Function = _levels[_levelCounter];


      // Call the method
  currentLevel.call();
  }
}

// Retry the current level
private function retryLevel():void
{
// Get the current level
  var currentLevel:Function = _levels[_levelCounter];


   // Recall the method
  currentLevel.call();
}


So, hopefully this is of some use to you out there if you ever needed to use a structure like this. There are a lot of other uses for this as well, for instance, if you had an RPG like Diablo 2 or World of Warcraft.  In those games, your character often has spells (good or bad) that affect the player over time. You could set this up as an array of function pointers that get cycled through per frame, with each function handling the different spells that are cast on you.  Of course, that may be a horrible way of handling it, but at the very least, it is some food for thought.

See you next time!