Using Callbacks

A Callback schedules a method to be called at specific intervals. A Callback is composed of a Scheduler, which triggers at a defined interval. When the trigger occurs, the Callback object calls a particular method within a class. The Untold Engine provides a U4DEngine::U4DCallback object which represents a Callback.

Declare a U4DCallback

The snippet below shows how to declare a U4DCallback object. Usually, when using a U4DCallback, you will also need to declare a U4DEngine::U4DTimer.

Notice that the declaration of the U4DCallback requires the class name as its parameter. In this case, the class name represents the class that contains the method which the U4DCallback will call.

//declare the callback with the class name    
U4DEngine::U4DCallback<MYCLASSNAME> *scheduler;

//declare the timer
U4DEngine::U4DTimer *timer;

Create a U4DCallback

The snippet below shows how to create the U4DCallback and U4DTimer. Notice again, that the U4DCallback requires the name of the class upon creation. The U4DTimer requires the callback as its parameter.

//Create the callback. Notice that you need to provide the name of the class
scheduler=new U4DEngine::U4DCallback<MYCLASSNAME>;

//create the timer
timer=new U4DEngine::U4DTimer(scheduler);

Schedule the U4DCallback

To schedule the U4DCallback requires the Method name to call, a pointer to the U4DTimer, a time interval, and whether or not to call the method once, or repeatedly.

//schedule the callback by providing the method to call, in this case it will call the method selfDestroy().
//The callback also requires a pointer to the U4DTimer, a time interval and whether or not to call the selfDestroy() method
//repeatedly

scheduler->scheduleClassWithMethodAndDelay(this, &MYCLASSNAME::selfDestroy, timer,2.0, true);

Delete the U4DCallback

Finally, in the class destructor, make sure to delete the U4DCallback and U4DTimer as follows.

//In the class destructor,  make sure to delete the U4DCallback and U4DTimer as follows. 
//Make sure that before deleting the scheduler and timer, to first unsubscribe the timer.

scheduler->unScheduleTimer(timer);
delete scheduler;    
delete timer;

Last updated