by date
TGB Schedule Manager Code
TGB Schedule Manager Code
| Name: | Drew -Gaiiden- Sikora | ![]() |
|---|---|---|
| Date Posted: | Feb 24, 2007 | |
| Rating: | 3.0 out of 5 | |
| Public: | YES | |
| Comments: | YES | |
| RSS Feed: | or Subscribe with . | |
| Profile Page: | View profile page for Drew -Gaiiden- Sikora |
Blog post
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
note - See the updated code for this in my next entry
Weighs in at 160 lines (with spaces and comments), so I'm pretty happy with it. Here's an example of it in use
this would be the same as saying
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
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
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.
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
note - See the updated code for this in my next entry
//---------------------------------------------------------------------------------------------
// 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.
Recent Blog Posts
| List: | 12/03/08 - GDNet: Weekly Sitrep 12/03/08 - GDNet: Tales from Journal Land 11/27/08 - GDNet: Weekly Sitrep 11/21/08 - GDNet: Tales from Journal Land 11/17/08 - GDNet: Tales from Journal Land 11/07/08 - GDNet: Tales from Journal Land 11/06/08 - GDNet: Weekly Sitrep 10/31/08 - GDNet: Tales from Journal Land |
|---|
Submit your own resources!| bank (Feb 24, 2007 at 13:15 GMT) |
new ScriptObject(Schedule) { appointments = new ScriptGroup(); };doesn't work, as you have two constructions one in one, and this will not work. It's not only TGB, it's Torque.
If you need to construct two objects, do it one by one.
Personally, I prefer to do like this:
// construct the scheduler
if (!isObject(Schedule) new ScriptObject(Schedule);
function Schedule::onAdd(%this)
{
%this.appointments = new ScriptGroup();
}
function Schedule::onRemove(%this)
{
%this.appointments.delete();
}
This kind of code allows me to perform to exec the same script over and over again, in case I do changes and need to apply it without restarting the Torque.
For every single ScriptObject I have set onAdd/onRemove so my scripts handles everything.
Overall - nice code, I like it :) Good luck
| Drew -Gaiiden- Sikora (Feb 24, 2007 at 17:58 GMT) |
| Andy Hawkins (Feb 24, 2007 at 21:41 GMT) |
You must be a member and be logged in to either append comments or rate this resource.



3.0 out of 5


