Game Development Community

Individual, and always active, functions

by D B · in Torque Game Engine · 05/19/2006 (4:15 pm) · 2 replies

I was wondering if there is a way to have it so each PlayerData has its own individual function that is always running automatically.

To be more specific, I want to have the characters blink using IFLs, and be able to set blinking to on or off, as well as have a random blinking interval. I'm not asking for help about how to apply those functions; I'm just wondering if anyone could tell me if there's a way to have the functions work for individual characters automatically.

Thank you.

#1
05/21/2006 (5:10 am)
You can use schedule() to re-schedule the same function again on the same object to be called after a random number of milliseconds.

There are two ways to use schedule. First as a console function:

schedule(3000, 0, "echo", "hello world");
Type that in the console, and you'll get a "hello world" after 3 seconds.

The first paramter is the time, the 2nd is the object. If you leave this 0, the function will be called as a console function. If you pass the name or the ID of some object, the function will be called as a method in that object. So, if you wanted to plant a time bomb in a player, you could do:

schedule(3000, player3, "delete");
That'll call delete on the object named player3 after 3 seconds.

There is also an alternate way to schedule a call on a certain object:
player3.schedule(3000, "delete");

You can also store the schedule in a variable, which allows you to stop it later:
player3.timeBomb = player3.schedule(3000, "delete");
And you can cancel the schedule this way:
cancel(player3.timeBomb);

Now, to make a function repeat itself over and over, just make it re-schedule itself and pass the same parameters on the schedule. By using getRandom() you can make it repeat in irregular intervals of time:

function Player::blink(%this)
{
   %time = getRandom(1000, 5000);

   cancel(%this.blinkSchedule);
   %this.blinkSchedule = %this.schedule(%time, "blink");

   //Code to do the blink animation goes here...
}

Note that I used cancel() before issuing a new schedule. This is a safekeeping measure, since if the blink() method is called from somewhere else other than the schedule, it'll create a new schedule before the previous one has fired, creating another "schedule thread", essentially making the funciton get called twice as much.
#2
05/24/2006 (2:54 pm)
Thanks, Manoel! Very clever! :D

Also, thanks for all of the details you've provided, as well. The examples were very nice.