1 min read

Extending the Content Pipeline - Part 3

Building the Level Data Structure

Our next step is to create a data structure to store our level information. For this tutorial, the level will simply be a grid of numbers, though, in practice, a level will probably be quite a bit more sophisticated. We need to create a class to store our level information, and we will create this class in the game library, which we’ve called “LevelLibrary” (at least, that’s what I called mine) Chances are, you’ve added a class to your project before, but if not, here’s how you do it. Right-click on the LevelLibrary project and choose Add > New Item from the popup menu. In the dialog that pops up, choose the Class template and give your class a name. I’ve called mine “Level.cs”.

In this class, make the data structure for your level (or whatever else the extension is for). I’ve created mine like below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LevelLibrary
{
    public class Level
    {
        public int[,] Values { get; set; }

        public Level(int[,] values)
        {
            Values = values;
        }

        public int GetValue(int row, int column)
        {
            return Values[row, column];
        }

        public int Rows
        {
            get
            {
                return Values.GetLength(0);
            }
        }

        public int Columns
        {
            get
            {
                return Values.GetLength(1);
            }
        }
    }
}

Feel free to just copy all of this code into your Level.cs file and replace everything. The data for the level is just simply a two-dimensional array of ints. The constructor just takes this array and uses it for the level data. Additionally, we provide methods for getting the number of rows and columns in the level grid, as well as retrieving values from the grid.

If you don’t just copy this code, be sure to change your class to public so that you can access it from other parts of the program.