Getting started with Events
The events an Alt:SA gamemode reacts to, when each one runs and what you should do in it.
Introduction#
Events are how the server tells your gamemode that something happened. You never poll: you override the events you care about, and the server calls them on the main thread, one at a time. Everything you don't override has a safe default, and an exception in an event is logged without stopping the server.
The events#
| Event | When it runs | Typical use |
|---|---|---|
OnServerStart() | Once, after the world has loaded and before any player is handed to you | Load settings, prepare data |
OnPlayerJoined(Player) | A player has connected and sent their name; not spawned yet if SpawnOnJoin is false | Sign-in, welcome screen, spawn |
OnPlayerLeft(Player) | A player disconnects; the object is still readable | Save their progress |
OnTick(float) | Every server frame | Timers and light checks |
OnServerStop() | Before shutdown; storage is saved after it returns | Final saves |
Using events#
Welcome a player#
With the default SpawnOnJoin, players are already spawning when they join:
1public override void OnPlayerJoined(Player player)2{3 player.Toast($"Welcome, {player.Name}", ToastKind.Success);4}Remember something when they leave#
1public override void OnPlayerLeft(Player player)2{3 var p = player.Position;4 Bucket.For(player).Set("lastPosition", new[] { p.X, p.Y, p.Z });5}Run something every few seconds#
OnTick runs often, so keep it light: count time and act only when it adds up.
1float sinceSave;2 3public override void OnTick(float deltaSeconds)4{5 sinceSave += deltaSeconds;6 if (sinceSave < 30f) return;7 sinceSave = 0f;8 foreach (var p in Players)9 if (p.IsSpawned) Bucket.For(p).Set("lastSeen", System.DateTime.UtcNow);10}UI callbacks#
Buttons on a UI panel take a callback. It runs on the server when the player presses the button, after the panel's field values have been updated, so it can use everything a gamemode can.
Next step#
Learn how players type commands in Getting started with Commands.