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
Subscribe
will be executed immediately. - If a derived
MBSubModuleBase
class 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 10/13/2020 15:35:52 +03:00 (UTC).
Commit Message
Author: aragas
Commit: f25a1264d096d9287b99c5d2b04bd6032ad81546
Actually fixed the shit