• Advertisement
  • entries
    740
  • comments
    957
  • views
    730841

Scheduling Goodness

Sign in to follow this  

70 views

Friday, Feb 23

So I finally got my damn schedule manager done - after two days *grump*. Mainly because I didn't have much time to work on it, but also because of stupid bugs that were a bitch to uncover. I finally admitted to myself that I needed a schedule manager as the number of schedule() calls continued to pile up from various places in the game. When I go to pause the game, the main pause function was getting cluttered with having to cancel and store the remaining time of every scheduled event in the game. Ugh. So here's what I have to manage it all


//---------------------------------------------------------------------------------------------
// Project : Blitz Blox
// File : .\gameScripts\schedule.cs
// Copyright : Blade Edge Software (C) 2006 - 2007
// Author : Drew Sikora
//---------------------------------------------------------------------------------------------

new ScriptObject(Schedule);

//---------------------------------------------------------------------------------------------
// Name : addObject
// Class : Schedule
//
// Arguments : repeat - whether the scheduled call repeats
// : object - the object this function belongs to
// : time - the time until the appointment is executed
// : funcName - the name of the function we will be calling
// : [argumentList] - the list of arguments to apply to ths function call
//
// Description : adds a new appointment to the schedule manager and stores the handle and appointment
// : data for later reference
//---------------------------------------------------------------------------------------------
function Schedule::addObject(%this, %repeat, %object, %time, %funcName, %argumentList)
{
if (%this.appointments $= "")
%this.appointments = new ScriptGroup();

%appointment = new ScriptObject()
{
id = %this.id++;
object = %object;
repeat = %repeat;
time = %time;
funcName = %funcName;
arguments = %argumentList;
handle = %this.schedule(%time, "call", %this.id);
};

// store appointment and return handle to caller
%this.appointments.add(%appointment);
return %appointment.handle;

} // end addObject


//---------------------------------------------------------------------------------------------
// Name : addFunction
// Class : Schedule
//
// Description : same as addObject, but for regular functions
//---------------------------------------------------------------------------------------------
//function Schedule::addFunction(%this, %repeat, %time, %funcName, %argumentList)
//{
// %this.addObject(%repeat, "", %time, %funcName, %argumentList);
//
//} // end addFunction


//---------------------------------------------------------------------------------------------
// Name : remove
// Class : Schedule
//
// Arguments : [apptHandle] - the handle to the appointment we want to cancel/remove
//
// Description : removes an appointment from the schedule manager and cancels the scheduled event.
// : if apptHandle is unused, all appointments are deleted
//---------------------------------------------------------------------------------------------
function Schedule::remove(%this, %apptHandle)
{
// find the handle, cancel and remove it
for (%appt = 0; %appt < %this.appointments.getCount(); %appt++)
{
if ((%apptHandle !$= "") && (%this.appointments.getObject(%appt).handle == %apptHandle))
{
cancel(%this.appointments.getObject(%appt).handle);
%this.appointments.remove(%this.appointments.getObject(%appt));
break;
}
else if (%apptHandle $= "")
cancel(%this.appointments.getObject(%appt).handle);
}

if (%apptHandle $= "")
%this.appointments.delete();

} // end remove


//---------------------------------------------------------------------------------------------
// Name : pause
// Class : Schedule
//
// Arguments : isPaused - whether to pause (true) or unpause (false) all Schedulerd events
//
// Description : Cancels all scheduled events and stores their remaining time for reactivation
// : when game is unpaused
//---------------------------------------------------------------------------------------------
function Schedule::pause(%this, %isPaused)
{
if (%isPaused)
{
for (%appt = 0; %appt < %this.appointments.getCount(); %appt++)
{
%this.appointments.getObject(%appt).timeLeft = getEventTimeLeft(%this.appointments.getObject(%appt).handle);
cancel(%this.appointments.getObject(%appt).handle);
}
}
else
{
for (%appt = 0; %appt < %this.appointments.getCount(); %appt++)
{
%this.appointments.getObject(%appt).handle = %this.schedule(%this.appointments.getObject(%appt).timeLeft, "call", %this.appointments.getObject(%appt).id);
}
}

} // end pause


//---------------------------------------------------------------------------------------------
// Name : call
// Class : Schedule
//
// Arguments : id - the unique id assigned to this appointment
//
// Description : is called to carry out scheduled function call but also to loop the event or
// : remove it from our appointment group
//---------------------------------------------------------------------------------------------
function Schedule::call(%this, %id)
{
// find the appointment
for (%appt = 0; %appt < %this.appointments.getCount(); %appt++)
{
if (%this.appointments.getObject(%appt).id == %id)
{
// check for any arguments
if (%this.appointments.getObject(%appt).arguments !$= "")
{
// first argument has no comma or space
%args = getWord(%this.appointments.getObject(%appt).arguments, 0);

for(%arg = 1; %arg < getWordCount(%this.appointments.getObject(%appt).arguments); %arg++)
{
%args = %args @ "," SPC getWord(%this.appointments.getObject(%appt).arguments, %arg);
}
}

// write out the function call and execute
eval(%this.appointments.getObject(%appt).object @ "." @ %this.appointments.getObject(%appt).funcName @ "(" @ %args @ ");");

// renew appointment or delete?
if (%this.appointments.getObject(%appt).repeat)
%this.appointments.getObject(%appt).handle = %this.schedule(%this.appointments.getObject(%appt).time, "call", %this.appointments.getObject(%appt).id);
else
%this.appointments.remove(%this.appointments.getObject(%appt));

break;
}
}

} // end call


Weighs in at 160 lines (with spaces and comments), so I'm pretty happy with it. Here's an example of it in use


Schedule.addObject(false, %this, 1000, "takeSpecialAction", %type SPC %position);

this would be the same as saying


%this.schedule(1000, "takeSpecialAction", %type, %position);

In addition, you can also set the first argument to 'true' and the schedule manager will automatically loop that schedule call for you until you remove it.

Oh and just so I can reference it later (and for anyone else interested), this is a bad way to define a ScriptObject


new ScriptObject(Schedule) { appointments = new ScriptGroup(); };

I had that at the top of the file and it compiled fine but for some reason TGB then refused to recognize Schedule as a valid object. It took me a while to think that having it defined that way was causing problems. I have no clue why but TGB doesn't seem to like it *shrug*

So yea, I converted all my schedule() calls to use the manager and now all I have to do in the main game pause function is say


Schedule.pause($isGamePaused);

And all the scheduled events are paused until the game is resumed. Whoo hoo!

Anyways besides that I added two more special block actions, the Drag Block, which slows down the rate at which your opponent's blocks fall, and the Speed Block, which increases the rate at which your own blocks fall. Now I just have to fix this collision layering bug so that faster blocks can pass slower blocks while dropping towards the same spot without bumping each other until they reach the bottom - at which the block lowest ends up beneath the higher-up block.

I'd add more special blocks but I have to get up early to coach. Blargh. I'll finish em up tomorrow.

Feel free to use my source code to help build your own schedule manager, I only ask for credit someplace.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement