Architecture

// Config: Engine - [], Toolkit - [v1.0.0]

One scene #

The demo is the single scene Scenes/Discord Demo (Template).unity. It has two views under one canvas, and DemoFlow shows one at a time:

ObjectHoldsShown when
MenuAccount, friends and lobby panelsNot in a session
SessionThe arena, the stage that spawns capsules, and the session barIn a session

The always-present objects are the camera (with DemoFollowCamera), the EventSystem, the Discord Social object, the Invite Prompt and the Tooltip.

The Discord Social object #

One object carries the demo’s components. Discord Social itself is a subsystem, so it is not in the scene: with the Application Id set it starts on its own, and the settings (start mode, remember sign in) are in Project Settings, Subsystems, Discord Social.

ComponentJob
DemoSetupCheckShows a “Setup needed” box while no Application Id is set.
PlaceholderNetwork (a DemoNetwork)The networking seam. Replace it with your own.
DiscordLobbySessionThe lobby: create, join, leave, announce the session.
DemoFlowMoves between menu and session.

Order of events #

  1. Play. The Discord Social subsystem initialises the client with the Application Id from Project Settings. With Remember Sign In on (the default) it uses a token store that keeps the token encrypted in PlayerPrefs, and signs in again with the saved token, refreshing it when expired. With no saved token the player presses Sign in.
  2. Sign in. DiscordSocialApp.Connect() opens the browser to authorise, then Discord calls back, the client connects and the status becomes Ready. DiscordConnectionStatus reflects the status each frame. DiscordUserProfile (the “You” row) shows the local user. DiscordUserList fills with friends grouped Online and Offline. DiscordFriendRequestList fills with received and sent requests.
  3. Create or join. Create lobby calls DiscordLobbySession.Create, which creates a lobby with a random secret and the host’s user id in the lobby metadata (heathen.host). Join takes a secret, or the player accepts an invite. In both cases the player’s activity is set to the lobby so friends can be invited.
  4. Invite. Invite on a friend row calls Invite on that user. The friend’s game shows DiscordInvitePrompt; accepting calls DiscordLobbySession.AcceptInvite, which joins using the secret from the invite.
  5. Start session (host). DemoFlow.StartSession calls DemoNetwork.StartHost(), which returns the address clients connect to. It captures the lobby members into DemoWhitelist, then DiscordLobbySession.StartSession(address) sends a lobby message with metadata heathen.session=start and heathen.address=<address>. The host switches to the session view.
  6. Announcement (members). DiscordLobbySession receives the message, ignores it unless it came from the host, and raises OnSessionStarted(address). DemoFlow.HandleSessionStarted calls DemoNetwork.StartClient(address), captures the whitelist and switches to the session view.
  7. Session. DemoSessionStage spawns one capsule per lobby member. The local member’s capsule has DemoPawn enabled and the camera follows it.
  8. Leave session. DemoFlow.LeaveSession calls DemoNetwork.Stop(), clears the whitelist and returns to the menu. The lobby is kept so the group can play again. Leave lobby leaves the lobby and clears the player’s activity.

Who owns what #

  • Discord owns identity, relationships, presence, the lobby and the invite.
  • The Foundation (DiscordSocialApp) owns the client, the token session and every operation.
  • The Toolkit components own the interface.
  • Your DemoNetwork owns the connection and the replication of the session.

Rate This Article!