Game Engine Tutorial Part III, Chapter 6 – Loading Content

We need to provide a way for components to load content. They can’t do it in their constructors because they may not have a GameScreen set to access the Engine’s content manager yet. To accomplish this we will add a Load() function to the Component class, which will be called (assuming the component has not yet been loaded) by the GameScreen when it binds to the component.

The first step is to add the Load function to the Component class:

protected virtual void Load()
{
}

The function is virtual, and protected, which means it can be overridden by child classes, and is also only available inside the Component class and it’s children. This means that it cannot be called externally, which is the desired behavior because we don’t want to have it called more than once. We will, however, need to provide an external way to tell the Component to load, and this is with the LoadComponent function below. You will notice that it first checks if the component is loaded, using the new “Loaded” property:

bool loaded = false;

public bool Loaded { get { return loaded; } }

public void LoadComponent()
{
    if (!loaded)
        Load();

    loaded = true;
}

Now we just need to add the code to the GameScreen class that will call this function. In the “AddComponent” function, after “Component.Parent = this;”, add:

Component.LoadComponent();

The GameScreen doesn’t need to worry about whether or not the Component has loaded already, as the component will perform this check itself.

We also want to allow the GameScreen to load content, so we will add nearly identical code to the GameScreen class as we did to the Component class:

bool loaded = false;

public bool Loaded { get { return loaded; } }

public void LoadGameScreen()
{
    if (loaded)
        return;

    loaded = true;

    Load();
}

protected virtual void Load()
{
}

Now we just need to have the Engine class call the external load function. After, “GameScreen.engine = this;” in the “PushGameScreen” function, add:

GameScreen.LoadGameScreen();

Again, the Engine class doesn’t need to worry about whether the GameScreen has been loaded already or not, as the GameScreen will perform the check itself.

That’s all we need to do to allow Components and GameScreens to load content!

Download the Code for this Chapter

2 Responses to “Game Engine Tutorial Part III, Chapter 6 – Loading Content” »

  1. Pingback by Innovation Engine Roadmap 2009-2010 | Innovative Games — January 5, 2010 @ 9:05 pm

    [...] Loading Content [...]

  2. Comment by Fan of Sean — October 28, 2010 @ 7:16 am

    Hi!

    Why specificall did you imlement the two loading functions differently? Clearly, they both do the same stuff, but you implemented it in two different versions:

    public void LoadComponent()
    {
    if (!loaded)
    Load();

    loaded = true;
    }

    public void LoadGameScreen()
    {
    if (loaded)
    return;

    loaded = true;

    Load();
    }

    The second version seems prettier though, the first always sets the variable loaded, even when there is no need anymore.

    Anyway, great Tutorial, thank you very much!

RSS feed for comments on this post. TrackBack URI

Leave a comment