05. Working with Multiple Levels


Many tile-based games include multiple levels. This walk-through shows how to set up a multi-level game.

Defining currentLevel

So far our GameScreen.cs  directly references Level1  when constructing collision. For example, the wall collisions are initialized as follows:

We can modify our code to create a currentLevel  variable in GameScreen  which is assigned in one place and used to create our collision. This abstraction can make it easier to grow a project (adding more collision and performing other custom logic using the currentLevel ).

To add currentLevel , modify the GameScreen  as shown in the following snippet:

The code above includes the following changes:

  • The currentLevel  variable is defined at class scope and is used to assign collision. Any additional logic depending on the LayeredTileMap  in subsequent guides will use the currentLevel  variable.
  • The currentLevel  variable is assigned in one place – AssignLevel . This will eventually contain logic to dynamically select a level.
  • Collision initialization has been moved into its own method. This can help keep code organized, especially for games which include multiple TileShapeCollection  instances.

Automatic Creation vs. LoadedOnlyWhenReferenced

So far we’ve been relying on the automatic creation of Level1  – prior to changing our code to use the currentLevel  variable, we haven’t written any code to create our level. Glue automatically creates Level1  for us. Of course, as we add additional levels we want to decide which level to create – otherwise all levels would be loaded at the same time.

To modify our project so that level creation is done in code:

  1. In Glue, expand GameScreen’s Files folder and select Level1
  2. Change LoadedOnlyWhenReferenced to true. Level1 will no longer be loaded and added to the engine automatically.

  3. In Visual Studio, open GameScreen.cs
  4. Modify AssignLevel  to add the created level to managers:

Now our level still loads just like before, but the creation is in our custom code rather than generated code. Since our code handles creation, we also have to modify CustomDestroy  to destroy the current level:

Adding additional levels

Our code and project are now set up to support multiple levels. For the sake of simplicity, we’ll copy the existing Level1 file to create our second level:

  1. Open the folder that contains Level1.tmx
  2. Copy and paste Level1.tmx
  3. Rename the newly-pasted file to “Level2”

  4. Drag+drop Level2.tmx into GameScreen’s Files folder in Glue
  5. Set Level2’s LoadedOnlyWhenReferenced to true so that it doesn’t get automatically loaded by Glue

Dynamically Loading Levels

Our game now has two levels which can be loaded: Level1 and Level2. We can change our code to load levels using a string value with the GameScreen.GetFile  method.

First, we’ll add a static LevelToLoad  variable in the GameScreen  class. This variable is static because games typically need to access it before the GameScreen  is created (such as from a level select screen).

Modify the GameScreen  to include the LevelToLoad  property:

Next, modify the AssignLevel  method to use the LevelToLoad  property:

Now the default value for LevelToLoad can be changed to any of the valid levels (Level1 or Level2) to load a different level. You may want to make changes to Level2.tmx to see the difference when switching to loading Level2: