Scene changes
In this section, you will find details on changing scenes in the AlchemyBow.Core framework.
You probably already know that to change the scene you should use the ChangeScene(int) method.
Have you noticed that it is protected
? The reason is to discourage you from referring to instances of the CoreController because it creates inconspicuous circular dependecies (the controller uses modules that directly or indirectly use the controller).
An easy way to trigger scene changes is to create a mediator object. For example:
public class GameSceneTrigger
{
public event System.Action Triggered;
public void Trigger()
{
Triggered?.Invoke();
}
}
You can bind it, 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 += OnGameSceneTrigger;
base.OnLoadingFinished();
}
protected override void OnSceneChangeStarted()
{
gameSceneTrigger.Triggered -= OnGameSceneTrigger;
base.OnSceneChangeStarted();
}
private void OnGameSceneTrigger()
{
ChangeScene(GameSceneBuildIndex);
}
// ...
}
How is it better then using a reference to the CoreController? Your modules are tightly coupled with the mediator instead of the controller, so it is much easier to implement them in scenes with 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 the CoreController. However, it is strongly recommended that you only use them for prototyping purposes.
Tip
You can use the AlchemyBow.LoadingScenes package to easily manage loading screens.