How To Connect Using Steam - Unity Multiplayer Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

neat

👍︎︎ 2 👤︎︎ u/skellytongs 📅︎︎ Aug 29 2020 🗫︎ replies
Captions
okay so hello welcome back to another unity multiplayer tutorial this video has been requested a lot over the last few weeks and couple of months people have been wanting to know for a while how to either connect through steam to play with their friends or how to even play their friends at all some people have tried putting in their ip addresses and it just doesn't work and that's because you need to know about port forwarding some people either don't know or they want to you know have an easier way of doing it or also maybe they just don't have access to change their router settings so there's plenty of reasons why you can't just use the normal method so i'm going to be showing you how to use steem as a really simple way to have the data transfer in your game so you don't have to port forward and you can easily just play with your friends and invite them and just use the steam api i hope you're looking forward to it let's get started so i think the best place to start will be showing you what i've got in my project it's nothing fancy as i said this video is focused on getting the steamworks working and playing my friend across steam so the project doesn't really matter but what i've got is from last video uh the notification thing where i can right click on my network manager send notification and it'll send this message to all the clients and they'll display it on their ui so when i start the game if i go to uh back here i've got this other scene let's just save this i've got this other scene with a text component down here and whenever a message is sent from the server the clients will receive it and display it here like this so they'll just keep putting their messages on here and that works as it is right now using telepathy which if we look back at the network manager is the default transport but we want to use steam instead so let's get started with that so you might recognize the steamworks website because i did a video a few weeks ago i think where i showed you how to read your name from steam or to you know grab various bits of information from steam using the the api and we didn't actually use steamworks directly because it's written c plus plus and we can't just add it to our project and have it work easily but someone has made or some people have made steamworks.net which is what we actually used which has some kind of it's just the the steamworks api c plus plus but it's a wrapper in c sharp so you can use all the same things but with c-sharp and it's very easy to just drop it in your unity project and have it work so we could just download steamworks.net from here and chuck it in our project but the problem is we then are missing all the mirror things that we need luckily for us if we go to the mirror website and we see down here it says multiple transports such as and in steam now we're using steamworks.net there is also face punch but i've never never messed with that so let's go steamworks.net and it tells us here you can get the release here with the latest version of steamworks included so we basically get steamworks and the mirror stuff needed all in one download clicking here it's their release on github so we can just download the unity package give that a second and now that that's done i can click on it and i'm assuming you already have mirror installed like the normal mirror stuff because what this is doing is this is just adding all the extra things to it so if i press import now and give that a second now that that's done we can go back to assets mirror and you'll see all the normal mirror stuff plus inside runtime transport we also have fizzy steamworks and then all the steamworks now and to to use this all we need to do is go to our network manager and remove telepathy and add steamworks or fizzy steamworks it'll actually add the steam manager which is just for connection between our unity game and steam and these settings you don't have to worry about right now i can do more videos in the future on this this is just going to be a simple setup make sure that your network manager actually has the transport there and then if we save it's almost good to go i can actually start a host right now and it will technically start a host using steam the problem is there's no easy way for us to connect and there's no way uh on my steam friends list for my friends to right click join or for me to right click invite them none of that is currently supported we have to code that so we'll jump into the code in a second but before that one last thing to mention is under the network manager i've got this buttons ui which is just a game object then with a canvas for this button and the button currently has no callback but it will in a minute when we get it to start host the reason i'm not using the the default ui is simply because that will not actually handle any of our steam lobby creation callbacks any of that stuff we need to do ourselves so i'm going to write my own code and have this button called that so just make a button and the reason we do it here is so that we can just turn it off when we're in the game so for the code let's go to our steamworks script and make a new c-sharp script and we'll call it like steam lobby and open it up so a quick warning is that this part might get a bit confusing it was confusing for me when i was learning it there wasn't much documentation on it i had to do a lot of trial and error before i got it working i've tried to keep it as simple as possible so i hope you can follow along but if you do need any help with what i'm doing here just feel free to ask down below but let's get started with it we want reference to our buttons game objects so we can turn it on and off that's quite simple just reference to a game object call it i don't know buttons so obviously i've already got the code for all this but rather than going down line by line it doesn't really make sense i feel like we should go in the order that like would actually be ran i feel like that makes the most sense so the first thing that would happen is in reality that we we want to host a lobby right so let's make a function for that we can call it on our button in the ui so let's make host lobby and all we'll do is we'll disable buttons right as soon as we press host we're assuming that we're going to be hosting a lobby so we'll turn off the button's game object i don't know why i said game object there let's let's set active false okay and then what we want to do is we want to tell steam steam has this thing called steam matchmaking so let's say steam matchmaking and we can create a lobby that's exactly what we want to do we want to create a lobby in steam and it first of all asks for the lobby type so a lobby type you know take your pick it's up to you so you can have friends only invisible private whatever and this all just changes how it shows up on the friends list so if it's friends only then quite obviously only your friends can join and see whereas you can make it private so that it's invite only and so on and so forth but i'm going to go for friends only and then how many connections are allowed to this lobby what's the max max members i'm just going to say the i want the max connections that the network manager has whatever i set in the network manager should be used here too so let's make a start method and we want to cache a private network manager let's just cache our network manager so in start we can say network manager equals get component network manager so we've got our network manager then down here we can say network manager dot max connections and let's zoom out for you guys all right so whatever the max connections is is also the same as the max lobby size in steam and that will tell steam to go start doing this thing but because it's obviously going to be doing that over over the internet then this could take however long right it's effectively asynchronous but it's not actually a synchronous we're not using async code where we await here or we're not using code routines we're just saying do this thing and then as far as our code's aware nothing else happens but then once steam has actually created our lobby for us it will trigger a callback so we can actually listen for a callback for when the lobby is created and we can then start the next part of our code so to do callbacks in steam it's kind of weird syntax but you'll get used to it so first of all let's say well if steam manager dot initialize let's if if not then return so if if steam isn't open on the machine then don't even try and do this next bit because you'll just get loads of errors so we want to make a callback um so the way you do that in here is let's make a protected it has to be protected callback is the class since steamworks and the type you pass in will be whatever callback you're listening for so we're listening for lobby created you see this here so we'll just call it lobby created and in start is when we actually need to make it so let's say lobby created equals and then this is just the syntax as i said it's quite confusing it's a bit weird but it's call back then the same type so lobby created and then you have to call the create method like so oh it's being a bit weird let's see uh cannot be used like a method yeah i don't know why i did that okay so dot create now this is a method and it takes in a funk which is basically um a function right it's going to be what what's it going to call this just means what function are we going to call when this happens when a lobby is created what do we call so let's make a method called uh on lobby created and then it has to take in a lobby created um like this and that's basically gonna give us all the data about the lobby that was created so you'll see here it gives us this result and this id lobby and so on it's there's a lot here this is why i said it's very confusing but if we put in on lobby created into this function then now we successfully have a callback so that when steam creates the lobby we will then call this function down here now this actually happens even if it fails to make the lobby so let's say if callback.result which is an enum so if that is not and then there's all these different ones but the only the successful one is okay right so if it's not okay which is the one we want then we want to actually return and at the same time we want to re-enable the buttons so button start so active true and then down here we know the lobby was successfully created so what we can do at this point is we can actually say networkmanager.start host so we're now hosting a game and normally when you connect to a mirror game using the normal transport you put in an ip address as the network address but when using the steam transport you actually put in their steam id now we don't want people to manually type the steam id we can just do it for us like we can get the code to do it right that's that's the point of writing code so we can say now steam matchmaking oops steam matchmaking and on a lobby you can have data steam will actually store data for you on the lobby and i think this is usually just strings i'm not sure whatever data types i'm sure you can probably use ins and characters and bytes and whatever but we can store data uh on steam and what that what that means is in the lobby we can actually store the the id of the host and by doing that whenever someone joins the steam lobby they can then tell their network manager to connect that to that id might not sound that obvious right now but when we get to it'll make more sense so let's set some lobby data and that wants us to first of all tell steem what is the id of the lobby so what we can do is we we need it to be of this see steam id type so let's make a new see steam id and that takes in uh well there's a few different overloads but we want to just pass in the lobbyids as a yulong and that actually comes in this callback so if we say callback.uh steamlobbyid that's that's what we need so we basically told it now this is the the lobby that we want to set some data on and next is the key now the key is it's like a dictionary so for every bit of data in the lobby it will have a key and a value so the key is how you identify it and then the value is the data so the key uh let's actually store the key as a const so that we we don't mistype it later so let's say um up here private const string host address uh host address key equals like host address that's going to be our key and we can use that down here so our host address key and then the value what's what's the value of this well the value is going to be the person's actual id and it's going to be our id because we're the ones hosting so it's going to be our steam user dot get steam id and it needs to be as a string so to string okay i'm going to actually just separate that onto multiple lines so it's easier for you guys to read and there we go so we're saying for this lobby steam has created for us uh so this is the lobby this is going to be our key so the host address and this is the value our our id so anyone joins can say go get me the value in the lobby for our host address key which will give us back our steam id and at this point we've made our lobby but it's just kind of sitting there now we need people to join so steam allows us to do that either by inviting or by manually joining i can go to the person who's hosting right click and join and what those things both do is they trigger an on game lobby join request so it's a long long thing but we have to do it that way so that's just another callback so what we can do is we can say protect it let's just copy paste this honestly so it's a callback of type game lobby join requested and we can call it game lobby join requested and what we can do is we can then go down to here and whoops in the start we can copy paste and we can say game lobby join requested of type game lobby join requested and then we want a function for this so we can say on game lobby join requested and then we can go make that method down here so we can say private void on game lobby all right so long it's a long a lot of typing here join requested and this takes in uh the game lobby join requested and let's call this callback finally okay the next part is to join the lobby so this is just a one-liner all of this just for one line to say steam matchmaking um team matchmaking.join lobby and it wants the the lobby id which luckily for us is in this callback.lobbyid so now we can uh join that lobby now let's join joining this is all on steam this is this is all steam stuff whenever we say steam matchmaking it's all on steam but the end of the day we just want to connect through mirror so we need to grab the id of the owner so the final thing we need to listen for the final callback is the on lobby joined oh sorry on lobby entered callback so we need one more made it protected callback of type uh lobby joined sorry lobby i said it again lobby entered uh so game uh or we'll just say lobby entered then down here let's see it again lobby entered equals callback of lobby enter and then we'll create and we'll make a callback on lobby entered don't worry we're near the end now it's not much more um it's just this last function is all we have to write so private void on lobby entered lobby enter callback so the final bit of logic as i said is to go and find their id and then to actually join it now on lobby entered is also called for you as a host but you're not joining your own lobby in the same way it's a bit different so we're going to say if the network server dot active return if the server is active on this on this client then return because we're a host but if we're not if we're just a client connecting then we want to say let's go grab their host address we want to go find it so to find it let's store it in a string variable host address and it's steam matchmaking now just like i keep steam matchmaking uh we had the set lobby data we also have get lobby data that wants us to pass in the lobby id which is when we enter the lobby we can get it from the callback so callback dot uh lobby id but that says a yulong we need it as a see steam id this this thing from up here so let's just say like this let's also put it onto new lines so we don't get too too long so that's the person's uh the lobby id sorry then we also want as well as the the lobby id the key so the key is the uh address key and that will actually return to us finally the the actual value as we see here which is the host address it returns a string going to be our host address and finally we now just need to say to our network manager to set the address equal to this host address and then once we've set it we can then become a client so start client and finally let's just turn the ui off and we're done i'm going to have a quick run through the code because it was very confusing and apparently i did something wrong here so lobby entered sorry so what the code does is we um we make a lobby right we host a lobby by pressing the button so on steam servers it will make us a a lobby of steam users and then when it's created if it's not okay then we stop but if it is okay we become a host in mirror and then we also set some data in in the steam in the steam lobby to be our steam id because we're the host of the lobby so then when someone requests to join they all join we don't want anything else in between this is where we could add extra logic for someone joining our lobby but we're fine to just have them join and then when you enter a lobby if you are a server aka if you're the host you're joining your own lobby you don't need to do any of this but if you're someone else joining then you want to say go to that data steam is storing about this lobby and go grab the host address key and with that host address set it in mirror and start a client which will then connect to this address using the steam transport and then turn off the ui so at the end of the day it's not that much code it's only 70 lines and obviously that's including all the spaces and the bit at the top and i can you know get rid of some of those usings and so on and so forth let's head back into unity i said i said before this is going to be very confusing and i'm hoping i don't lose too many people but at the end of the day if you really need this and it's a bit too advanced you can just go ahead and copy paste it i'm usually against copy pasting code but yeah again this is just something to do some connection and it's it's really confusing anyway but at least it works so if i go to the network manager and add on my steam lobby script i just need reference to the button or the buttons game object and to be honest this should all work uh once we actually set the button callback to the network manager game object to tell the lobby to host the lobby so when we press that button it will trigger all that stuff happening and the final thing we need to do is build and run so let's let's build the game so now the game is built you see it here but the problem is if i press shift tab which those of you who use steam will know what brings up the steam overlay it doesn't actually work and that's because steam hasn't recognized this as a steam game so to do that what we'll do is we will go back to unity and we'll open up our builds folder so this is where our build is our game here we can go back to steam here's my steam obviously we're not playing mirror's edge we're playing this mirror project so if i click add game add a non-steam game i can then press browse go to the build and click on the executable and open it and then press add selected programs and now it's actually recognizing my mirror multiplayer tutorials as a steam game so if i hit play on steam it'll open up just like normal but now it'll say the steam community and if i press shift tab the whole steam stuff is working here so now i'm going to show inviting my friend so if i press shift tab you see my friend over here is playing the game it's called space war that's just the the default name for a steam game when you're using steamworks if you now right click there's no way for join game or invite game or any of that but if i hit start host the game will now go to the the other scene then if i press shift tab i can actually uh invite to game i'll press invite an invite has been sent and if i now go over to discord you'll see he right clicks on join game and it takes a second and then once it's done he'll actually be in the scene with me inside unity so now to show you that the code i already written still works what i can do is i can show you in the editor so i have my scene here and he's connected you can see here he's in the scene nothing's on the ui and if i now right click on my network manager and send the notification so the high goes through you see hi here if i now go over to discord you'll see higher has a pid on his screen and you know just to show this is still actually working in real time i can spam a message right click send notification and if i go over here it shows up on his screen so now we're actually playing a game connected through steam and it all works just fine so yeah that's it for this video i hope you enjoyed if you did please leave a like and subscribe especially if it was useful because i know a lot of people have really been wanting this and have really been struggling it also took me quite a bit to prepare this and get it right i still think the coding is a bit confusing i tried to do my best and explain it if you're still struggling obviously ask down below and you can get access to the code yourself on my github under the repository mirror multiplayer tutorials i hope it makes sense and if any of you have any you know improvements to this or any other suggestions for content like this let me know down below thanks always for watching and i'll see you in the next one goodbye but of course before i go i've got to thank my patreon special thanks to taylor rustio francisco lira liz kimber jake nixon benjamin hilder sam marcus matt freya ln fabian renault malvin john salig david mcdermott exit berdot dustin miller rack euros letter and renee if anyone else is able to help support the channel monetarily link to my patreons down below if not there are links down below to other social media such as twitch twitter and discord if you could help us out by following on any of those or checking any of those out that'd be greatly appreciated i'll see you in the next one and bye
Info
Channel: Dapper Dino
Views: 49,078
Rating: undefined out of 5
Keywords: Get start with Unity, Get started with game development, How to make a game, What is a game engine, Unity tutorials, Game design, Game developer, Game designer, Video game developer, How to make games, Game development software, C# Game development, How to, Dapper dino, What is Unity, Animation, Animator, C#, Coding, Programming, Multiplayer, Shader, Material, Shader graph, Visual, AI, ML, Scripting, Mirror, Server, Client, Rpc, Unity networking, Unity multiplayer, Steam, Steamworks, Friends
Id: QlbBC07dqnE
Channel Id: undefined
Length: 21min 44sec (1304 seconds)
Published: Sat Aug 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.