Scenes and Levels



One common question you see when it comes to using the Armory engine is “how do I structure my game?” This tutorial demonstrates one way a game can be organized into levels and scenes, using Scenes in Blender. In Blender, you can have multiple Scenes in a single Blend file. You can create a new Scene using the + icon in the topmost Blender bar.

Armory3D Levels and Scenes 1

For this example, I created the following collection of Scenes:

Armory3D Levels and Scenes 2 - devga.me

Keep in mind, for each scene you create, you need to manually set the renderer to Cycles to work with Armory. TitleScene is going to be our simple game start screen, displaying a title graphic and playing background music until the space bar is pressed, at which point we will load Level One. We will then demonstrate how to switch between game scenes or levels by switching between LevelOne and LevelTwo. We will also show how to pull data from a separate scene, demonstrating how you can share data across multiple scenes.

Ok, let’s start with the title scene. I create the following simple scene in Blender:

Armory3D Levels and Scenes 3 - devga.me

With the following structure:

Armory3D Levels and Scenes 4 - devga.me

Be sure to watch the earlier Audio chapter on details on setting up the Speaker object and hooking up a sound file. In this case, I added a simple soundtrack .ogg file I download from the internet. The title screen is simply a 1024×768 texture that I applied to a plane I scaled and UV mapped. For details on recreating everything, be sure to check the video version of this tutorial. I also configured the camera in the rendering settings to match these settings:

Armory3D Levels and Scenes 5 - devga.me

Next, we are going to create a node graph to start playing our title screen music and wait for the spacebar to be hit. Let’s start off by playing some music on Init:

Armory3D Levels and Scenes 6- devga.me

Then check for the spacebar being hit, at which point we stop playing our music and switch scenes. It’s important to stop playing the music before changing scenes, as the speaker is part of the scene we are leaving!

Armory3D Levels and Scenes 7- devga.me

Switching scenes is as simple as calling the Set Scene node, passing in the name of the new scene to load. In this case, we are loading the LevelOne scene.

Now we attach our node tree to the scene trait of our title scene:

Armory3D Levels and Scenes 8- devga.me

Now let’s set up two different scenes, LevelOne and LevelTwo. This would obviously be your different game levels, making sure that at least one camera is defined in each. In this case, I simply populate with a couple of different meshes.

Armory3D Levels and Scenes 9- devga.me

Now let’s wire up a node graph that is going to handle switching between the two different scenes. I created a new graph named SceneSwitcher and use the following nodes:

clip_image020Armory3D Levels and Scenes 10- devga.me

This really looks more complicated than it is. When the keyboard is pressed we check the active scene name and compare it to “LevelOne”. If it’s already level one, we switch to “LevelTwo” or vice versa. Scene switching is done using the Set Scene node, passing in the name of the scene to make active.

Now when we run our game, we can hit spacebar to toggle between active scenes.

Remember the Data scene we created earlier? Well, we actually have the ability to populate the contents of one scene into our active scene. This gives a handy way to create reusable objects that can be used across different scenes. In the Data scene, we are going to create a single mesh node populated with Susanne, like so:

clip_image020Armory3D Levels and Scenes 11- devga.me

Notice there are no cameras or lights in this scene? You of course can create other objects, but they will also be imported when we instance this scene in our active scene. Speaking of which, let’s look at how we do that! In our existing graph, add the following nodes:

clip_image020Armory3D Levels and Scenes 12- devga.me

Here we create the new scene using a Spawn Scene node, once again you can set the scene value by passing in the scene’s name. In this case, we randomly update the transform to be between -2 and +2 in x, y, and z directions. This means whenever we press the Shift key, a new Suzanne will be randomly created within our scene.

Now don’t forget to apply our newly created SceneSwitcher node graph to BOTH LevelOne and LevelTwo scene traits.

Now run your game. Be sure to select the TitleScene as active when you run your Armory3D project, as the currently active scene is the one that will be loaded when run. And the end results:

Armory3D Levels and Scenes 13- devga.me

So far, we did the entire process using Nodes, but it is all possible using Haxe as well. If you’d prefer to develop in Haxe, here is the equivalent code:

TitleScreen.hx

package arm;

class TitleScreenHaxe extends iron.Trait {
  public function new() {
    super();

    notifyOnInit(function() {
      iron.Scene.active.getSpeaker("Speaker").play();
    });

    notifyOnUpdate(function() {
      if(iron.system.Input.getKeyboard().released("space")){
        iron.Scene.active.getSpeaker("Speaker").stop();
        iron.Scene.setActive("LevelOne");
      }

    });
  }
}

SceneSwitcher.hx

package arm;

import iron.object.Object;
import iron.math.Vec4;

class SceneSwitcher extends iron.Trait {
  public function new() {
    super();

    notifyOnInit(function() {
    });

    notifyOnUpdate(function() {
      if(iron.system.Input.getKeyboard().released("shift")){
        iron.Scene.active.addScene("data",null,function(o:Object){
          var randomX = (Std.random(100)/100.0 *4) -2;
          var randomY = (Std.random(100)/100.0 *4) -2;
          var randomZ = (Std.random(100)/100.0 *4) -2;

          o.transform.loc = new Vec4(randomX,randomY,randomZ,1);
          o.transform.buildMatrix();
        });
      }
      if(iron.system.Input.getKeyboard().released("space")){
        
        if(iron.Scene.active.raw.name == "LevelOne")
          iron.Scene.setActive("levelTwo");
        else
          iron.Scene.setActive("levelOne");
      }
    });
  }

Scroll to Top