MBSingleton
MBSingleton can be used to create a C# singleton to access through other scripts.
Use the 'MBSingletonCreateOnLoad' attribute to have the Singleton instance created on the code/mod loaded.
MBSingleton's with MBVariables need a 'MBMemberLocation' attribute. This will set where the values will be saved (with the World, or Creation, Or Server, or as a Preference)
[MBUICategory("Game")]
[MBUIGroup("AI")]
[MBMemberLocation(MBMemberLocation.World)] // save values with each world
public class AStarPathfindingSystem : MBSingleton<AStarPathfindingSystem>
{
public BoolVariable UsePathfinding { get; private set; }
}
Unlike some Unity singleton patterns they are not auto created on use. They are set when added to the world - then set to null on Destroyed
using ModboxMain;
namespace ModboxMods.Core
{
public class CorePlayersSystem : MBSingleton<CorePlayersSystem>
{
// MBVariable events
[MBParameterNames("player")]
public EventVariable<EntMBPlayer> MBPlayerJoined { get; private set; }
[MBParameterNames("player")]
public EventVariable<EntMBPlayer> MBPlayerLeft { get; private set; }
// MBVariable Value that will show in the world settings under 'Game'
[MBShowInSettingWindow( SettingsWindow.World, "Game", "Rules")]
[MBDefaultValueAttribute(true)]
public BoolVariable AddPlayerNameTags { get; private set; }
protected override void Awake()
{
// always use base.Awake with MBSingleton
base.Awake();
// add a UI button on how to play
RegisteredUIButton helpButton = new RegisteredUIButton("How To Play", this, IconFiles.Instance.Help);
SettingsVars.ShowButtonInWindow(helpButton, SettingsWindow.MainMenu);
helpButton.SetButtonVisible = () =>
{
return MBGameState.InLobby;
};
helpButton.OnClicked = () =>
{
UIScreenTutorials tutorials = helpButton.LastUIScreenCanvas.ShowScreenByType<UIScreenTutorials>();
tutorials.HideEditTutorials();
tutorials.Intro.isOn = true;
};
EntMBPlayer.Event_MBPlayerJoined.AddListener(OnEvent_MBPlayerJoined);
EntMBPlayer.Event_MBPlayerLeft.AddListener(OnEvent_MBPlayerLeft);
}
void OnEvent_MBPlayerLeft(EntMBPlayerBase player)
{
MBPlayerLeft.RunEventLocal(player as EntMBPlayer);
}
void OnEvent_MBPlayerJoined(EntMBPlayerBase player)
{
MBPlayerJoined.RunEventLocal(player as EntMBPlayer);
}
}
}
Updated almost 2 years ago