Lab 8: Using Callbacks

In this lab, you are going to learn how to use a scheduler, timer, and a callback object to call a method at set intervals. Specifically, the method will be called every three seconds, and it will alternate between an Idle and a Walk animation, as shown in the image below. If you haven't done so, make sure to read Using Callbacks with the Untold Engine.


Pre-requisite: Make sure that you know how to create a Game Object class.

Declare the Callback and Timer objects

At its simplest term, a U4DEngine::U4DCallback schedules a method to be called at specific intervals. A U4DCallback is composed of a U4DEngine::U4DScheduler, which triggers at a defined interval. When the trigger occurs, the Callback object calls a particular method.

You are going to set up a callback and timer. The callback will call a method that alternates between an idle and a walking animation.

Let's declare the U4DCallback and the U4DTimer object.

Open up the "Astronaut.h" file and include the following files:

#include "U4DCallback.h"
#include "U4DTimer.h"

Next, within the Astronaut class, declare the U4DEngine::U4DCallback and the U4DEngine::U4DTimer object as shown in lines 1 & 2:

Notice that the declaration of the U4DCallback requires the Astronaut class name as its parameter.

Also, declare the alternateAnimation() method as shown in line 3.

//Astronaut inherits its behaviors from U4DGameObject
class Astronaut:public U4DEngine::U4DGameObject {
//Animation object
U4DEngine::U4DAnimation *walkAnimation;
//Current state of the astronaut
int state;
//Line 1. declare a callback
//Line 2. declare a timer
//Constructor of class
//Destructor of class
//Initialization method to load attribute data
bool init(const char* uModelName, const char* uBlenderFile);
//Method to update the game character
void update(double dt);
//Play Animation
void playAnimation();
//Stop Animation
void stopAnimation();
//set astronaut state
void setState(int uState);
//get astronaut state
int getState();
//change the astronaut state
void changeState(int uState);
//Line 3. alternate animations from idle to walk
void alternateAnimation();

Create the Callback and Timer object

Open the "" file. In the class constructor, create the U4DCallback and the U4DTimer object as shown in lines 4 & 5.

//Constructor Method
//Line 4. Create a callback object
//Line 5. Create a timer object using the callback as parameter
timer=new U4DEngine::U4DTimer(scheduler);

Note again that the U4DCallback requires the name of the class. In this case, "Astronaut."

The input argument for the U4DTimer object is the callback.

Next, scroll down to the init() method and schedule the U4DCallback to call the alternateAnimation() every three seconds, repeatedly (line 6).

//Init method
bool Astronaut::init(const char* uModelName, const char* uBlenderFile){
//Load the attributes into the game object
if (loadModel(uModelName, uBlenderFile)) {
//Enable shadows
//Create an Animation object and link it to the 3D model
walkAnimation=new U4DEngine::U4DAnimation(this);
//Load animation data into the animation object
if (loadAnimationToModel(walkAnimation, "walking", "astronautWalkAnim.u4d")) {
//If animation data was successfully loaded, you can set other parameters here. For now, we won't do this.
//Set the state of the astronaut to idle
//Send attributes to the GPU
//Line 6. Schedule the callback to call the alternateAnimation() method, every 3 seconds. Set this to repeat to true
scheduler->scheduleClassWithMethodAndDelay(this, &Astronaut::alternateAnimation, timer,3.0, true);
return true;
return false;

Define the alternateAnimation() method

Finally, define the alternateAnimation() method. This method is called every three seconds by the U4DCallback. The method checks the current state of the astronaut and changes the state from idle to walk and vice-versa.

void Astronaut::alternateAnimation(){
//Alternate the animations from idle to walk
if (getState()==idleState) {
}else if (getState()==walkState){

Run the project. You should see the astronaut alternating from being idle to walking.


Source Code:

If you want to double check your work, the source game files are found here Github page.