Scene changes
In this section, you will find details on how to change scenes in the AlchemyBow.Core framework.
To change a scene, you should use the ChangeScene(int) method. Have you noticed that this method is protected
? The reason for this is to discourage direct references to instances of the CoreController, as doing so can create subtle circular dependencies (the controller uses modules, which in turn directly or indirectly reference the controller).
A simple and effective way to trigger scene changes is by using a mediator object. For example:
public class GameSceneTrigger
{
public event System.Action Triggered;
public void Trigger()
{
Triggered?.Invoke();
}
}
You can bind your mediator and then inject it wherever you need to trigger the scene change. The controller only needs to subscribe to the event:
using AlchemyBow.Core;
using AlchemyBow.Core.IoC;
using System.Collections.Generic;
[InjectionTarget]
public class MyCoreController : CoreController<MyCoreProjectContext>
{
private const int GameSceneBuildIndex = 0;
[Inject]
private GameSceneTrigger gameSceneTrigger;
protected override void OnLoadingFinished()
{
gameSceneTrigger.Triggered += OnGameSceneTriggered;
base.OnLoadingFinished();
}
protected override void OnSceneChangeStarted()
{
gameSceneTrigger.Triggered -= OnGameSceneTriggered;
base.OnSceneChangeStarted();
}
private void OnGameSceneTriggered()
{
ChangeScene(GameSceneBuildIndex);
}
}
How is this approach better than directly referencing the CoreController? By using the mediator, your modules are decoupled from the controller and instead become tightly coupled with the mediator. This makes it much easier to implement these modules in scenes that use different controllers.
Note
The method described in this article is very similar to how state conditions work (see: Stateful Behaviour).
Note
You can create public
methods in your implementations of CoreController, but it is recommended to only use them for prototyping purposes.
Tip
You can use the AlchemyBow.LoadingScenes package to efficiently manage loading screens.