Skip to content

Instantly share code, notes, and snippets.

@msciotti
Last active November 15, 2018 21:17
Show Gist options
  • Save msciotti/540466752e8ba46ba1c455da3d861915 to your computer and use it in GitHub Desktop.
Save msciotti/540466752e8ba46ba1c455da3d861915 to your computer and use it in GitHub Desktop.
Migrating from GameBridge to GameSDK

Intro

If you're reading this, woo! You've got access to Discord's new GameSDK. It does a lot of fancy stuff, which you can see over here. But, most importantly, it handles voice and text chat in a way that's much easier than GameBridge.

What Was GameBridge

So, how did GameBridge work? You had to:

  1. Integrate the SDK
  2. Manage all your own RPC connections, commands, events, etc.
  3. Do OAuth2 token exchanges to get, store, and refresh users' bearer tokens, requiring a backend server
  4. Create Group DMs with a different token and add all those users
  5. Manage individual voice settings via RPC commands and events
  6. Remember to kill Group DMs once you're done.

As many of you have completed or are working through integrations, you know that these are not necessarily simple tasks. When we deprecated GameBridge in August, we explained that we were unhappy with how difficult it had been. So, we made this new thing.

What Is the GameSDK

What do you have to do with the GameSDK? None of that.

In the GameSDK, we have the concept of "Lobbies". Lobbies are simply a collection of users in whatever way makes sense for your game: a group, a party, a raid, a lobby, a match, etc. Lobbies can include more than 10 people—which Group DMs couldn't!—and a user can be a member of up to five lobbies at once.

Lobbies include built in voice and text chat that work with native code functions as simple as SendLobbyMessage and ConnectVoice. That's it. That's all there is. No worrying about OAuth2 tokens and bot tokens and Group DMs and authenticating and RPC commands. Stick a bunch of people together in a lobby, and they can start chatting with text and voice!

There is one big difference when it comes to the new GameSDK: users must have Discord running on their machine for the SDK to work. The SDK itself is a very small stub DLL—a couple hundred kilobytes—that calls back to the local Discord client. That way, we can update on the fly without you needing to ship new DLLs. While GameBridge allowed the use of "guest" accounts, we found that it was not a great experience for users. Non-Discord users weren't sure where voice was coming from, and existing users were managing things like voice settings in ways that didn't feel like what they were used to in Discord. We understand this may cause some hiccups for some players, but we fully believe that the new SDK provides a better, more complete, cohesive experience. Especially when developers go above and beyond to fully make use of features like Rich Presence for game invites.

Also included in the GameSDK is a brand new way to manage voice settings, with a lot less work for you! This PR is landing really soon but for a sneak peek, look here. In short, we've put a ton of work into our new overlay, and we're using it as much as possible to make your lives easier. From within the overlay, a user can control voice settings that are unique to them in the context of your application:

They can also see when they're connected to a lobby voice chat and take voice actions exactly like they would in Discord:

All without any effort from you. All you need to do is call overlayManager.OpenVoiceSettings. We will be exposing the following as native code in the SDK as well:

  • PTT/VAD input mode
  • Self mute
  • Self deafen
  • Local mute
  • Local deafen

In case you want to create your own UI or keybinds around that.

Shut Up and Show Me the Code

We want to make sure that migration is as painless as possible. We'll be here to support along the way. This code example below shows just how simple it is to get the new SDK up and running. All of the documentation for Lobbies can be found in our documentation.

// First, we init the SDK. As you are not currently shipping your game on Discord, you'll want the NoRequireDiscord flag
// This means that we won't exit the game if Discord isn't running!
var discord = new Discord.Discord(myApplicationId, Discord.CreateFlags.NoRequireDiscord);

// Next, we want to create our managers. These are how you interact with the various parts of the SDK.
// For this, we'll want access to lobbies and the overlay
// We also want to keep a "secret" for our lobby, which we'll use later
var overlayManager = discord.GetOverlayManager();
var lobbyManager = discord.GetLobbyManager();
var secret = "";

// Let's pretend I'm the first person in the party. I'll make the lobby!
// First, we make a transaction for making a new lobby
// My lobby is for a 5 person party
var txn = lobbyManager.GetLobbyCreateTransaction();
txn.SetCapacity(5);
txn.SetType(Discord.LobbyType.Private);
lobbyManager.CreateLobby(txn, (Discord.Result result, ref Discord.Lobby lobby) =
{
  if (result == Discord.Result.Ok)
  {
    Console.WriteLine("I'm in the lobby! Its id is {0}", lobby.Id);
  }
  
  // Let's say hi to everyone, and then connect to voice
  lobbyManager.SendLobbyMessage(lobby.Id, System.Text.Encoding.UTF8.GetBytes("Hello!"));
  lobbyManager.ConnectVoice(lobby.Id, (resultA) =>
  {
    if (resultA == Discord.Result.Ok)
    {
      // I'm in voice! Let's let them adjust their settings.
      overlayManager.OpenVoiceSettings((resultB) => {});
    });
  )};
  
  // Lobbies include something called an "activity secret". This is a password to join the lobby
  // It's also super handy for use in conjunction with Rich Presence for joining people into a lobby
  secret = lobbyManager.GetLobbyActivitySecret(lobby.Id);
});

// So, how do I know when other people have sent me a chat message? Callbacks!
lobbyManager.OnMessage += (lobbyId, userId, data) =>
{
  Console.WriteLine("Message in Lobby {0} from User {1}: {2}", lobbyId, userId, System.Text.Encoding.UTF8.GetString(data));
}

// If I'm someone looking to join an existing lobby, I want to connect with the activity secret
lobbyManager.ConnectLobbyWithActivitySecret(secret, (Discord.Result resultC, ref Discord.Lobby lobbyA) =>
{
  if (resultC == Discord.Result.Ok)
  {
    // You're in! Start doing stuff!
  }
});

There's a bunch of other cool stuff that you can do with lobbies, like:

All this is available to you, as well as the rest of the SDK. Go nuts! And we're always here to help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment