Game Development Community

FIFO-Queue in Torque Script

by Stefan Beffy Moises · 06/07/2007 (11:11 am) · 2 comments

Download Code File

//*********************************************************
// A simple FIFO-Queue object by Stefan "Beffy" Moises
//*********************************************************
// use it like this:
//      $Queue = new ScriptObject(Queue) {};
//      MissionCleanup.add($Queue);
//      $Queue.init(); // you need that
//      $Queue.pushBack(%someObject); // store any object
//      $Queue.pushBack(%anotherObject); // and another one
//      $Queue.debug(); // print out objects in queue
//      %someObject2 = $Queue.popFront(); // should give you %someObject back from the queue
//      %anotherObject2 = $Queue.popFront(); // should give you %anotherObject back from the queue
//*********************************************************

//*********************************************************
// should always call init()!
// sets default size'n stuff
//*********************************************************
function Queue::init(%this)
{
   echo("Queue::init()");
   %this.maxSize = 64;
   %this.array = "";
   %this.makeEmpty();
}

//*********************************************************
// this is called by init(), so dont't bother :)
//*********************************************************
function Queue::makeEmpty(%this)
{
   %this.currentSize = 0;
   %this.front = 0;
   %this.back = 0;
}

//*********************************************************
// gives you the current max object count you can store in 
// the queue
//*********************************************************
function Queue::getMaxSize(%this)
{
   return %this.maxSize;
}
//*********************************************************
// you can set a new max size for the queue here
//*********************************************************
function Queue::setMaxSize(%this, %size)
{
   %this.maxSize = %size;
}

//*********************************************************
// always interesting... the current number of objects stored
//*********************************************************
function Queue::getCurrentSize(%this)
{
   return %this.currentSize;
}

//*********************************************************
// is the queue empty?
//*********************************************************
function Queue::isEmpty(%this)
{
   return %this.currentSize == 0;
}

//*********************************************************
// is the queue full?
//*********************************************************
function Queue::isFull(%this)
{
   return %this.currentSize == %this.maxSize;
}

//*********************************************************
// ok, get and remove the first element in the queue
//*********************************************************
function Queue::popFront(%this)
{
   if(!%this.isEmpty())
   {
      %this.currentSize--;
      %frontItem = %this.array[%this.front++];
      if(%this.front > %this.maxSize)
         %this.front = 0;
      return %frontItem;
   }
   return -1;
}

//*********************************************************
// ok, add an object to the queue if not full already
//*********************************************************
function Queue::pushBack(%this, %obj)
{
   if(!%this.isFull())
   {
      %this.array[%this.back++] = %obj;
      if(%this.back > %this.maxSize)
         %this.back = 0;
      %this.currentSize++;  
   }
   else
   {
      error("Queue is full! Max size:" SPC %this.maxSize);
   }
}


//*********************************************************
// print out all the current object ids in the queue
//*********************************************************
function Queue::debug(%this)
{
   %max = %this.getCurrentSize();
   for(%i=0; %i < %max; %i++)
   {
      %elem = %this.getByIndex(%i);
      echo("***QUEUE ELEMENT AT:" SPC %i SPC "IS:" SPC %elem);
   }
}

//*********************************************************
// ok, check the first element in the queue
// this is mainly for debug purposes, you usually want to
// call popFront() to also remove the element!!
//*********************************************************
function Queue::getFront(%this)
{
   if(!%this.isEmpty( ))
      return %this.array[%this.front];
   return -1;
}

//*********************************************************
// this isn't really queue-specific either, but if you are using
// it more like an array, you probably find this handy
// gets the element at a specific index [0...currentSize] without removing anything
//*********************************************************
function Queue::getByIndex(%this, %idx)
{
   if(!%this.isEmpty())
   {
      // elements internally start at 1, not 0
      %internalIdx = %this.front + %idx + 1;
      %item = %this.array[%internalIdx];
      if(isObject(%item))
      {
         return %item;
      }
   }
   return -1;
}

#1
06/08/2007 (4:01 pm)
Nice. I'll definitely make use of this.
#2
10/17/2009 (7:20 pm)
thanks, very useful..