NetCode Update and Unity Game Services: Lobby and Relay

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so um welcome back everybody there we go sorry about that okay so last month we had talked about what was then mlapi and about three days later uh unity went ahead and launched their uh gaming services um initiative i guess you'd call it and uh they put a bunch of their services um out there that are most of them are currently still in beta but they've kind of um conglomerated them all into one service or a bunch of services and we'll be talking about um they renamed mlapi to now it'll be netcode for game objects they also have a for dots which is their uh data oriented tech stack um it's the similar name but they are they are completely different uh frameworks for this so we'll be going over the uh netcode i will also dip in a little bit into their relay service which is a peer-to-peer service that will handle like getting through nat services and stuff like that they also have a lobby system that's also in beta that is pretty easy to use we'll go through a little bit of how to set that up as well some of the other services that they've released um vivox is a in-game voice chat service that they purchased a few years back a lot of big name games uh use it for their in-game voice um multiplay is another service that they bought um is for hosting uh dedicated servers that is not quite public yet um apparently that is soon as is their matchmaker which is going to tie into uh multiplay if you were with us a few months ago i talked about how playfab does their matchmaking and server allocation things like that um they've also bundled uh their they have cloud code which is i believe also their uh cloud build i think is part of that as well uh there's cloud save the economy which is the in-game items and uh currencies and whatnot uh authentication we will actually be using a little bit of this because the relay service actually ties into their authentication service here uh we have on this side here like the analytics side remote config their cloud content delivery all this stuff has been out for a while um but they are now just bundling it into uh its own little tab here there's also the in-app purchases in-app uh ads i believe is is part of uh the monetize here but we are here to talk about upgrading from mlapi to netcode for game objects and they did break some stuff here from what we did last month but it is not um entirely unreasonable here so the first thing uh you'll need to do actually is go into your package manager and if you've had uh ml mlapi installed before uh you'll just need to uninstall it from the package manager and in here you click the little plus symbol here and you'll go to add package by name and i believe it is a com game objects game object leave it as game objects and you'll get a netcode for game objects here you'll also get a couple of samples here and if you did extensive work back in uh the mlapi days with like the network transform uh they've kind of changed how that works they've made that more of a well made totally server authoritative instead of client authoritative so before when you had your network transform you could move the player on your client and then it would tell the host or the server that you moved and then it would replicate that out to the rest of the of the clients um the way it is now that it is now uh server authoritative so you need to tell the server uh that you've moved so you'll basically use the remote procedure call to send a input to the server then the server will move and then it replicates itself out we'll look over that a little bit here because i had to change that up with a little demo we made uh last month but here in the samples there is a client network transform that will work exactly like the old network transform does uh you'll also want to install the lobby and the relay i think is uh this is here but it's also called something weird it is uh uh what is it called netcode adapter for utp is uh what you need to type in in the name here so the other big change that they did was they changed the namespace so here in our code wherever we had using mlapi all of these have gotten consolidated into unity.net code the other things that they've changed is a bit how network variables work so before when we had a a network variable for like a string or an int or something they had just a class that was network variable int or network variable string and now you just have to let's see do i have is sample here uh a little bit here so now instead of um using a was a network list but same is still here uh you just have to put it in the angle brackets here is what your type is and i guess since we're here network variables uh can't use strings so what they've done is in this unity.collections they've given us a unmanaged string class here for fixturing 32 bytes uh there's a number of these and we can actually see uh there's 32 i think there's 64 and 128 uh string links here in this unity.collections uh yeah the other things that changed is the network variable settings uh this got removed completely so whereas before you could set who could read and who could write to network variables now it's just straight up only the server can write to a network variable and the clients just have read permissions so you can't even set these anymore uh the other thing i don't know if i was actually using scene manager [Music] but the scene manager and the spawn manager um are now just inside of the network manager singleton here they were their own class so now you don't you don't have to um you don't have to worry about those anymore it is just um networkmanager.singleton.scenemanager uh is how you would go about switching seams like i was actually doing here uh i didn't did we use i think i actually did so another thing that changed was a network start was previously called once uh i think the first time when a object gets instantiated on the on the network uh that is now called on network spawn i think the player might be maybe we use it yeah i use it here so the on network spawn also uh needs to be overridden here instead of just using uh network start so i guess technically i could use a base dot nope that's not what i do that's why i don't do live coding uh base dot on network start there we go okay uh the other big thing is on our uh custom message handler we were using this to uh just send our unnamed messages and also register named messages for this it's also just been consolidated down into unity.net code but the custom messaging messenger manager is also in the network manager singleton as well and instead of getting passed in a pooled network reader they've gotten rid of that and this is uh i believe just a fast fast buffer reader and also there's a fast buffer writer as well so you can see here before we just had to get a network reader give it our stream uh right write our string to it um now we are just uh taking our stream our fast buffer and there's just a read value safe out of it and this will automatically just output our string to it that we just print so yeah this will actually work with with any type that you actually give it or the writer is uh much the same way except for the writer you do have to get a new fastboot for writer but it'll just write whatever you want to it it's not it's not a big change but um it is something you need to look out for and i think for update that's really about don't it anything else that actually change uh i guess for the uh network variables if you're making your own this did change a little bit here so instead of being a network serialized it is a network serialized with a generic type and this this also uses the uh reader writer instead of uh no it just has that as the uh has the type so the uh instead of using the serializer dot serialize that we used up top it's just a serialized value and a reference to said value uh you also i think this is actually needed as well i haven't actually tested this but you'll need to have a equals here from equatable to tell the net code that your variable has actually changed so if it hasn't changed it will not flag itself dirty and update the other clients for it okay and yeah that's that's it for the big changes okay so let's talk about the relay service now so the relay service is like i said is their uh peer-to-peer service uh that its main use case is getting through a net punch through or peer-to-peer connections for uh for players is it also will integrate into the lobby system that will get to you in just a second here so this is a nice little little diagram here on how uh the actual service is set up as so you'll have one player as your host player that'll basically be like your server we're using it as a server here it's going to connect to the relay server all of your connecting players will also connect to this relay server and then from here the relay server uh this will basically relaying the uh packets from uh the relay server to the host player and back and forth that way so this way like you're connecting players don't need my ip address to for me to host a game on it uh there are some i guess we could get into some of the prices here there are some uh limitations here uh there is a maximum of 10 concurrent players per match so you you're not going to be having um you know huge 16 player peer-to-peer matches if you're doing anything more than this you need to have some dedicated servers set up which arguably you should probably do that anyways for pretend players that's a lot for a single home connection to handle uh for now i think there are only four servers available right now uh asia east east europe west here oh this east europe west u.s and central us once this gets out of beta they will be opening up more more locations okay here you need to actually pick up some prices here uh this is important uh because these services um are not completely free there is a free tier uh the free tier is pretty good for uh especially when while you're developing is definitely more than you'll need so here for our relay the free tier is for 50 concurrent users uh the standard tier is from 50 to 200 concurrent users uh and the premium tier is 200 plus concurrent users uh then here's the uh the pricing for it as well uh there is a lobby here so the lobby is free for the first 10 gigabytes of bandwidth that you use per month uh and then beyond that it is uh nine cents per year per gigabyte in the us 16 gigs in asia and australia so yeah just something to uh to keep in mind very few of these uh relay services are going to be completely free for uh for all your players so once you get into production uh depending on you know your success levels um you know 50ccu you might never even see that okay so basically uh setting up the relay as a host you can you can either do this through uh the netcode framework uh or you can actually do it through their unity transport layer the uh the transport package is a lower level so you're dealing like directly with like the network driver and listening to uh and binding ports and whatnot most people don't have to won't have to worry about doing that uh the other thing that you will need to do is go to your dashboard in unity which is a little bit up here here so if you've never been to your unity dashboard uh this is where um all the various services actually live for your project and this is you know if you're keeping track of all your monetizations your users and whatnot is all going to be here uh you'll so this will have a all of your projects is listed here individually and for us we'll go down to multiplayer and you need to make sure that your relay is turned on and active here when you turn on relay it will also turn on lobby as well uh so you don't have to do them both separately you can use them independently of each other but for now in the beta they're they're linking the two so you turn one on you will turn them both on okay so this is our client we'll take a look at the server here first once it decides to come up okay so if thanks to my azure okay so there's going to be um a bunch of services here that uh we'll be actually using here so there's the relay service uh the models for uh like player uh classes and whatnot will be here uh we'll do the authentication i'll show that here in a second actually we can pop over to unity here to see that in our lobbies uh services models and stuff here so the first thing that we are actually doing here is uh initializing our services and also uh authenticating here this is not in here so this will be in our authentication here okay so this is how you actually just authenticate anonymously uh you just call your authentication service dot instance dot sign in anonymously async and what this does is this will communicate with the unity service here in our project settings here in the project settings you'll see uh some of the services here you can get set up here for authentication you can i don't know if you can actually see that but there are different uh providers that you can set up for uh apple facebook google and steam so if you were set up for steam authentication you could add your app id and your key and then you can go through and there is a authenticate with steam uh service here which i think you can just look at some of this authentication service dot instance dot uh so here we can uh link with any of these external services or we can sign in with apple facebook google uh we can sign in with an external token if you're using some other weird thing to uh install oauth or whatever uh and yeah it's nothing with steam here but we're just hanging out uh uh anonymously i believe this is using like a system you would or something and uh this actually gets saved to your registry for the uh for the project and so when you link uh like your steam or facebook or whatever account to this you don't have to keep signing in it will save your authentication token to the registry and then pull that back up whenever you want need to sign it again so yeah once this actually goes through or throws an exception or whatnot then our connection manager here uh once this gets signed in we're going to run a co routine here to start our host now for the relay server to get a join code uh you just need to give it uh your max connections so however uh many people you want to join your match uh this will you need to give it to that so i'm just using 10 here since that is the max of uh the relay server we're just using that and so that's just going to wait until that task is completed uh most of these are using uh are using tasks on the um the api side which allows us to do cool things like this so if you've never seen um a return value like this this is something you can do with tasks and this is going to uh well this task here i think we can look that up here it returns a ip address a port uh the allocation id and bytes the connection data as a byte array are key in our join code so using tasks in this way lets us do like multiple return types and our join code is what we're actually going to be using to create our lobby with so once this gets done here then we are going to uh we get our join code for our relay so you could technically use this without creating a lobby if you just want to type in this join code in in a group chat or something you can just join straight up for uh have the users type in this join code and then they'll just automatically connect to you so here we are going to start creating our lobby with this join code but we'll also go ahead and start our server so we're just starting it our server with these the address and the port that the relay server gave us here so we're using the unity transport here and we're setting the server data for all the server data that we got here and then you just start your server like normal uh this task is the actual allocate server allocation join code task this is just pulled straight from the docs and i didn't actually change anything here but uh it is just going to create an allocation with our max connections and our region and i think default region is east u.s i believe uh and then this will create our join code here just pass in your allocation id and then it returns our ip address port and whatnot here so creating the lobby here so our lobby has a number of options here uh i think the oxford lobby here we go so our lobbies can be public or private and that just dictates how you can actually join a lobby whether it's uh discoverable through a search or joinable through a lobby code or a lobby id so just like with the relay code you can type in a lobby code and just join directly to it there's also quick join which is what i'll be showing you and this is just a quick match so it will grab whatever lobbies are open and then pick one at random we're only going to have one lobby here and it's just going to join uh joy automatically there you can also give it uh some options which i don't know if our options our and it does say here somewhere but basically when you create a lobby uh you need to give it a name whether it's public or private uh the lobby size so you'll give it a max number of uh players in your lobby so like i said before your uh your lobby does not have to work with the relay service so there really is no maximum lobby size you can have um if you want to do this with your you know battle royale or whatever you can have a hundred people in your lobby and that will work fine with your uh dedicated servers you also need to give it uh some initial lobby data so what we're going to be doing is we're going to give our lobby data the relay code for our relay service so that's going to be how the clients will connect to your to your actual relay uh relay server here you can also do things like game modes um map ids if you wanted to do um like skill uh rankings for la ladder matches and whatnot uh you can put that in in there as well so you can do some rudimentary matchmaking here if you had like uh uh skill skill levels for this match like it's going to be you know gold level or platinum level or whatever you could do do that with that with this uh there's also your host player data since our host is a server uh i'm not actually using host player data but this would also be a place where you would put in uh like player names you can also put in uh characters that they are they are selecting before they join a lobby that'd be a good place for that as well uh your lobbies will only remain active for uh 30 seconds if it doesn't receive a heartbeat request from uh the host keeping it alive so if your host disconnects or for whatever reason uh stops uh communicating your lobby will actually inactivate itself until three or five minutes then it'll actually delete itself but uh there is a rate limit for heartbeats as well so you can't just ddos the uh lobby servers uh it you it's a limit of uh five per 30 seconds i think mine is heart beating every 15 seconds i believe we'll check that out so let's get back to our code so here uh we're just creating some new lobby options here and here we are our data is a dictionary with a string and a data object here the data object uh also has some visibility options here so we've got uh here we've got uh public uh which is available to people who haven't joined the lobby yet members which is available once someone has joined the lobby and private which i believe is the host only um the other thing here is the indexes here uh you have a limit of how many uh options here you can add so there's uh five strings and five numbers i believe these are the integers i believe uh so for our relay code we're just using that as string one for here so we could also have you know your map type your mode type you can have uh max players or players per team uh can be in here as well and then we just create a lobby and then we create a lobby we give it our name our max players and our options uh and that's basically it once this gets done our lobby is created and then i start a co-routine that will heartbeat the lobby every 15 seconds and for the heartbeat it is just the there's a lobby's singleton here uh which is just a instance dot heartbeat ping and you give it the lobby id and also uh once the application is quit uh i'll i'll delete the lobby because i don't want the lobby hanging out there for uh longer than it needs to be now for the client it's really about the same here so what i'm doing here this is the old code here so here is a quick join so when you are looking for a lobby you have a query filter here so this will let you limit the number of lobbies that you are looking for and i think there's a limit to it the number of queries you can have but you can totally stack these up so you right now we are looking for any of them with a max player that is uh greater than or equal to 10 and there's a bunch of these field options as well by default you can look for max players available slots so you're not looking for matches that are full already you can sort by name or their created date or the last date they were updated and again you can also search for any of these string names or the number indexes as well you can also hear i didn't actually put any player data here but uh here is where you could enter in any of your other player data that you wanted here and so like our player data uh player data also has uh visibility options as well and it works much like the uh lobby data so it is just a dictionary of a string key with the player data object so you can put in uh whatever you wanted to here this could be you uh display name and then give it a visibility of public value of whatever your display name is uh and then also if you are wanted to update your player information after you've joined the lobby then you would just use this update player async information here uh the same for the lobby data as well if you want to change any of that data say you guys picked a different map or whatever you could update the lobby that lobby data through uh update lobby async as well our server you know so yeah let's uh take a look server okay we have time to put uh fancy graphics on the screen we've got um our anonymous sign in is connected uh we've got some server connection data our allocation id this is our join code uh for the relay join code and we've created a lobby uh i should have printed out there it is so this is our lobby id this little random blob here and our lobby code is right here as well and um heart beating here so our lobby stays alive and then in our client here and when i start client here this is going to also authenticate anonymously and then it will find the lobby and then join its relay server here unless it doesn't find any lobbies come on there we go link so yeah we've got our we joined our lobby we get our connection data our allocation id and then we start our client and we are connected and our my uh player code here is a bit janky but it works yeah see [Music] do it this way uh for some reason uh it is always looking back straight up when i stopped moving i haven't quite figured that out yet and you can see some sliding going on uh that is just uh lag from hitting the relay server and back again uh you could do a bit more work with some uh client-side prediction and whatnot but uh this works uh pretty well for just basically using the standard uh our standard third person controller here there is a couple of bugs here with uh the relay service with uh sending too many packets or something i'm not sure what that is but doesn't seem to be affecting much here so in our this one armature here so what we ended up doing uh here is first we had to just like before we had to make it a network behavior and the only thing i've changed is down in our movement here uh i just have a server rpc that i am sending um a vector3 to which is uh ultimately the direction that our character is moving in so here is our normal um actually i think i did change this up a little bit but here is our target direction which is getting get in from our input system we're giving a speed uh time dot delta time and i added in this was in our jump code uh so this is our vertical velocity so whenever we press jump uh this vertical velocity becomes one uh and then i'm giving it um multiplying that by times that delta type i don't need to do that twice that might be why i'm messing something up here but i'm just getting that vector three i am moving our controller locally here which actually i don't think actually does anything because we are in a uh have a network transform on it but i kept it there uh so we're taking this we are sending it to our uh our server and then in server [Music] third person controller i commented out the move function here just because on the server there is no input system so there's no input to actually control any of this and then all i'm doing here in the move player script is we are rotating uh yeah we're just basically rotating the player and then calling the character controller to move it around uh this is probably where my my bug is happening that it's uh moving back up to to the top whenever i let go but anyways we'll figure that out later so yeah that's uh that's basically it for the uh relay and lobby system um it's a lot easier than uh than play fabs was but not quite as fully featured either but most of that will will change with with time especially once these things get out of beta so yeah if um anybody has any questions or anything like that just uh leave it in the description there or hit us up on discord or on our telegram and uh we will uh we'll get back to you on that um i will end up uh this will be up on youtube as well and i should be able to get these both these projects up on github uh later tonight there's anything weird in here that i have to strip out but yeah didn't do too bad about 40 something minutes cool all right so uh i guess thank everybody for watching and we will see you guys next time you
Info
Channel: Memphis Game Developers
Views: 230
Rating: undefined out of 5
Keywords:
Id: WKo1lQAFCCw
Channel Id: undefined
Length: 55min 25sec (3325 seconds)
Published: Sat Nov 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.