#Get your Discord Integration in your PowerQuest games here!

1 messages · Page 1 of 1 (latest)

elfin frost
#

It's really easy to add Discord integration into your PQ games...
You want this because every player of your game that has Discord open will be telling all their friends that they are playing your game, which might make them what to look it up and play it as well.

  1. Download the Discord SDK here: https://dl-game-sdk.discordapp.net/3.2.1/discord_game_sdk.zip
  2. Follow this step to setup your account and application ready to integrate: https://discord.com/developers/docs/game-sdk/sdk-starter-guide#get-set-up
  3. Open up that SDK zip that you downloaded.
  4. Copy the contents of the lib/ folder to Assets/Plugins in your Unity project
  5. Copy the contents of the csharp/ folder to Assets/Plugins/DiscordGameSDK
  6. Create a new game object in your first scene - probably your title room and call it "DiscordManager"
  7. Create a new c# file with the below code and name it: DiscordManager.cs
  8. Edit DiscordManager.cs and change the applicationID to your new application id from the Discord portal step above. Also worth editing all the strings at the top of the file to suit your game or else it will look like you are playing my game 😛
  9. Add the script to your game object and wallah! After a few seconds, if you have Discord open, you should see your status update to be playing your game. Which is what it will look like for everyone else who plays your game and has Discord open.
Discord Developer Portal

Integrate your service with Discord — whether it's a bot or a game or whatever your wildest imagination can come up with.

#
using System;
using Discord;
using UnityEngine;
using UnityEngine.SceneManagement;

public class DiscordManager : MonoSingleton<DiscordManager>
{
    public string applicationID = "YOURAPPIDHERE";
    public string details = "Playing Frontier Fugutive";
    public string state = "Solo";
    public string largeImage = "discord_splash";
    public string largeText = "Frontier Fugitive";
    public string smallImage = "favicon1024";
    public string smallText = "Ben Blake";
    private long time;
    private Discord.Discord discord;

    private void Start()
    {
        try
        {
            discord = new Discord.Discord(Int64.Parse(applicationID), (UInt64)CreateFlags.NoRequireDiscord);
            discord.SetLogHook(LogLevel.Debug, (level, message) =>
            {
                Debug.Log(level.ToString() + message.ToString());
            });
            time = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            InvokeRepeating("UpdateDiscord", 3f, 20f);
        }
        catch
        {
            Debug.Log("Discord not found");
        }
    }

    private void UpdateDiscord()
    {
        try
        {
            if (discord == null) return;
            discord.RunCallbacks();
            UpdateStatus();
        }
        catch
        {
            Debug.Log("Discord crashed");
        }
    }

    void OnApplicationQuit()
    {
        if (discord == null) return;
        discord.Dispose();
    }```
#
    {
        try
        {
            if (discord == null) return;
            ActivityManager activityManager = discord.GetActivityManager();
            state = "Solo";
            if (SceneManager.GetActiveScene().name == "SceneRoomDesert")
            {
                details = "In the desert";
            }
            else if (SceneManager.GetActiveScene().name == "SceneRoomOverlookTown")
            {
                details = "Near Bordertown";
            }
            else if (SceneManager.GetActiveScene().name == "SceneRoomTown")
            {
                details = "In Bordertown";
            }
            else if (SceneManager.GetActiveScene().name == "SceneRoomStoreCreek")
            {
                details = "At the General Store";
            }
            else if (SceneManager.GetActiveScene().name == "SceneRoomStoreCellar")
            {
                details = "In the Cellar";
            }
            else if (SceneManager.GetActiveScene().name == "SceneRoomCredits")
            {
                details = "Finished Frontier Fugutive";
            }
            else
            {
                details = "Preparing to start";
            }```
#
            {
                Details = details,
                State = state,
                Assets =
                {
                    LargeImage = largeImage,
                    LargeText = largeText,
                    SmallImage = smallImage,
                    SmallText = smallText,
                },
                Timestamps =
                {
                    Start = time
                },
                Instance = true,
            };
            activityManager.UpdateActivity(activity, res =>
            {
                Debug.Log("Update Activity: " + res.ToString());
            });
            var imageManager = discord.GetImageManager();
            var userManager = discord.GetUserManager();
        }
        catch
        {
            Debug.Log("Discord crashed");
        }
    }
}```
#

I'll share my MonoSingleton code here as well because I am using it above and you might not have one in place already. This simply forces that gameobject you attach the DiscordManager to, to persist across your scenes while the game is running.

#

using System;
using UnityEngine;

public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
{
    [Header("MonoSingleton Settings")]
    public bool destroyOnLoad;

    public static bool applicationIsQuitting = false;

    private static object _lock = new object();

    protected static T m_Instance = (T)((object)null);

    public static T instance
    {
        get
        {
            if (MonoSingleton<T>.applicationIsQuitting)
            {
                Debug.LogWarning("[Singleton] Instance '" + typeof(T) + "' already destroyed on application quit. Won't create again - returning null.");
                return (T)((object)null);
            }
            object @lock = MonoSingleton<T>._lock;
            T instance;
            lock (@lock)
            {
                if (MonoSingleton<T>.m_Instance == null)
                {
                    MonoSingleton<T>.m_Instance = (UnityEngine.Object.FindObjectOfType(typeof(T)) as T);
                    if (MonoSingleton<T>.m_Instance == null)
                    {
                        Debug.Log("No instance of " + typeof(T).ToString() + ", a temporary one is created.");
                        MonoSingleton<T>.m_Instance = new GameObject(typeof(T).ToString(), new Type[]
                        {
                            typeof(T)
                        }).GetComponent<T>();
                        if (MonoSingleton<T>.m_Instance == null)
                        {
                            Debug.LogError("Problem during the creation of " + typeof(T).ToString());
                        }
                    }
                }
                instance = MonoSingleton<T>.m_Instance;
            }
            return instance;
        }
    }```