-
Notifications
You must be signed in to change notification settings - Fork 0
GameModeAPI
This module implements a game mode selection system to multiplayer and single player lobbies.
Handles registrations of game modes, provides useful event callbacks for before and after each game session, and also provides plugin information
The module should be treated like a static class
and should be accessed accordingly GameModeAPI.PropertyFieldOrMethod
.
Do all registrations right when your mod is added or you might run into problems
These are general use case hooks and can be used outside of game mode instances.
//Called on both client and server right before game session starts
GameModeAPI.OnPreGameStart += OnPreGameStart;
//Called on both client and server right after game session starts
GameModeAPI.OnPostGameStart += OnPostGameStart;
//Called on both client and server right after game session ends
GameModeAPI.OnGameEnd += OnGameEnd;
//Called on server only right after a player connects
GameModeAPI.OnPlayerConnect += OnPlayerConnect;
//Called on server only right after a player disconnects
GameModeAPI.OnPlayerDisconnect += OnPlayerDisconnect;
Game mode changes the game session's behaviors allowing for more varieties of gameplay.
How do I create a new custom game mode?
The API provides a base class called GameMode
that on it's own acts exactly like the vanilla game mode
but expanded to be able to customize a few features.
Every custom game mode should inherit the GameMode
class as it's root foundation and expand it's functionality to tailor a unique game mode.
GameMode
provides some useful callback methods to create various custom gameplay behaviors but also leaves room for expandability for custom hooks where it lacks by using the SetExtraHooks
and UnsetExtraHooks
callback methods.
public class MyGameMode : GameMode
{
public MyGameMode(string gameModeName) : base(gameModeName)
{
//Good place to change default game mode settings
}
protected override void Start(Stage self)
{
//Good place to create custom behaviors when the stage first starts
//Client and server
}
protected override void Update(Stage self)
{
//Good place to create custom behaviors that require constant updates
//Client and server
}
protected override void OnTeleporterInteraction(TeleporterInteraction self, Interactor activator)
{
//Good place to create custom behaviors when a teleporter is interacted with
//Server only
}
protected override void OnGameOver(Run self, GameEndingDef gameEndingDef)
{
//Good place to create custom behaviors when there's a game over
//Server only
}
protected override void OnPlayerRespawn(Stage self, CharacterMaster characterMaster)
{
//Good place to modify player characters before they respawn here
//Server only
}
protected override void OnStagePopulate(SceneDirector self)
{
//Good place to spawn interactables here (chests, drones, etc)
//Server only
}
public override void SetExtraHooks()
{
//Set custom game mode specific hooks here
//Client and server
//Example: On.RoR2.ShrineRestackBehavior.AddShrineStack += PreventRevivesShuffle;
}
public override void UnsetExtraHooks()
{
//Unset custom game mode specific hooks here
//Client and server
//Example: On.RoR2.ShrineRestackBehavior.AddShrineStack -= PreventRevivesShuffle;
}
}
The base GameMode
class also comes with a few built-in settings fields that can change the behavior of the custom game mode.
//Allow vanilla's mob spawning system
//Turning this to false will prevent mobs from spawning by normal means
MyGameMode.AllowVanillaSpawnMobs = true;
//Allow vanilla's interactables spawning system (spawning of chests, drones, etc)
//Turning this to false will prevent interactables from spawning by normal means but will still recieve callbacks to OnStagePopulate allowing creation of custom behaviors to happen
MyGameMode.AllowVanillaInteractableSpawns = true;
//Allow vanilla's teleporter interactions
//Turning this to false will prevent the teleporter from activating by normal means but will still recieve callbacks to OnTeleporterInteraction allowing creation of custom behaviors to happen
MyGameMode.AllowVanillaTeleport = true;
//Allow vanilla's game over to happen
//Turning this to false will prevent returning to lobby by normal means but will still recieve callbacks to OnGameOver allowing creation of custom behaviors to happen
MyGameMode.AllowVanillaGameOver = true;
//Prevents items contained in this collection from spawning in the game
MyGameMode.BannedItems = new HashSet<ItemIndex>();
//Prevents equipments contained in this collection from spawning in the game
MyGameMode.BannedEquipments = new HashSet<EquipmentIndex>();
How do I register the new custom game mode?
//Registers a custom game mode
GameModeAPI.RegisterGameMode("My game mode description", "@MyAssetBundle:Assets/Resources/UI/MySprite.png", MyGameMode("My game mode name")); //Ensure each game mode or game mode variant has a unique name
How do I get all registered game modes?
IReadOnlyDictionary<string, GameMode> gameModes = GameModeAPI.GameModes;
How do I check what the active game mode is?
After leaving the lobby scene the API stores the active game mode in GameModeAPI.ActiveGameMode
.
GameModeAPI.ActiveGameMode
will be null if currently not in a game session or the game mode mod that the host is running is not installed on the local machine.
A easy place to do all this is by using the built-in hook GameModeAPI.OnPreGameStart += OnPreGameStart
.
static void OnPreGameStart()
{
//Check if the active game mode is a MyGameMode instance
if (GameModeAPI.ActiveGameMode is MyGameMode)
{
//Cast the active game mode as MyGameMode
MyGameMode myGameMode = (MyGameMode)GameModeAPI.ActiveGameMode;
}
}