KROYSDOCS
HomeWhat's newv0.1.0

GameMode

The game's rules on the dedicated server, written in C#. One instance runs per server (start the server with --game-mode Namespace.TypeName); it is not attached to an entity. It receives the session events before its OnFixedUpdate each server update, owns the match state and adds and drives bots. Game modes are server-only.

Kindclass
NamespaceKroys.EngineScripting
Members5
SourceGameSession.cs
EXAMPLE

In a script

A complete script that uses GameMode; copy it into your scripts project.

Multiplayer.cs
// Start the server with: KroysDedicatedServer ... --game-mode DocExamples.Api.KingOfTheHill
public sealed class KingOfTheHill : GameMode
{
    private readonly Dictionary<uint, float> secondsOnHill = new();
    private float matchSeconds;

    protected override void OnStart()
    {
        GameSession.MatchState = "Warmup";
        // Rules come from GameRules.kserverrules or --rule bots=2.
        for (int index = 0; index < GameSession.GetSetting("bots", 0); ++index)
            GameSession.AddBot($"Bot {index + 1}");
    }

    protected override void OnPlayerJoined(Player player) => secondsOnHill[player.ClientId] = 0.0f;

    protected override void OnPlayerLeft(Player player) => secondsOnHill.Remove(player.ClientId);

    protected override void OnPlayerReadyChanged(Player player, bool ready)
    {
        if (GameSession.MatchState == "Warmup" && GameSession.AllPlayersReady)
            GameSession.MatchState = "Playing";   // clients receive Kroys.Session.MatchState
    }

    protected override void OnFixedUpdate(float fixedDeltaTime)
    {
        if (GameSession.MatchState != "Playing") return;
        matchSeconds += fixedDeltaTime;
        foreach (Player player in GameSession.Players)
        {
            Vector3 position = player.Entity.Transform.WorldPosition;
            if (Vector3.Distance(position, Vector3.Zero) < 5.0f)
                secondsOnHill[player.ClientId] += fixedDeltaTime;
        }
        if (matchSeconds > GameSession.GetSetting("time_limit", 300.0f))
            GameSession.MatchState = "Ended";
    }

    protected override void OnMatchStateChanged(string state) => Debug.Log($"match state: {state}");
}
REFERENCE

Members

Callbacks you override
#protected virtual void OnPlayerJoined(Player player)
A client connected or a bot was added.
callbackprotected
#protected virtual void OnPlayerLeft(Player player)
A client disconnected (or was kicked) or a bot was removed. The player is no longer in the session.
callbackprotected
#protected virtual void OnPlayerReadyChanged(Player player, bool ready)
A player became ready or not ready.
callbackprotected
#protected virtual void OnTravelled(string map)
The server loaded another map (players are at its spawn points).
callbackprotected
#protected virtual void OnMatchStateChanged(string state)
The match state changed (also when this game mode set it).
callbackprotected
Signatures
protected virtual void OnPlayerJoined(Player player)
protected virtual void OnPlayerLeft(Player player)
protected virtual void OnPlayerReadyChanged(Player player, bool ready)
protected virtual void OnTravelled(string map)
protected virtual void OnMatchStateChanged(string state)

KroysEngine 0.1.0 documentation, built from the engine repository by Tools/BuildDocsSite.py.