React to players and send messages
Use the session events, the player list, notices and messages. Side Hustle runs the Steam lobby, so you need no netcode of your own.
You need: a registered gamemode. See Add a gamemode.
-
Subscribe to the session events once, in the same class as your descriptor.
API.SessionStarted += ctx =>{if (ctx.Descriptor.Id != "you.yourmode") return; // fires for every gamemodeLog.Msg($"started as {ctx.Role} with {API.Players.Count} player(s)");};API.SessionEnded += id => { if (id == "you.yourmode") Log.Msg("ended"); };SessionStartedfires after your launch callback returns.API.CurrentSessionandAPI.Playersare set by then.SessionEndedfires once, right afterOnExitToHub. It also fires for a failed or aborted session.ctx.RoleisHost,ClientorSingleplayer.
-
Track players.
API.PlayerJoined += p => Log.Msg($"joined: {p}");API.PlayerLeft += p => Log.Msg($"left: {p}");- Players who are in the lobby at the start are in
API.Players. They do not raisePlayerJoined. - The list is polled about twice a second. In a World session,
PlayerJoinedfires before the character spawns. PlayerLeftdoes not fire for the remaining players when the session ends.SessionPlayerhasSteamId,DisplayName,IsHostandIsLocal.
- Players who are in the lobby at the start are in
-
Show a notice to the local player.
API.ShowNotice("Round 2 starts in 10 seconds.", 5f);In the world this is the game’s notification popup. In the menu it is the hub’s toast, with a fixed duration.
-
Send messages to the other players.
API.SendToAll("you.yourmode", "score", "red:3,blue:1");API.SendTo(steamId, "you.yourmode", "role", "seeker");- Messages go to the other players only, never to yourself.
- Delivery is reliable. A payload is at most
API.MaxMessageBytesbytes. falsemeans no session lobby or a refused message. The reason is in the log.
-
Receive messages.
API.OnMessage("you.yourmode", msg =>{if (msg.Channel == "score") ApplyScore(msg.Text);});Handlers run on the main thread. Only members of the session lobby can send to you.
SenderSteamIdcomes from Steam, not from the payload. -
Optional, host only: remove a player.
API.KickPlayer(steamId, "Too many players");It never kicks the host. On a client it does nothing.
All calls are safe from any thread. Calls off the main thread run on the next frame.
Check it works
Section titled “Check it works”- Host your gamemode and join with a second instance.
- The host log shows
joined:with the client’s name. - Send a message from the client. The host’s handler runs with the client’s
SenderSteamId.
Run on Side Hustle 2.x too
Section titled “Run on Side Hustle 2.x too”These members need Side Hustle 3.0. On an older Side Hustle they are missing, and a call fails. Ask for each feature by name before you use it:
if (SideHustleLink.Has("session-events") && SideHustleLink.Has("messages")) SideHustleLink.TryCall(() => Gamemode.Subscribe(), "lifecycle events", LoggerInstance);The names are the constants on ApiFeatures, for example players, notices and messages.