Switching Context

Introduction

This tutorial teaches how to easily switch between contexts during run-time. Switching context allows us to alter the behavior of a game or application while keeping the context hierarchy intact.

In this tutorial we continue with the result of the previous tutorial. If this is the first time using IoC+, we suggest to read the introduction first.

The Switch Signal

In this tutorial we’re going to make the game context switch to an application context, which will bind an alternative value to the string type so our logger view will output a different piece of text. Let’s start with the signal that will trigger the game context to switch. Create a new script file called “SwitchContextSignal.cs” and add the following code.

using IoCPlus;

public class SwitchContextSignal : Signal { }

The Application Context

Let’s make a context to switch to. This will act as the same as the game context, only with a small alteration. Create a new script file called “SwitchContextSignal.cs” and add the following code.

using IoCPlus;

public class ApplicationContext : Context {

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

        Bind<string>("Application");
        Bind<SwitchContextSignal>();

        On<SwitchContextSignal>().SwitchContext<GameContext>();
    }

}

Instead of binding the “Game” value to the string type, the application context binds the “Application” value. Also, it binds the SwitchContextSignal. When the SwitchContextSignal is dispatched, the application context will “switch” to the game context.

We need to update the game context so it will switch to the application context when the SwitchContextSignal is dispatched. It will also need to inject the signal. Open up the GameContext and update it with the following code.

using IoCPlus;

public class GameContext : Context {

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

        Bind<string>("Game");
        Bind<SwitchContextSignal>();

        On<EnterContextSignal>().AddContext<MenuContext>();

        On<SwitchContextSignal>().SwitchContext<ApplicationContext>();
    }

}

There, now we are able to switch back and forth!

The IoC+ Monitor

Go ahead, hit play and open up the IoC+ Monitor. We can test the SwitchContextSignal without a view, by right-clicking it in the IoC+ Monitor and selecting “Dispatch SwitchContextSignal”. Using this feature can save you a lot of time when developing parts of functionality. If you’d now hit space you should get a log message containing the name of the current context.

Child contexts, like the menu context, are automatically passed on to the context that is being switched to, but all bindings are updated. This means we could easily bind a different mediator to a view, or change the response of commands on the dispatch of a signal. Note that contexts will not be exited or re-entered upon switch.

Conclusion

In this tutorial we’ve seen how to switch a context. We’ve learned that the context hierarchy will stay intact upon a switch as child contexts will be passed to the new context. Switching context allows us to easily change the behavior of our game or application. We’ve also learned how to easily test new features without having to implement a view by dispatching the signal via the IoC+ Monitor.