Consider this case:
You have a piece of code that should be executed after some SubModule, e.g. StoryModeSubModule, but you don't want to add StoryMode as a DependedModule.
This is not supported out-of-the-box, but ButterLib provides one possible solution.
protected override void OnSubModuleLoad()
{
base.OnSubModuleLoad();
// You first call Register() so the SubModule will be tracked by ButterLib
DelayedSubModuleManager.Register<StoryModeSubModule>();
// You subscribe to the module's method call.
// In this case, we do something after StoryModeSubModule.OnSubModuleLoad
// is executed.
DelayedSubModuleManager.Subscribe<StoryModeSubModule, SubModule>(
nameof(OnSubModuleLoad), SubscriptionType.AfterMethod, (s, e) =>
{
// StoryModeSubModule does not implement OnSubModuleLoad(),
// so we can only catch the base virtual method call.
if (e.IsBase)
return;
// SOME CODE
});
}
protected override void OnBeforeInitialModuleScreenSetAsRoot()
{
base.OnBeforeInitialModuleScreenSetAsRoot();
DelayedSubModuleManager.Register<GauntletUISubModule>();
DelayedSubModuleManager.Subscribe<GauntletUISubModule, SubModule>(
nameof(OnBeforeInitialModuleScreenSetAsRoot), SubscriptionType.AfterMethod, (s, e) =>
{
// GauntletUISubModule overrides OnBeforeInitialModuleScreenSetAsRoot, so we can
// catch the override method call.
if (!e.IsBase)
return;
// SOME CODE
});
}
Note:
- If for some reason your Module will load after the Module that you subscribed to, the delegate you passed in
Subscribewill be executed immediately. - If a derived
MBSubModuleBaseclass overrides the method you subcsribe to and calls the base.Method(), you will get two calls, one from the override and one from calling the empty virtual method. Don't forget to filter bySubscriptionEventArgs.IsBase. - The current implementation does not allow to subscribe to methods e.g.
OnBeforeInitialModuleScreenSetAsRoot()outside theOnBeforeInitialModuleScreenSetAsRoot()override. You will not be able to subscribe toOnBeforeInitialModuleScreenSetAsRoot()inOnSubModuleLoad()override and vise-versa.
This page was last modified at 05/15/2024 00:13:04 +03:00 (UTC).
Commit Message
Author: Alexey Chernyshov
Commit: 4bb15ac31cf6ed21944111379972e21e8dcdd38b
Update Overview.md