1 Requirements

Tutorial 1 - First Steps

2 Introduction

Now that we have a simple game running, we can start adding some GameObjects.

3 What is a GameObject?

It is necessary to add some theory here as the whole GameObject system is kind of complex. But if you know how to use it, it is really simple and flexible.
Every entity in the world is a GameObject. Obviously each object like cars, trees, etc. but also the terrain, special points of interest in the world or some areas with special effects if you enter them. All these GameObjects (or at least most of them) have one thing in common: The position in the world. But many other properties differ. Cars can move, areas are not visible, trees can’t be pushed away, your main character is followed by a camera, ... . A common approach is creating a GameObject class and deriving many subclasses for different types of objects. This is a fast way and sufficient for small games. But in larger games with many different objects where some of them can change their behaviour within the game (e.g. a player picked up an item giving him invulnerability), the amount of subclasses will explode as well as some code that is needed more than once.
To face this problem, we use another design pattern: We have a class GameObject that is nothing but a container for components. A component implements a specific property objects can have. Examples are: Position in the world, being displayed on the screen. If you now need a GameObject, just create one object of type GameObject. This newly created object is empty: It can’t do anything. Next, you have to add some components to the GameObject. These than control the behaviour of the GameObject or define some properties. After creating a GameObject, you can add and delete components as you like. As soon as you add one, the GameObject will behave accordingly to the component. Likewise, the GameObject will loose a certain ability in the moment you remove the corresponding component. You probably won’t understand this concept now, but using it will make things clear.

4 Creating GameObjects

Every GameObject in the world is created out of a template you have to specify in xml. In this xml file you specify what type the GameObject has and what components it has with which default settings. Every parameter can be changed later for specific GameObjects and also it is possible to add and delete Components. For simplicity, we start with a template for creating a GameObject that has a position in the world, has a mesh (it is visible) and a camera attached to it so we can see it.

 
1<?xml version="1.0" encoding="UTF-8"?> 
2<GOTemplate type="Tutorial"> 
3    <Component template="StaticState" owner="true"> 
4        <Attribute name="pos">0.0 0.0 0.0</Attribute> 
5        <Attribute name="rot">1.0 0.0 0.0 0.0</Attribute> 
6        <Attribute name="scale">1.0 1.0 1.0</Attribute> 
7    </Component> 
8    <Component template="Camera" owner="true"> 
9        <Attribute name="pos">0.0 7.0 30.0</Attribute> 
10        <Attribute name="lookAt">0.0 4.0 -10.0</Attribute> 
11        <Attribute name="nearclip">1</Attribute> 
12        <Attribute name="aspect">0.5235987755982988</Attribute> 
13        <Attribute name="viewport">1</Attribute> 
14        <Attribute name="vp_left">0.0</Attribute> 
15        <Attribute name="vp_top">0.0</Attribute> 
16        <Attribute name="vp_width">1.0</Attribute> 
17        <Attribute name="vp_height">1.0</Attribute> 
18        <Attribute name="vp_red">0.0</Attribute> 
19        <Attribute name="vp_green">0.0</Attribute> 
20        <Attribute name="vp_blue">0.0</Attribute> 
21        <Attribute name="vp_alpha">1.0</Attribute> 
22    </Component> 
23    <Component template="MeshAppearance" owner="true"> 
24        <Attribute name="mesh">Ball.MESH</Attribute> 
25        <Attribute name="visibility">1</Attribute> 
26        <Attribute name="pos">0.0 0.0 0.0</Attribute> 
27        <Attribute name="rot">1.0 0.0 0.0 0.0</Attribute> 
28        <Attribute name="scale">1.0 1.0 1.0</Attribute> 
29    </Component> 
30</GOTemplate>
Listing 1: GOTemplateTutorial.xml

5 Using the Template

Now that we have our template, we need to use it in the game (MyApp.cpp). We can create GameObjects as shown in listing 2. The GOTemplate parameter is used for specifying the Components on the GameObject. But we will use the default configuration from the xml, so this parameter doesn’t matter for now. The third parameter is the owner id. This feature is only necessary for multiplayer games, in single player the id should always be the one of the EngineController. The true indicates whether the object shall be synchroniszed on other clients. As we don’t have any network at the moment, this value also doesn’t matter. Of course you have to include

i6engine/api/facades/ObjectFacade.h

to get the code compiling.

1i6e::api::EngineController::GetSingleton().getObjectFacade()->createObject("Tutorial", i6e::api::objects::GOTemplate(), i6e::api::EngineController::GetSingleton().getUuid(), true);
Listing 2: creating objects

Ok, we have a first object in our world. But we won’t see it. Why? Because we don’t have any light. Luckily to get a simple light is a one-liner shown in listing 3. Don’t forget to include

i6engine/api/facades/GraphicsFacade.h

1i6e::api::EngineController::GetSingleton().getGraphicsFacade()->setAmbientLight(1.0, 1.0, 1.0);
Listing 3: adding lights

Now you should see your object in the world. Listing 4 shows the complete AfterInitialize().

1AfterInitialize() { 
2    i6e::api::EngineController::GetSingleton().getGraphicsFacade()->setAmbientLight(1.0, 1.0, 1.0); 
3    i6e::api::EngineController::GetSingleton().getObjectFacade()->createObject("Tutorial", i6e::api::objects::GOTemplate(), i6e::api::EngineController::GetSingleton().getUuid(), true); 
4}
Listing 4: AfterInitialize()

Last thing to do is adding a resource path to our resources.cfg. You can add as many FileSystem lines as you want. But they all have to be below the line with [i6engine]. The path specified will be used to find the files you try to load for the graphic (.mesh, .material, .png, ...).

1[i6engine] 
2FileSystem=.
Listing 5: resource.cfg

6 What’s next?

Next, we will add a terrain to our world.