DevLog 20: Twitch integration in Death and Taxes


Hello peeps!

Oak here! As chief shitposter and coder extraordinaire, I'd like to talk to you a little bit about Twitch integration. I implemented a way for Twitch streamers to interact with their audiences in Death and Taxes, which involves a few checkboxes and a really neat little book.

QUICK REMINDER. We're going to be launching IN A MONTH (less than 30 days), so if you haven't followed us here on itch already, or haven't wishlisted us on Steam yet, do so now! Then you'll get notified when we release :3

You can conveniently enable it in the Options menu!


After that you can click on the book to vote! And when that's done, viewers can post messages in the chat to express their desire.

We thought it'd be a fun way to just give content creators a chance to bring their audience into the game with them. All of this is completely optional, of course. This feature was something that we had been discussing mostly "as a joke" last summer, but the more we talked about it the more sense it made. Eventually, we laid down some facts on how we could use it, prototyped it, did some very basic UX design for it, and then Leene produced dedicated art assets to give it a polished and in-world look. We didn't want to sacrifice immersion for the sake of having a "gimmick" (so to speak). We're quite pedantic when it comes to worldbuilding, and I think in this case it played out in our favour.

So how did we actually do this?

Luckily, it's quite simple and only takes VERY limited coding knowledge, so basically anyone could do it! Our engine of choice for Death and Taxes has been Unity, and we're running on the 2019.2.10f1 version. It's not the latest, but it's stable enough for our needs.

The steps you need to take to set everything up, if you're using Unity:

  1. Go to https://github.com/TwitchLib/TwitchLib.Unity and download the DLL there, the latest on at time of writing is 1.0.0: https://github.com/TwitchLib/TwitchLib.Unity/releases/tag/1.0.0 (TwitchLib.Unity.dll)
  2. Import the DLL into your Unity project (literally just drop it somewhere in your Plugins folder)
  3. Set up Twitch credentials so that you'd have a bot who does all the heavy lifting for you (reading chat messages, essentially)
  4. Create a class that manages your Twitch connection
  5. That's it.

This list seems short, and that's because it's quite simple to do. You can set all of this up within hours. The longest time it took for me at any single point was the third, as I was waiting for a verification e-mail from Twitch for about 20 minutes. Other than that, it was super fast. All you really have to do, is follow IMPORTANT ----> this guide <----- IMPORTANT and you'll be golden. The most important place where you really have to pay attention to what you're doing is the "SETTING THINGS UP" chapter. Just follow the guide line-by-line and you will be fine. I won't transcribe or re-iterate on what the guide does. Seriously, it's one of the best guides I've read. It really takes you through everything step-by-step.

You can do a lot of neat things with the library, but our design needed something very barebones. We just needed our bot to read the chat messages and search for the phrases that would count as a vote. To that end, I merely had to register to a message handler, which the library provides, which looks something like this:

    private void ClientReference_OnMessageReceived(object sender, TwitchLib.Client.Events.OnMessageReceivedArgs e)
    {
        Debug.Log("Sender: " + sender + "; " + e.ChatMessage.Username + " wrote: " + e.ChatMessage.Message + " is broadcaster: " + e.ChatMessage.IsBroadcaster);
        if(VoteCounter.instance.IsVoteInProgress())
        {
            if(e.ChatMessage.Message.ToLower().Contains(SaveManager.instance.CurrentOptions.StreamCommandDie.ToLower()))
            {
                VoteCounter.instance.RegisterVote(e.ChatMessage.Username, true);
            }
            if (e.ChatMessage.Message.ToLower().Contains(SaveManager.instance.CurrentOptions.StreamCommandLive.ToLower()))
            {
                VoteCounter.instance.RegisterVote(e.ChatMessage.Username, false);
            }
        }
    }


I have a very simple algorithm that the code is supposed to be doing:

  1. Check if a vote is in progress (clicking on the book starts a vote)
  2. Check whether the message contains the phrase that counts as a vote (which can be customized in the options menu) - and yes, I do realize that if you'd write both phrases into a single message then sparing someone would take the vote, but I'm writing code for cool people, not for trolls
  3. If there is a "vote phrase" in the message, register the vote
  4. If a vote by an user was already registered, replace it with the new vote (no multi-voting so spamming won't work)
  5. Update the visuals!

    public void RegisterVote(string username, bool die)
    {
        if (UserVoteMap.ContainsKey(username))
        {
            if (UserVoteMap[username])
            {
                DieVotes--;
            }
            else
            {
                LiveVotes--;
            }
            UserVoteMap[username] = die;
            if (UserVoteMap[username])
            {
                DieVotes++;
            }
            else
            {
                LiveVotes++;
            }
        }
        else
        {
            UserVoteMap.Add(username, die);
            TotalVotes++;
            if (die)
            {
                DieVotes++;
            }
            else
            {
                LiveVotes++;
            }
        }
        float liveRatio = (float)LiveVotes / TotalVotes;
        float dieRatio = (float)DieVotes / TotalVotes;
        Debug.Log("liveRatio: " + liveRatio);
        Debug.Log("dieRatio: " + dieRatio);
        Debug.Log("livePercent: " + Mathf.RoundToInt(liveRatio * 100.0f));
        Debug.Log("diePercent: " + Mathf.RoundToInt(dieRatio * 100.0f));
        SetScaleTargetAngle(Mathf.Lerp(-35, 35, dieRatio));
        TextLivePercentage.text = Mathf.RoundToInt(liveRatio * 100.0f) + "%";
        TextDiePercentage.text = Mathf.RoundToInt(dieRatio * 100.0f) + "%";
    }


This is just scratching the surface of what you could do with this library. Fortunately or unfortunately, Death and Taxes does not really have many points of interaction for community engagement in the game, other than the main mechanic. Implementing all of this took me about an hour, with around 15 minutes of testing on top. At first, I didn't have the voting phrases customizable, but I asked our streamer friends on Twitter and they said it'd be a nice thing to have. Adding that on top took me another hour, with additional testing and user input validation.

On top of that, using this kind of library is safe. You can also program the bot to send messages to the chat, but I left that out, as some streamers would have to grant the bot extra privileges to write to chat, plus there is no real need for it, as the streamer can call out the vote on-stream and also there is a visual indicator on the screen (the red book).

Just make sure you come up with secure credentials for your bot and enable two-factor authentication. You don't want to be losing accounts.

In action, all of it looks something like this:



Many thanks to the creators of TwitchLib, and Honest Dan Games for the awesome guide on how to get started with it! I hope that if you're ever making a game yourself, you found inspiration on how quickly you can add a neat little feature to share the fun!

And as always, thank you everyone for reading along! I hope you all have a wonderful day :)


Much love!

Death and Taxes <3


PS: *Release anxiety intensifies*

Get Death and Taxes

Buy Now$12.99 USD or more

Comments

Log in with itch.io to leave a comment.

(+1)

ah no way, i didnt know you guys had twitch integration
i wonder how many people on twitch didnt know that either??

There are quite a few who have used it already!