Creating a GameMode
Note
This article is describing features that are currently unreleased, but will be released in a future patch.
- The minimal example using
BaseGameMode
described here will cause null reference exceptions ifGetHUD()
returns null.
Gamemodes define rules about what should happen on a level. They are set in a level file. Typically when making a gamemode, you'll want to inherit the class from either BaseGameMode
, VersusGameMode
, or TeamVersusGameMode
. Or, if you're just adding something very simple to an existing gamemode, you can also inherit from Campaign
, Survival
, Deathmatch
, etc.
Minimal gamemode
The most minimal gamemode you can make is this:
[GameMode] class BasicGamemode : BaseGameMode { BasicGamemode(Scene@ scene) { super(scene); } }
Note the [GameMode]
preprocessor that describes our BasicGamemode
class. This tells the engine that this class is a gamemode that should show up in the editor and be a playable gamemode.
The above will simply spawn the player in the map without any weapons or HUD. If we wanted to give the player a few weapons for example, we'd add an override for SpawnPlayer
provided by BaseGameMode
:
void SpawnPlayer(int i, vec2 pos = vec2(), int unitId = 0, uint team = 0) override { BaseGameMode::SpawnPlayer(i, pos, unitId, team); // Give weapons g_players[i].AddWeapon("weapons/smg.sval"); g_players[i].AddWeapon("weapons/railgun.sval"); // Give ammo g_players[i].GiveAmmo("weapons/ammo/bolts.sval", 30); g_players[i].GiveAmmo("weapons/ammo/bullets.sval", 500); }
At this point you probably also want to add a HUD so you know what's happening. To do this, we have to do a few things:
- Instantiate a
HUD
object using the GUI builder - Return the object in the
GetHUD()
method - Update the HUD in the
UpdateWidgets(...)
method - Render the HUD in the
RenderWidgets(...)
method
This looks roughly like this:
HUD@ m_hud; BasicGamemode(Scene@ scene) { super(scene); @m_hud = HUD(m_guiBuilder, null); } HUD@ GetHUD() override { return m_hud; } void UpdateWidgets(int ms, GameInput& gameInput, MenuInput& menuInput) override { m_hud.Update(ms); BaseGameMode::UpdateWidgets(ms, gameInput, menuInput); } void RenderWidgets(PlayerRecord@ player, int idt, SpriteBatch& sb) override { m_hud.Draw(sb, idt); BaseGameMode::RenderWidgets(player, idt, sb); }
Additionally, you can create a class that inherits from HUD
and use that instead, if you want to modify any behavior inside of the default HUD.
Custom campaign
If you want to base your gamemode on the default Campaign
gamemode, setting it up is as smple as inheriting from Campaign
:
[GameMode] class MyOwnCampaign : Campaign { MyOwnCampaign(Scene@ scene) { super(scene); } }
Your gamemode will then work exactly like the official Campaign gamemode. You can then override methods to modify any of the gamemode behavior.
Versus
We provide a few base gamemodes for versus, in particular VersusGameMode
and TeamVersusGameMode
. The former is for free-for-all based versus modes (like Deathmatch), and the latter is for team based versus modes (like Team Deathmatch).
//
Functions
Further, you can implement any function you'd like from the below list in your class, and they will be called as events.
void Start(uint8, SValue@, StartMode) SValue@ Save() void AddPlayer(uint8) void RemovePlayer(uint8, bool) void UpdateFrame(int, GameInput&, MenuInput&) void UpdatePausedFrame(int, GameInput&, MenuInput&) void PreRenderFrame(int) void RenderFrame(int, SpriteBatch&) void ResizeWindow(int, int, float) void Paused(bool) vec2 GetCameraPos(int) vec4 GetVignette(int) // Advanced methods void LobbyCreated(bool loadingSave) void LobbyInviteAccepted() void LobbyEntered() void LobbyDataUpdate() void LobbyMemberDataUpdate(uint8) void LobbyList(array<uint64>@) void SetMenuState(MenuState) bool MenuBack() void ShowMessage(MenuMessage) void OnBindingInput(ControllerType, int) void ChatMessage(uint8, string)