How to build a Discord integration for your Unreal Engine project
(source: https://discord.com/developers/docs/rich-presence/overview)

How to build a Discord integration for your Unreal Engine project

What are we going to build

First, we will go through the groundwork needed to integrate the Discord SDK.

The setup is easy but lengthy. We will dive deeper into customizing the functionality in a follow-up thread.

Today we will cover a rich presence proof of concept.

Discord SDK

The Discord GameSDK is available at: https://discord.com/developers/docs/developer-tools/game-sdk

The DX (developer experience) is quite good.

Even the technical writer behind the documentation is proud of it:

Application setup

To get started head over to the developer portal: https://discord.com/developers/applications and create a new application.

Application Id

After you create the application, copy your Application Id from the portal. We will need it later on to initialize the SDK.

Downloading the GameSDK

Next up, go to the SDK docs (https://discord.com/developers/docs/developer-tools/game-sdk) and download one of the SDK versions.

I played around with both of them and they seem stable.

Experience taught me to always download the SDK labeled as stable, but you do you.

Creating the Unreal Engine plugin

Whenever prebuilt binaries are involved, I always opt to have at least 2 modules. In this case:

  1. DiscordCppSdk - to load, link, and integrate the binaries
  2. DiscordIntegration - normal runtime to access the functionality.

DiscordCppSdk structure

The DiscordCppSdk module lives in the Source folder and contains:

  • Binaries folder - holds all the prebuilt files for each platform. Currently, only Win64 which copied from the lib\x86_64 folder of the SDK
  • Build.cs file - we will examine it now

DiscordCppSdk Build.cs

This module takes care of the prebuilt binaries. To achieve we use:

  • PublicDelayLoadDLLs - loads the DLL during startup
  • PublicAdditionalLibraries - links the LIB during compilation
  • RuntimeDependencies - copies the DLL during build


DiscordIntegration structure

For the runtime module, we will need to copy the C++ files from the cpp folder of the SDK.

To keep things organized I copied all of them into a DiscordSDK folder.

DiscordIntegration Build.cs file

We have 2 critical here:

  • the DiscordCppSdk needs to be added as a dependency to ensure it is loaded
  • the DiscordIntegration path is added as an include path to simplify including files from the DiscordSDK folder

DiscordIntegration module files

Currently, the module file is almost empty.

The DLL loading is already handled by the DiscordCppSdk so we only need to define a barebone structure.

DiscordSubsystem

Here is where the real magic happens.

We have some callbacks to Initialize, Deinitialize, and Tick the subsystem.

Most importantly we have the Discord Core object which we will use to access the Discord SDK functionality.

Tick preparation

Before we dive into the Discord setup we will bind the the Tick function of the Subsystem so we can run the mandatory updates.


Initializing the Core object

Here we use our Application Id from step 4 as the Client Id to create a Core object instance.

I will store the result in an Unique pointer member but you can use it as a raw pointer.

Updating the activity

With all the setup behind us, we can run the much-awaited update for the discord Activity.

We will use some placeholder data here but feel free to go crazy.

The rest of the implementation

According to the SDK documentation, we should execute the RunCallbacks every frame, so we add it to our Tick function.

Finally, we will reset our Core unique pointer so it executes its own cleanup.

The Results

Finally, our discord activity gets properly updated during editor PIE or while running the packaged game.

We still have a lot of functionality we can leverage but that's a story for another day.

Source

Honestly, I don't know how you survived this long (unless you scrolled directly at the end).

If you want to give the Discord SDK a try, you can grab the source files for this thread at: https://github.com/outoftheboxplugins/DiscordIntegration


要查看或添加评论,请登录

Alexandru Oprea的更多文章

社区洞察

其他会员也浏览了