1 Requirements

Tutorial 2 - Adding GameObjects

2 Introduction

Currently, you should be able to write templates for your predefined objects and create objects out of them. Although this is enough for creating a whole world, it lacks of flexibility and would result in a lot of code. To face this problem, the i6engine provides a simple function: loadLevel() that loads a level from a given xml file. In This tutorial, we show you how to use this method.

3 The XML

Take a look at listing 1. It demonstrates the basic structure of such an xml file: The root tag is a Map element. Within this map, you can define several sections indicated with flags (e.g. A or B). Later in the code you will specify one flag while loading the level. The engine will then load every game object defined within the corresponding section. Maybe you have noticed the third section having quite a weird flags attribute: A|B. You can use this | character, to define sections that are loaded if either the left or the right side matches the loading flag. Thus A|B is loaded whenever you load with flag A or with flag B. Sure, you can combine even more flags: A|B|C|D. We use the flag attribute to differentiate between objects only created on a server and others created only on clients, but you can use them for any other purpose. And last but not least each Attribute tag defines one attribute you would like to pass to the template for which you used the string map earlier. But that was kinda obvious, I guess.

 
1<?xml version="1.0" ?> 
2<Map> 
3    <Section flags="A"> 
4        <GameObject name="myTemp" send="True"> 
5            <Attribute name="pos">0.0 0.0 0.0</Attribute> 
6            <Attribute name="rot">1.0 0.0 0.0 0.0</Attribute> 
7            <Attribute name="scale">0.042 0.024 0.2</Attribute> 
8        </GameObject> 
9    </Section> 
10    <Section flags="B"> 
11        <GameObject name="myTemp" send="True"> 
12            <Attribute name="pos">0.0 0.0 0.0</Attribute> 
13            <Attribute name="rot">1.0 0.0 0.0 0.0</Attribute> 
14            <Attribute name="scale">0.042 0.2 0.024</Attribute> 
15        </GameObject> 
16    </Section> 
17    <Section flags="A|B"> 
18        <GameObject name="myTemp" send="True"> 
19            <Attribute name="pos">0.0 0.0 0.0</Attribute> 
20            <Attribute name="rot">1.0 0.0 0.0 0.0</Attribute> 
21            <Attribute name="scale">0.2 0.024 0.042</Attribute> 
22        </GameObject> 
23        <GameObject name="Terrain" send="True"> 
24            <Attribute name="heightmap">terrain.png</Attribute> 
25            <Attribute name="size">1700.0</Attribute> 
26            <Attribute name="inputScale">60.0</Attribute> 
27            <Attribute name="vertices">65</Attribute> 
28            <Attribute name="layers">1</Attribute> 
29            <Attribute name="layer_0_size">10</Attribute> 
30            <Attribute name="layer_0_diffusespecular">GrassFloor.tga</Attribute> 
31            <Attribute name="layer_0_normal">GrassFloor.tga</Attribute> 
32            <Attribute name="minX">0</Attribute> 
33            <Attribute name="maxX">0</Attribute> 
34            <Attribute name="minY">0</Attribute> 
35            <Attribute name="maxY">0</Attribute> 
36        </GameObject> 
37    </Section> 
38</Map>
Listing 1: level.xml

4 Loading Levels

We’re nearly done. The xml is written. Now comes the C++ code to load a level.

1i6e::api::EngineController::getObjectFacade()->loadLevel("level.xml", "A");
Listing 2: loading a level

Yeah, you’re done. Pass the path to the level as well as the flag specifying the section(s) you want to load and the rest is done by the engine.

5 Terrain

You may have noticed the new GameObject map used in the xml file. A Terrain is one of the very few GameObject templates offered with the engine. You can use them just like your own ones. For a terrain, you have to specify the heightmap in map, the texture layers in layers, the size extends in size, the height scale factor in inputScale, the different terrain segments with minX, maxX, minY and maxY and the textures for diffuse and specular map in layer_0_diffusespecular and for displacements and normal map in layer_0_normal. The attribute vertices has to be always a value of 2n + 1. Everything else is done by the engine.

6 Conclusion

You can define your level within an xml file. Thus you can easily switch between levels ingame and you can modify your level without recompiling your game.

7 What’s next?

The next tutorial will show you how to use physic.