Lab 7: Create a Game Object in Xcode

Our goal is to create a subclass object of U4DEngine::U4DGameObject. You will learn how to inherit the properties of a U4DEngine::U4DGameObject, how to initialize the object and how to instantiate it.

You will represent the Astronaut character shown below, as a subclass of U4DGameObject:

image

Create a New Class in Xcode

Step 1. Create New File

Right-click on the Game folder and click on New File.

image1

Step 2. Select C++ File

On the new window, click on C++ File.

image2

Step 3. Name the Class

Give the following name to the file: Astronaut. Make sure to check the box: Also create a header file.

image3

Step 4. Save the file

Make sure to save the file in the Game folder. Under Targets, make sure to check the following boxes:

  • UntoldEngine
  • Untold4D iOS
  • Untold4D macOS
image4

Step 4. Change the file extensions

Since we are using C++ and not Objective-C, we need to change the files extensions for both the header and source files.

Click on the Astronaut.hpp file and change the extension to Astronaut.h. Next, click on the Astronaut.cpp and change the extension to Astronaut.mm.

Both header and source files should look as shown below:

image6

Next, click on Astronaut.mm, and change the following code:

#include "Astronaut.hpp"

to

#include "Astronaut.h"

Declaring the Astronaut Class

Open up the Astronaut.h file.

The first step is to include the U4DGameObject parent class, as shown in the line below.

#include "U4DGameObject.h"

Next, declare the Astronaut class as shown below:

//Line 1. Astronaut inherits its behaviors from U4DGameObject
class Astronaut:public U4DEngine::U4DGameObject {
private:
public:
//Line 2. Constructor of class
Astronaut();
//Line 3. Destructor of class
~Astronaut();
}

The Astronaut class is declared as a subclass of the U4DGameObject class (line 1). Recall that U4DGameObject provides its subclasses rendering and dynamic behaviors.

Lines 2 and 3 shows the constructor and destructor of the class, respectively.

As mentioned earlier, the Astronaut class must contain an init() method, as shown below in line 4. The init() method will be responsible for loading attributes into the game object. The class must also declare an update() method responsible for updating the character's properties (line 5).

//Line 4. Initialization method to load attribute data
bool init(const char* uModelName, const char* uBlenderFile);
//Line 5. Method to update the game character
void update(double dt);

Complete Code Snippet

Astronaut Class Declaration

The Astronaut class declaration should look as follows:

#include "U4DGameObject.h"
//Line 1. Astronaut inherits its behaviors from U4DGameObject
class Astronaut:public U4DEngine::U4DGameObject {
private:
public:
//Line 2. Constructor of class
Astronaut();
//Line 3. Destructor of class
~Astronaut();
//Line 4. Initialization method to load attribute data
bool init(const char* uModelName, const char* uBlenderFile);
//Line 5. Method to update the game character
void update(double dt);
};

Defining the Astronaut Class

Next, open up the Astronaut.mm file.

Let's start by defining the Constructor and Destructor classes as shown in lines 1 & 2:

//Line 1. Constructor Method
Astronaut::Astronaut(){
}
//Line 2. Destructor Method
Astronaut::~Astronaut(){
}

Next, we need to define the init() method. The init() method is responsible for loading the attributes into the game object (line 3a) and for sending the attributes to the GPU (line 3c).

The init() method is also the place where you would enable Shadow effects for the character (line 3b). Within this method, you would also enable the Physics Engine, Collision System, animation objects, etc.

//Line 3. Init method
bool Astronaut::init(const char* uModelName, const char* uBlenderFile){
//Line 3a. Load the attributes into the game object
if (loadModel(uModelName, uBlenderFile)) {
//Line 3b. Enable shadows
setEnableShadow(true);
//Line 3c. Send attributes to the GPU
loadRenderingInformation();
return true;
}
return false;
}

Finally, we need to overwrite the update() method. The update() method is automatically called by the engine and is where you would update your character's properties.

