Twitch integration with Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] or even if you wanna pet me you can also give me a little [Music] hello i'm paulo this way here is milo today we're gonna do a tutorial on how to integrate twitch chat into your game so how this works is basically twitch has an rc chat and we are going to connect to it with a little protocol and then basically any text that comes in or out we're going to read from that and then we can use that text in any way we want in our game so to get started let's just create an empty object over here let's call them uh twitch connect and then let's add the same for a script twitch connect so we get rid of the start function with we're not going to need it and then we add a couple includes uh first for system.net.sockets which is what we'll have the tcp client and then for system io which will have the stream reader and right uh we declare a few variables here this is client twitch and that's what we're gonna use to connect to drc and then stream reader and steamrider those are the ones that we're gonna connect to our to our tcp so they can actually read and write text and we're going to need the url for twitch which is irc.chat.twitch.tv [Music] and the port which is 6667 let's create a function here to connect to twitch and let's declare the twitch equals new tcp client passing the url on the port [Music] and then declare the stream reader and stream writer and then connect those to the stream that you get from torch and then scroll away and connect to twitch from there so at this point you're gonna need an actual twitch account to be able to log on to the chat uh for this i just made a temporary one that i'm just gonna throw away so but you should basically make one for this i recommend you don't use your normal twitch account because you know there's a few security vulnerabilities that you're gonna expose yourself to rather it'd be just a throwaway account that doesn't really matter so in this case i made domo arigato mis roboto which i'm very surprised it wasn't taken but oh well and then you're going to need what's called an authorization and this is essentially like credentials to log in and to get them you can use this website which is twitchapps.com tmi and in there basically it will basically ask you for you to sign up and to approve certain permissions and that will give you that authorization string now again that authorization is almost as good as a password so you really don't want to expose it the only reason i have it here right now is just because it is kind of a throwaway account that i'm never going to ever use again so i don't really care too much what happens to it but you should care for yours and then you're gonna need which channel do you want to connect to in this case i'm just gonna use my own channel pablo so now we're actually going to connect so we use writer.writeline and then we're going to pass the password and then that there goes the authorization then we're going to pass our nic which is the nickname and that's where our user goes and then we say join hashtag and then the name of the channel now the channel and the username need to be in lowercase so we just add a two lower at the end and we're good to go and once you have all this you can call writer.flush and this will actually send all the stuff that you wrote to the tcp so that it actually connects and now in our update we're going to check if twitch dot available greater than zero and that basically just means that there is something available in the tcp that we can grab with our stream reader and then we're just going to say string message equals reader.readline which will just read the next available line in our tcp then we just print it just so that we can debug it and let's go from there and now we look at the log in the game you can see we have a tmi.twitchtv001 don't worry gotta meet roboto welcome good luck have fun so that's like a little intro text that you get from from twitch when you connect to your servers and now in chat i do like hello hello and then i send it you can see now in my output i can i get your hostess dmi.twitch.tv this server is rather new it really isn't they've been saying that for years you're an amazing twisty package passages all alike i don't know they got philosophical there but sure um and then below you can see like a call to join that's the joint to the channel and then over there is um like an entry for the connection to that port connection to the names and then there's a message that got sent by pablo poon which is me hello hello a couple little extras here just to maintain the connection to twitch basically we're gonna do if twitch is not connected then call connector twitch so it reconnects and then also uh we're gonna add a ping counter and this basically just how often do you wanna ping and then we're gonna say if ping counter is greater than 60 so if 60 seconds have passed then send the ping to the url and that kind of keeps the connection basically alive if you haven't messaged anything in a while so now that we get messages we can actually check for if it is an actual message written by a chatter and for that we're just going to say message.contains prepmessage and that's basically a code that will come with any message that it's been written by a user and then over here inside i'm gonna put in a little comment just showing like a a print of everything you get when you get a private message as you can see you get quite a bit of information there and then we're gonna parse it and for doing that we're gonna say message.indexof exclamation point if you look at the example string you can see it's pablopun exclamation point so we can isolate the username from that then we can say string chatter equals and then message.substring one comma split point minus one so i'll basically just extract that first word bamboopoon and then we say search for message index of the colon and that you can see at the very end of the message it just says colon hey so basically if everything after that colon will be the actual message the user typed so we grab string message equals message.substring split point plus one so basically anything past that colon bring it in in that string so to use that user and a message we're going to create an event we go to the top using unityengine.events we're going to need that and then let's declare a public unity event give it the type string comma string so basically it's going to pass two strings along let's call it on chat message then when we receive the private message we say on chat message and then dot inbox and then we pass chatter and message so when you want to connect something to a chat message you can just look at it in the editor or you can do it through code as well and then just add here and i'm going to drag this fruit bender that i have and in fruit bender i just declared an on chat message in it the code for my fruit bender is very very simple it just says whenever you get a message check if it has exclamation fruit in it and if it does then create an instance of fruit which i'll show you what that does in a moment and then just let the fruit know who the chatter is and what the message is and then over here we can do exclamation fruit fresa and there you go it just creates a little brussel with my name fruit coconut and it creates one and then for example if you do exclamation lalala then just type something nothing happens because the only the only closet listening to this first checks that there's fruit um so let's do a few for it random i want to copy this so i can spam it a little bit so you can imagine interactions like that with your chat you can do all sorts of things this is another example this is something i'm working on right now and if you ever want to watch it in stream i'm actually working on this at the moment and it's essentially a way to play chess versus chat so for example i can make my move here and now it's chad's turn so chad can do exclamation chess and then they can move any piece for example let's do um let's do a b8 and move it to c6 and you can see now that moved over there and now it's my turn and basically we can go back and forth playing me versus chat and this can be useful not only for like games and stuff but you can do it for general interactions for example here i have exclamation pack which i do in a very similar way so that brings out the hat too so you can pet uh milo or even if you wanna pet me you can also give me a little hello so as you can see there's a ton you can do with this you can do chat playing a game you can do chat influencing the game you're making you can also be for example the interactions with the stream you can use it for all sorts of things um so yeah if you want to watch more of this i actually am working on the chess versus chat thing on stream on mondays wednesdays and saturdays and it will be for the next few weeks or so and also remember to like like and subscribe and leave a comment and post the algorithm to the moon and all of that stuff i'll see you on the next one adios [Music] [Music] well thanks for stopping by don't forget to like subscribe and hit the bell to be notified when new videos come out thanks for watching
Info
Channel: PabloMakes
Views: 13,608
Rating: undefined out of 5
Keywords: game, videogame, gamedev, tutorial, unity, script, design, c#, UI, menu, interact, interface, dev, programming, twitch, integration, chat, code, coding
Id: QG3VIUFcif0
Channel Id: undefined
Length: 10min 48sec (648 seconds)
Published: Wed Mar 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.