Your first context

Introduction

This tutorial teaches how to define and instantiate a context which will use a predefined signal to execute a command. A context is a container of injection, command and mediation bindings. Contexts are responsible for binding modules of IoC together.

If this is the first time using IoC+, we suggest to read the introduction first.

The Context

Open up a fresh new Unity Project and import the IoC+ framework from the Asset Store. Create a new C# script called “GameContext.cs”. This code file will contain the context of our little example. Add the following code.

using IoCPlus;

public class GameContext : Context {

	protected override void SetBindings() {
        base.SetBindings();
    }

}

Our own contexts are always a subclass of the Context class. In the override SetBindings() method we can set additional bindings, but notice that we call the SetBindings() of the base class. Some bindings are set by default. One of those bindings is the EnterContextSignal.

The EnterContextSignal is a signal that will dispatch when the context is instantiated. In the SetBindings() method, we can bind commands as a response to signals.

The Command

Let’s first define a command. Create a new script called “WelcomeCommand.cs” and add the following code.

using IoCPlus;
using UnityEngine;

public class WelcomeCommand : Command {

    protected override void Execute() {
        Debug.Log("Welcome to the Game Context!");
    }

}

Commands in IoC+ are always a subclass of the Command class. In the override Execute() method we write the code that the command needs to execute. In this case, a little debug log to test whether our command is being executed as a response to our signal.

Binding the Command

In IoC+ we can set bindings in Contexts. There are three types of bindings: injection bindings, mediator bindings and command bindings. We’ll cover all bindings in upcoming tutorials. In this tutorial we’ll focus on command bindings!

Let’s create a command binding now to bind the WelcomeCommand as a response to the EnterContextSignal. Open up the GameContext and add the following code.

using IoCPlus;

public class GameContext : Context {

	protected override void SetBindings() {
        base.SetBindings();

        On<EnterContextSignal>().Do<WelcomeCommand>();
    }

}

By calling the On<T>() method, we define a signal type that we want to respond to. We can use the result of the On<T>() method to add commands as a response to that signal via the Do<T>() method. In this case, when the context is entered, the WelcomeCommand will be executed.

The Root Context

If we’d hit the Unity play button right now, we’d still get nothing. This is because the GameContext still has to be instantiated. To easily instantiate the first context of a game or application in IoC+, we use a ContextRoot. Create a new script called “GameRoot.cs” and add the following code.

using IoCPlus;

public class GameRoot : ContextRoot<GameContext> { }

We make our GameRoot a subclass of the ContextRoot<T> class, where we set T to the context type that we want to instantiate: the GameContext. In the Unity scene, create a new empty gameobject. Add the GameRoot script as a component to the gameobject. This gameobject will now instantiate our GameContext on awake.

Go ahead and hit play! If you’ve followed the steps correctly, you should now see our welcome text pop up in Unity’s debugger.

The IoC+ Monitor

Want to see something cool? Open up the IoC+ Monitor via Window -> IoC+ Monitor in the top bar of Unity and hit play!

The IoC+ Monitor gives a live feed of all contexts in game. It shows our instance of the GameContext, flashes the EnterContextSignal and then flashes the WelcomeCommand as it is triggered as a response.

Why does my IoC+ Monitor not show the same things as in the image?

The IoC+ Monitor has a set of options that allows the user to tweak what is displayed. In the top-left of the IoC+ Monitor there are three time options: “Sync”, “Delay” and “Pause”. The tutorial images are all shown in Delay mode. In the top-right of the IoC+ Monitor there is a button for even more options. Make sure to click it and turn on all display options to get the same result as the images in the tutorials.

We can right-click the context, signal and command to open the associated code file. By using the right-click we can even dispatch the EnterContextSignal again!

Why does my IoC+ Monitor not open the correct code file when I right-click?

When opening a code file the IoC+ Monitor tries to open a file with the same name as the type (e.g. “GameContext”). Namespaces make it possible to have multiple types with the same name. In this case it is undefined which code file will be opened. Delete the other tutorials from the IoC+ package to be sure that the correct code files are opened.

Conclusion

In this tutorial we’ve seen how to declare a context and a command. Contexts are there to link IoC modules together and we’ve seen how to bind a command as a response to the EnterContextSignal, a default signal from IoC+. We’ve made a context root to instantiate the root context of our game. Last but not least, we’ve used the IoC+ Monitor to visualize and test our creations in real time!