//Line 4. Update method
void Astronaut::update(double dt){
}

Complete Code Snippet

Astronaut Class Definition

The Astronaut class definition should look as follows:

//Line 1. Constructor Method
Astronaut::Astronaut(){
}
//Line 2. Destructor Method
Astronaut::~Astronaut(){
}
//Line 3. Init method
bool Astronaut::init(const char* uModelName, const char* uBlenderFile){
//Line 3a. Load the attributes into the game object
if (loadModel(uModelName, uBlenderFile)) {
//Line 3b. Enable shadows
setEnableShadow(true);
//Line 3c. Send attributes to the GPU
loadRenderingInformation();
return true;
}
return false;
}
//Line 4. Update method
void Astronaut::update(double dt){
}

Instantiating the Astronaut Object

Click on the Earth.h class and inspect its class declaration. It states that Earth is a subclass of U4DWorld.

class Earth:public U4DEngine::U4DWorld

The U4DWorld provides access to the scenegraph; which in turns communicates with the Game Engine Loop. The Engine Loop takes care of rendering and updating the game object.

What does this mean?

It means that game objects can only be added to the Scenegraph through a subclass of U4DWorld. In this case, we are going to add the Astronaut object to the scenegraph through the Earth class.

Add a data member pointer

Open up Earth.h and include the Astronaut.h header file as shown below:

#include "Astronaut.h"

Next, add a data member that points to the Astronaut object, in this example, our data member is named littleAstronaut (line 1).

class Earth:public U4DEngine::U4DWorld{
private:
//Line 1. Add an Astronaut data member
Astronaut *littleAstronaut;
public:
Earth(){};
~Earth();
void init();
void update(double dt);
//Sets the configuration for the engine: Perspective view, shadows, light
void setupConfiguration();
//creates background models: islands, skyboxes, etc.
void setupBackgroundModels();
};

Instantiate the Astronaut

Let's initialize and render the Astronaut character.

Open the file Earth.mm.

In the init() method, scroll down to the line that says: CREATE ASTRONAUT HERE

Below this section, allocate memory for the object as shown in line 1.

//Line 1. Create an instance of Astronaut type
littleAstronaut=new Astronaut();

Next, we call the init() method and provide the name of the model and the name of the file where the attribute data can be found. This is shown in the code snippet below:

//Line 2. Load attribute (rendering information) into the game entity
// name of character: "astronaut"
// name of attributes file: "astronautAttributes.u4d"
if (littleAstronaut->init("astronaut","astronautAttributes.u4d")) {
}

The init() method returns true or false depending if the initialization was successful or not. This is why we use it as a test condition within the if statement.

If the initialization was successful, then we add the game object to the scenegraph, as shown in line 3.

//Line 2. Load attribute (rendering information) into the game entity
// name of character: "astronaut"
// name of attributes file: "astronautAttributes.u4d"
if (littleAstronaut->init("astronaut","astronautAttributes.u4d")) {
//3. If the initialization was successful, then add the game object into the scenegraph
addChild(littleAstronaut);
}

Complete Code Snippet

The complete code to create and add an entity into the scenegraph should look as follows:

Code snippet: init() method
void Earth::init(){
/*----DO NOT REMOVE. THIS IS REQUIRED-----*/
//Configures the perspective view, shadows, lights and camera.
setupConfiguration();
//Renders the background models: skybox and island models. Remove if you want to.
setupBackgroundModels();
//Line 1. Create an instance of Astronaut type
littleAstronaut=new Astronaut();
//Line 2. Load attribute (rendering information) into the game entity
// name of character: "astronaut"
// name of attributes file: "astronautAttributes.u4d"
if (littleAstronaut->init("astronaut","astronautAttributes.u4d")) {
//3. If the initialization was successful, then add the game object into the scenegraph
addChild(littleAstronaut);
}
}

Results

In Xcode, select either the iOS or Mac device and then click on Run.

The game engine should render the Astronaut.

image

Source Code

Download the source game files from our Github Tutorials page.