MLAPI Netcode for GameObjects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right welcome everybody my name is david from memphis game developers and this month we are going to be talking about uh unity multiplayer networking um i guess a bit of history with this is um mlapi is was a mid-level api for guess it was during the unet networking days where unity supplied the low-level networking and a group got together and made a mid-level api on top of that they have since been purchased by unity last year-ish and unity has since started integrating that into the actual engine they have since recently in the last month or so renamed mlapi to [Music] netcode for game objects which makes it hell to actually google search for because that is a generic name for basically any kind of networking code but here we are anyways so it also makes searching youtube videos really weird because you'll end up with a lot of old videos if you search for mlapi but for right now um all of this documentation is up on um unity right now under uh docs dash multiplayer.unity3d.com we'll have a link to this in the in the description there but right now it's still in uh preview access so to actually install this you need to go in through the package manager and actually install it through a github link which should still work here if you copy this and this actually might be an old link because that says mlapi but if you go into your package manager and click on this plus here you'll be able to install it through get here manager here you'll install through a get url already got it added and you'll see it show up as a mlapi networking library uh soon that will say uh during the next uh 0.2 release they'll probably rename this to networking for game objects so this is just for migrating and whatnot but that's fine so after you get it installed um the first thing you actually need to do is set up a network manager and the network manager uh for now is going to live on a game object in your scene and it's um by default it's set to uh don't destroy on load so it'll persist throughout um scene changes and stuff uh we'll go over uh the network prefabs in a bit but for anything that you are going to be spawning and despawning throughout um and syncing between server and client you need to register all of your prefabs so it knows what it is actually looking for by default the uh you'll need to have at least one uh default player prefab and uh yeah by default this is set up to actually spawn your player uh when you actually connect the you'll have a game version uh well a protocol version here the network transport is actually um the low-level um like the socket connections and whatnot for your networking it comes with um the unet transport which is um kind of the old uni transport layer there is a new one that they are about to release here soonish uh that's just it's uh i think it's just com.unity.transport i think in the uh in the package manager but you don't actually have to know about any of this for right now uh just know that you can use your own transport layer so if you are uh where is the i guess it doesn't actually let me switch unless i go to none do this real quick so this is what you'll see when you actually first uh pop in a network manager you actually won't have a transport layer but you can install uh the u-net version there is uh different ones for there's like e-net and lib net uh light netlib transport there's also um the photon transport layer there's one for uh steam p2p uh it's actually up on github here i can pull it up it's in the documentation as well but these are actually made by the community see where here we go uh so here we have uh there's ruffles e-net steam p2p websockets uh photon real time face punch which was the steamworks guys and these are just all community made transport layers for for ml api that you can use that has various other uh features to it but that's for more advanced usage i do want to look at that lag compensation anyways so yeah uh so i'm just using the unit transport here uh by default the you can set the tick rate and the network tick interval uh this is going to be changing here soon with the uh the next uh 0.2 update they're reworking how uh the network tick rate uh actually functions and all the believe the entire like network update loop is getting changed uh you can you need to enable network variables uh this is how you actually sync variables between the client and the server i'll go into more of this when we get into the code but this is important to have checked you can also ensure network variable link safety uh i think that's just checking for like buffer overflows and whatnot uh the connection uh protection approval this is for if you have like room names and i know photon uses rooms in room names this can also be used we'll use this for um spawning different player prefabs other than the default prefab for like selecting characters and stuff for the spawning here we have um create player prefab and this will automatically create your player prefab on connection um sometimes you might not want to do that if you have like a lobby system where you are selecting a character and then you want to spawn into the game you uncheck this you'll be able to spawn the player prefab manually uh we will also for setting up an actual uh dedicated uh where is it here it is of course prefab sync uh when you have a separate project for the actual server uh which i'll show you guys here in a second this uh use prefab sync will uh force a reload of all of your network objects in the client to match up with via the server project so if you're just using regular um server hosts or client host setup for your game you'll want to leave this unchecked but i'll show you how this works here uh there's also scene management here so if you if you want to have multiple scenes uh in in your game you'll need to have the server needs to be on the same scene as the clients otherwise they get they'll desync and the when a scene changes the server like unloads everything from the old scene and loads up the new scene and if the clients aren't keeping up with that then they'll be sending um data to game objects that don't exist on the server so you will need to um if you have multiple scenes you'll need to enable the scene management um to keep everything in line so when you change a scene on the server it'll automatically change the scene on the clients and then we have our start host start server and start client the difference between a host and a server is that the server or the host actually is running both a client and a server in the same instance and servers are just a server and clients are just a client so what we'll be doing is in our in our second project here we'll be starting as a server and then we will start our this project here as a client and uh down below when we select our network transport it adds this transport component down here and this is the actual like ip address and port data that we'll be using to actually connect to it we'll just be using the default the local host here so we'll just be connecting to the the same computer but if for example this would be like your you know amazon aws server address here or whatever your matchmaking system spits out uh that we talked about last month like through playfab uh this is what you would use to connect all that together uh there is an uh mlapi relay um project that you can uh run for um that punch through if you wanna do like a uh p2p game you'll need to actually set up a relay server to connect uh people's um internal networks together i haven't actually tried this i'm not sure if this is a test server or what so let's actually pop over into our server project here so this is our server and you need to make sure that your settings for your network manager are the same as your client otherwise you'll get like a network config mismatch error i think it's called and your project doesn't have to be named the same uh but we'll go over the prefabs and how that works um here in in just a second here make sure okay yeah i'll just unit here all right so let's hop over into some code here let's close all the others okay so in your in your actual code here what we are doing is this is my game manager script and instead of inheriting from mono behavior we are inheriting from network behavior and network behavior is basically a bottle behavior that has some hooks into api and our our network stuff for i guess we can talk about the network variables and actually how they work so the network variables um what they do is they will automatically sync um from the server to the clients by default uh there there have permissions set up on who can write and who can read to them when the server updates the clients on a change in this case this is a network list which is a list of players and i was using this to just keep track of all the players that have joined the game and basically you can subscribe to a there's an on list change for network lists uh i believe network variables uh have an on value change and you can just subscribe to it and that will here's the list update so whenever the server updates the list this gets fired off and then you can do what you will with with that change so if it's like health then you can like update your ui and you can damage the player particle effects what not uh the other thing i'm doing here in start is the network manager uh is a singleton uh for now i believe this is actually going to be changing as well so you can actually have multiple network managers running at the same time but for right now it's a singleton and they have various callbacks that you can hook into on the client uh i'm just looking into uh there's on client connected and there's on client disconnect this is also network uh connection data or when we are using the [Music] um connection approval function here we'll show that in a second for uh for the server and then you can just start client um normally you probably wouldn't do this and start you would uh you know do this in your menu but i didn't feel like hooking up any ui so we're just doing it in start and there is a built-in logging here which is actually kind of neat i just got this set up last night but if you use mlapi.logging here you get access to this network log so what this will do is when you call this it's basically a a debug log but it also will get sent back to the server which is good for like play testing and stuff you don't always have access to uh whatever client is running but you should have access to your server and i think i got it yeah so here uh when i call what i connect this will call log info server client has connected and then here in our server here you see that it has the uh sender equals two this is the client id and it just says client has connected and i think this also gets written out to the actual uh log file as well so let's actually see this um function here and yeah so what we'll do is we'll just start our server here and what should happen is when we connect it is going to uh automatically spawn in our player capsule here as a as our prefab on our client we also have the player capsule as the default for the actual prefab here this is actually super important especially doing a uh split project like this so the way network objects are kept track of in mlapi is through uh network objects so everything in our scene that's going to be spawned and synced up has to have a network object now see here so the actual prefabs themselves uh don't have to be identical so here uh we have a our player capsule on our client side has this grayish silver color to it and uh the on the client is just like the default uh grade capsule coming on the server uh this prefab hash this is what employee api is actually using to spawn and despawn um our our prefabs this is what it is actually registering in the uh network prefabs here the other thing that you'll need to know for i believe also for network variables as well as our pcs which are the uh remote procedure calls we'll log into that here soon but uh the actual prefabs don't need to be identical but uh they do need to have um the correct uh components in the right order so for this player capsule prefab it has our network object uh the animator is actually disabled for this one because it's not actually animating uh the character controller the network transform which is here so the network transform is just a component that is going to be syncing the rotation and position of this game object to the server this one here by default is actually client controlled so on the server i can't actually change the position we'll show that here in a second uh in the next update i believe they are introducing a server server authoritative uh network transform and that just means that you'll be sending inputs from the client to the server the server is going to be updating the position and then sending that back to the client and just due to physics and the speed of light and whatnot that is introducing a little bit of delay uh from when i hit up to when the player actually moves and there's some you can do some interpolation and some compensation to mitigate that but it's actually super complicated to do uh so this one has um there is like a snap distance so if your network connection flakes out for a little bit and you end up moving uh farther than the last update after uh like 10 units i think this is it'll just snap to it instead of trying to lurp over to it just to keep everything lined up and in sync there is also um channels that you can have an ammo api uh like you can have unreliable channels and reliable channels just for like udp and pcp uh i think by default this is set to uh reliable i believe but let's pop this open and see what happens here so this is on this on the client i'm on the server here and on our client we will start and we should see our capsule buddy pop up yay we connected um yeah oops not that here okay that's cool so where'd my mouse go all right uh so yeah oh i think i have this thing locking the i should change that so this is using the um third-person controller from the essential assets with the new input system and i think this thing locks the uh locks the mouse somewhere yeah but in our uh night manager here we can stop the client and there we go that's annoying right so uh i guess that we could show now so here sure set the same so it actually won't let you have two uh default player prefabs checked at the same time if you don't have a default player prefab you'll get this error message here that there is no prefab selected i don't think this order matters we'll find out so here in our client a little bit bigger we have a this player armature so this one is a little bit more complicated than our little capsule dude this has a full animator here set up and on our prefab on our server uh it doesn't need uh i don't care about its um actual animations going on um i don't need the player input on the server um and i don't actually need these any of this uh the starter asset stuff on the uh on a client prefab all i need is the network pre to the transform to keep track of the rotation and position of the player uh but i do want uh this network animator this is a separate component that will keep track of the animations to other clients that are connected uh i don't know if i have this one built out or not but we can do that real quick so what this will do is on our prefab here this will keep our components are parameters for the animator all synced up now this will not sync uh triggers in the in the animator so where is here so our parameters are the speed jump grounded free fall these are just bulls if we were to add a trigger that actually does not get get synced up so you just need to keep that in mind for for your animations but for our uh actual prefabs like i said um the actual prefabs don't have to be identical but the component list and order uh actually does matter um i don't remember if i said this or not but the prefab hash here uh that only um it hashes the actual name of the [Music] uh prefab or uses this string here so even though this is player armature prefab and this is just a player prefab uh i overrode that here at the prefab hash generator to the player on both sides and then just make sure that this hash is the same on both sides so if even if we uh put a space in there the hash changes and this would like break everything so this all should still work i don't think i broke anything we'll find out so we'll start our server and then we'll start our client okay and you should disappear good so what i'll do right now is we'll just build and run this and it shouldn't take too long while that's building does anybody have any questions so far about this now that's pretty cool are you gonna demo it with two clients yes we are sweet oh i guess i should have had my uh not having ui uh started the server first yeah okay so we will start our server actually just left it running then this so oh crud i don't know how uh the input system is kind of weird in that well you can see here that the the animations do sync up and in our server here it's just uh you know capsules running around and honestly i don't even need the capsules here for the server uh it'll be running headless and that just means there's no graphical components running at all on it but this is just for streaming purposes so we can close i don't know why that doesn't work sometimes all right and actually just show that real quick so to actually build uh the server out what you do is you just make sure that this server build is checked and that enables some build defines for servers and also i believe this just this will just run uh headless by default uh on your your actual machines so how much of that do you have to worry about like if you included the capsule for instance versus not including the capsule is that going to make any difference i mean like how much um should you think about making this server a totally different program with with no nothing except transforms rotations um for the actual build it doesn't really matter uh because it's not displaying any graphics anyways you don't have to worry about um you know polygon count and whatnot or frame rates and stuff um so yeah it doesn't really matter uh it's just um really just to demonstrate the point cool so let's show the connection approval so with this checked uh it will actually run a connection approval uh callback here you have that on the server side and i just got to make sure that they are both checked and server link so here in our client um right before i called start client i set this network config connection data and right now i just set this to a room name this can be [Music] really any data you want uh it just has to be uh serialized out to a byte array so this could be just like a json file for like selecting your character and like you know like level difficulty and stuff for like matchmaking and whatnot so here in this is our server and for here uh i also have a on connect and on disconnect but there is also a connection approval callback and this only gets called when that connection approval option is checked and when that gets called it gets passed in that connection data byte array the actual client id which all the client ids here are u-logs and also a callback that gets called so for here you would uh do some kind of check to make sure that um the connection data that you sent um you can check for like make sure that this is the same room name um that this connection manager is set to i'm not doing any of that right now we're just go ahead and just setting it to true this is just going to be to set a new uh player prefab for our different characters and what i'm doing here is i have a list of player characters which i set up up here at the top uh here just a regular list of game objects and i'm just getting a random number of i only have two set up for um actual client stuff and it is uh generated from the name and then once uh i have that then you have this callback that's going to get automatically called and then you just pass in uh the create player object your prefab hash and the approval where you're going to actually spawn and the rotation to actually spawn in so what will end up happening is on our server in our game manager i just have a player and our player capsule in our client um i actually don't have anything set up here um what you would normally do is you would have the same prefab set up here and you would send it you would have like ui set up for like you know choose your character here then you would add this into your connection data that would get sent over to the server and then it will spawn uh whichever one of these you have set up like i would pass in um the index for the player characters you would make sure that um the [Music] player which i think is the armature here would be first and then the capsule here would be second so you would send in if you want to spawn in with the human character you would send it index 0 if you want to spawn it with a capsule character it would spawn in index one but for now because i'm lazy and didn't want to do any ui it's just picking a random uh character on the server side to pick which one so we'll go ahead and run our server here we're gonna quiet i'm sure that works okay and whoops forgot i need to rebuild this one it's over here and [Music] still doesn't work i think it's because the uh standalone client is eating my input but it's fine cursing okay so yeah this is um the way that you get your prefabs to uh to spawn different players or your different clients uh let's see we went through network variables um you can set up uh network variables for any of the um unity primitives so things like your um you know your strength actually strings are going to be changing here soon but ants floats bools i think strings are getting changed to either character arrays or i think they have a separate uh string variable that we'll be using because strings are weird in c sharp see network objects connection approval let's talk about object visibility actually wait i have some other stuff set up here i think this will work maybe so in our client here we can show off some syncing of network variables and here we have a little chat system set up i think maybe that's fine okay so here is a simple chat system what i'm doing here is [Applause] um creating a network list of strings which is going to be our uh chat messages and when you create a network uh variable you need to set it to a new network variable of that type and you can pass in a network variable settings and these are your permissions that you set up so there is read permissions which i'm setting this up for everybody can read this this variable and everybody can write to it by default only the server has write permissions and every client has read permissions so i believe the different permissions is custom everyone owner only or server only so if you have a network variable on um like your players you might want to set the read and write permissions to be uh only the owner can write to this but if you're doing a dedicated server setup you probably don't want to be trusting the client anyways so but this is just for demos and uh for a chat system people need to be able to write to this i guess the other way is you could uh send your strings to the server and then have the server update this uh network list variables but um either way it's fine so when we change this list this will call this chat updated and all that's going to do is add a that message to our list of f strings and then we'll see that uh i thought uh i uh yeah i guess i don't have like usernames set up or anything real quick yeah i'm not going to all right that is on there and i believe i need to we'll just throw this on our server project so this is on the server you need to have the same network variable setup on it with the same permissions and that's basically all you really need these are for the rpcs which we'll talk about here next this out build okay that is not going to work because it is going to be looking for this prefab so what i need to do real quick is create an empty let's call it chat system system object [Applause] got to make sure the hash is the same and so yeah everything that is a network behavior everything said network behavior has to have a uh network object attached to it as well so that's just something you need to keep in mind now that should work so can you explain why the input and the box are not showing up on your other side on the server side right yeah i just don't need it uh this is just on the client side so uh server doesn't need to have the input box on it i'm just wondering like why so in your chat chat system variables why it's those variables are not showing up on the server side oh it's just it's a separate script it's a whole different project i just named it the same okay gotcha so now let's see oh no crud okay uh i will need to make this a prefab and i need to add this prefab to our okay and this error match is showing up uh because we have the prefab sync setup here so with prefab sync everything that has a game object on a network object on the server side is going to try to get spawned on the client side and we have to make sure that this is on our okay oh this should work i think it's every build okay that crud see to check this let's try this again sure actually and i hate stuff okay that should work it's not working to put the system okay save that okay okay so so yeah this is um super simple uh variable syncing up um i don't know why client keeps freaking out when the client is running but it still syncs up um even on reconnect the server is keeping track of this it does not sync up automatically [Applause] and that doesn't work um there's probably a way to uh set it up uh oh yeah i think i know what's going on now uh to actually sync up on connection i think you just have to set the uh variable um set it as dirty on connection so it automatically syncs up instead of waiting until you send something and change the variable because this is the server this on list change up uh function here only gets called uh when the list actually changes so it's not actually syncing up like on update or whatever okay so let's talk about our pcs and remote procedure calls so on do we have any on the game manager uh these actually aren't doing anything but um it's enough so the way uh rpcs stands for remote procedure calls these are functions that uh you get it gets called on one system and it gets run on another system so server rpcs they get called on the client and then get run on the server on the server side uh i don't have client rp set up here but on the server side on the game manager script you have the same uh function uh signature here you do have to make sure that the signature is the same because that's part of uh what what gets uh sent over here so for this um send player to server rpc on the client side this will send the player data this is a struct that i made that will contain some some player data and also we'll send these server rpc parameters and this will on the client side you don't actually need any of the actual method calls here it's just going to be an empty um empty call because this is not running on the client side here on the server side uh it will actually run and this is where your your actual code block is going to live so this gets passed to the player i mean to the server um which will then on for this particular function this is if you were to call um a spawn player manually um this is how you would actually go about doing it uh so for this like i said before when you were setting up a ui for this you would send over a character index and then also this rpc params this will actually be the client that actually sent this so to spawn uh an object on the server side here this is all happening on the server uh you first just have to instantiate it and just extension like normal normalgameobject.instantiate but then you also have to get the network object uh of that prefab and then there is a uh spawn as player object function of network object there's also just straight up spawn if you're spawning like missiles or bullets or whatever and uh to spawn as a player object and then you give it the client id to spawn it as now this is a special case here for this particular rpc uh this i set up to for when the player spawns in it's going to send its own data to the server so here on the client side i've got a player script so i guess we could talk about this really quick so i made my own uh network variable and to do that you just need to make a struct that implements the i serial network serializable uh and with this this will allow you to send your data across the network through rpcs and stuff uh so this is just going to have a uh int for the player id a string for the display name uh and also i have a debt debt card system kind of set up this is also a custom network variable which is just a array of cards the cars is also network serializable which is just an id and a power i haven't implemented like any of this yet but it's there and um i guess it works i haven't actually uh made any cards for it but it didn't throw many errors when i do this so i'm assuming it's working um i have also set up on the actual player uh a bunch of other um network variables that i want to sync up eventually and then each one of these network variables also has an unchanged that i am subscribing to that i'm not actually doing much with right now but anytime any of these changes from the server side i'll know and i can actually do do stuff with it but um on connect which i just subscribed to from the network manager singleton uh what i'm doing is i'm just grabbing the player data that i've saved out locally and then i am sending this to uh the server um i could technically use an rpc on the player to sync itself up but i wanted to just uh demonstrate this to make sure that it actually works uh so this is going to send it from the game manager it's calling send player data to server uh i do also have one here no i didn't actually set that up yes i did up here so i just made a local one that would just do the basically the same thing so on the player it is syncing up between itself on the game manager uh because it is being spawned by the server uh the game manager is actually owned by the server so in order to call a rpc on an object that you don't actually own uh it needs to be you need to have this um attribute set up is require ownership equals false otherwise you'll get an error message that says something like you don't have permission to do this because it's owned by the server and then what this is going to be doing on the uh server game object is this is going to be adding uh that player to a list a list of players and in theory i was going to add the client and the player just so we can uh map those together because for most the most part um what you'll be getting from the client is just the client uh client id so when a new person joins or leaves the only thing you'll get is a client id on when that person joined or left so mapping it up to a player just gives me a little bit more info on like their player name and stuff so that is what was going on here uh i also set up the player data as a scriptable object but that's for another time so i can just change uh my display name to something else so would you really ever want to take and send your player data from the player to the server what if you want to just have it say okay hey i'm logging in and then have the server just pull up whatever that player's last info was and load it ideally yeah but i don't have a whole player system set up that requires like a whole back end and stuff i mean we didn't if you were doing like playfab that's exactly how you would do it you would just log in and then all that player is just stored on playfab servers but i didn't set up any of that stuff here so here close that all right so yeah that's just the basics of rpcs uh option visibility so here on our server let's load this up a little bit and you still have that synced up okay good so here we'll just have we've got a cube uh set in the middle and that's fine so because uh this is on our server and we've got a network object on it uh i think okay that's fine so for network objects they have this check object visibility delegate here and what this does is on a client connection it will check to see if this client is this this object is visible to this client this is normally just run once on uh on connection so for example if this was um like a real-time strategy game and you have um like a fog of war and so when you connect you want all of the enemies players units to be not visible to the to the player and this is how you would actually set that up so if it returns true then it gets uh it's visible so it will get spawned on the client and if it's false it is going to not show uh so this little code snippet from the unity docs there and what this will do is this just calls a vector3.distance uh from the uh player client or from the player object to this actual game object and if it's less than five units away then it will it will uh if it's less than five it'll show it if it's greater than five then it gets uh disabled so on spawn this is just spawning i believe just in the middle and zap zero zero right so it's gonna be spawning directly on the player so we should see this cube show up uh even though our plane here uh doesn't actually have any game objects in it what's going to happen is because we have our uh playfab sync or prefab sync uh set up on our network manager it's going to spawn all of our server objects onto our client you just have to make sure that your prefabs uh are actually set up here in the in the network prefabs list so if we just play this we have our cube now on our player it's on my actual server so um on our player object on the server i have this um area of interest so what i have here is just an ontrigger enter and on trigger exit and when this gets when it exits the the trigger volume it is going to call this network object hide and it's going to hide it from this client so it's going to hide it from ourselves and when it gets inside this volume we're going to show it so network show and network hide will spawn and despawn the object from the client i guess we can so as you can see on the server side it's still there and technically other users would be able to see this as well uh but it's outside of my range and when it goes back into my range it shows backup and all of the uh variables and whatnot will be synced back up to it so that you would use this um ideally this would be your trigger volume would be like beyond uh the camera visibility range so if this was like uh like this camera style here your volume should be um probably greater than your your actual camera field of view um just because you don't want stuff popping in and out while you're actually playing this will save on bandwidth if you have particularly large uh open world scene you don't need to uh have your objects updating and sending data for stuff that you aren't interacting with or can't even can't even see seems like that would be pretty good for like anti-cheat stuff too where you're reading memory and because it's actually removing it from the memory on the client it looks like yes yeah so like for um uh enemy team stuff so if you don't want um uh things to show on on your client for like uh you know opposing players stuff whatever that actually is you can um just hide it from the from the player and yeah that those updates just don't come across the network at all that's really cool yep there's probably a better way of doing it uh beyond like putting triggers on the uh on the server side but this was nice and easy to do and easy to explain so and i guess we can let's do this real quick show it off a little bit um you know cool yeah i should move the camera to something better but anyways that's the idea of that and finally we can talk about uh custom messages yeah it's a good good thing to wrap up on so this is the client okay so say you don't actually want to uh use our pcs to send messages back and forth to different objects or whatever uh what you can do is use this custom messaging manager which is part of i believe it is just part of uni messaging here yeah so there is a um unnamed uh message callback and there is also a named message callback and i think i'm only using this one is just using the unnamed so what this will do is for an unnamed message uh this doesn't have a message type or anything this will just send a message a stream which is just a bite array buffer to um i believe this is just so on the server side this just sends it to everybody uh or i send it to the um the client what i was doing with this is i was using this for uh the chat system so i was sending a message to the server from the client and then i was sending a message back to everybody so on the client side i have this uh client uh message handler so on awake here what i have is i have a uh custom messaging manager here and on unnamed uh unnamed message here i'm just doing this inline so this will receive a message if you're the client um you're only receiving messages from the server so this is just server id and then this stream which is just our byte array and what i'm doing here is i'm using this uh pooled network reader which is part of mlapi serialization pooled yeah and this will uh just it will spin up a reader and then it will get this stream i know that it is just going to be receiving a string uh this could technically be whatever you're sending um any of the i believe it is any of the primitives here so you can have bits bools bite arrays uh double arrays float since all kinds of stuff here uh but right now yeah vector threes and fours this is just gonna be strings and i'm getting that string uh and then printing it basically and because it's unnamed i don't know what kind what type is it going to be or anything like that i don't know anything about it but for a named message then this actually has a type to it and it's a string so it can be just whatever string you want i set this up as a enum just because i don't like dealing with strings and function parameters but for each uh message type you can have a different message handler and the same way it also comes with the sender id and also the actual stream so what i was doing here is in the uh string type what i was doing is i just was sending a this notification message uh which is a little bit more complicated than i would have liked but what i did here was i have a message type enum which i'm setting up as just these various things for card game messages that you would might want to send from a server and i just made a empty interface here called network message that enter that uh implements the i serializable and then i have various structs that implements network message and for the serializable i don't know if i messaged this but the way uh network serialized gets called is that when it is uh reading this struct it will serialize uh its um members here and when it is writing when it's reading it will deserialize it for you so for this notification message here it's just a string and it will serialize and deserialize for you and uh the reason i did it this way with an interface is that back here i can just send a um a message uh just a network message and then on the receiving side um it can be whatever one of these i want it to be where's our server so here on the server side um what i was doing was i was sending the unnamed message from the client to the server uh the server will grab it and all it's going to do is just uh read that string into this message string and then just send it right back out and on this send function uh it is just going to call the network buffer and what this will do is this will create a buffer array and then this writer from that buffer and then it's going to write this network message uh into the writer and then send that off with this uh send named message so here i'm just sending it as a notification string with two set client which was just the same one that i got and then it's sending the buffer which is what we wrote out to it then back on the client side we are getting this uh named message of uh notification we're making a new notification we're reading it uh we are reading the packed object of type notification message and then i'm calling this uh this event here that i just made up it's just a static unity event which other things can subscribe to i don't remember where this is actually getting involved i may have like removed all that uh here oh i think i removed it but anyways any um any other script can just listen into that uh this notification event and then do do what you will with it this is not really necessary to use with network variables that gets sent for you so yeah this would just be for like really specific um instances show how nope that's under c's here we go so this is kind of important to keep in mind with um rpcs and how how they actually function here or server rpcs they are sent from the client and it is executed on the server so what happens when you call an rpc um also i'm not sure i actually mentioned this but uh server rpcs and client rpcs they have to end with server rpc uh or with client rpc on it uh it's using a reflection i believe to uh make sure that to call on the receiving end so it needs to have uh that actual in the uh suffix of the actual function call and that's rather important uh so yeah what happens is when you send rpcs it will serialize the parameters first and then send them on so this is kind of a pseudo code on how server rpcs actually work and these uh parameters here they can be of those i network serializables that that you made earlier so instead of using the um custom messages i could just use the rpcs to send that that same network uh that notification message i could just send that through an rpc and that's easier to do than doing it all custom yourself uh yeah and finally i guess it's um the scene management the only real change here is that when you are switching scenes uh you just need to call a network scene manager and it's just a switch scene and you need to do that on the server side uh to make sure that the the clients get all synced up and you also just want to make sure that the network manager is set to don't destroy on load which it is by default so so yeah that is yeah the the basics of it what's up steven i'm just curious the scene management thing so it seems like the clients and servers or that all the clients don't necessarily have to be in the same scene right so what exactly is what is the process like for that uh the clients they actually do have to be in the same scene um because uh when a scene gets changed all of his game objects gets unloaded from memory so they do have to be in the the same scene and that's what that network scene manager uh keeps everything in track of so if you were to have different areas that are separate scenes would you need to like run them like as two different rooms on your server and then move players between the rooms or how to how would that work right now um you actually can't it can't be different scenes um what you would do i don't think you can slowed them additively yet i think that's something they might be adding i think i hope but but yeah right now it has to be in the same scene so what you would do is you would just have a different set of terrain further out in the distance that you would just move the players to uh you could also use the network show and hide to like hide different buildings or hide hole terrains if you wanted to and then load in the new terrain so that would be also a way to do that okay to kind of set up like a little portal you know like we did in that one game we did basically you get to a point then you kind of just you know basically transport them to a different area and that way they're they're then seeing that train or whatever you want that they can have and then if they ever go back you know like let's say if you're going through like a doorway or an entryway and you see something different once you walk through there you kind of transport it then to the next one so you're not having to change the scene right yeah yeah yeah the um the network show and hyde um might actually be better in that case um i haven't actually tried that yet or whole areas but it should technically work and it's just spawning and despawning anyways so depending on how big your area is um i don't know if that would like slow down your scene or you get some stuttering if you're showing a whole bunch of stuff at the same time that would just require some some other profiling and whatnot cool anybody have anything else it's almost uh eight o'clock time to start wrapping it up very interesting talk thanks stupid i was mostly curious about that in terms of like a diablo type game or something where you have different dungeons and players going in and out of different ones yeah for um for this yeah you could actually for that type of game you probably could switch scenes just you have to wait until like everybody was like in the portal and then switch the scene over to uh the actual dungeon gotcha yeah i like doing a team based thing so as soon as you like achieve the the objective and you can kind of just change the scene and just teleport everybody then into whatever the next scene is going to be for the like the next level or right yep cool so yeah like i was saying in the beginning though they are this is still in preview and uh in active development and then they're uh on the github there is a develop branch that is that uh 0.2 branch that's currently working that they said was going to be out here relatively soon that is a has a lot of fixes and new new features and stuff so we might have to revisit this well we probably will have to revisit this in another year or so but yeah you post the link to your project uh i could if it was on github i'll put it up there uh you don't have to i was just thinking it might be it seems like you added a lot of like you customized it a lot so i'm curious yeah i'm sure yeah i can throw it up there i mean there's nothing nothing too fancy about it um but yeah i can throw it up on github both both projects they're technically two different projects so i can put the client and the server up there and i'll put a link to them in the in the description cool thank you awesome all right okay so yeah we'll also throw this up on youtube um probably later tonight so yeah thanks everybody for coming out thank you i enjoyed it thanks you are welcome uh so yeah uh later tonight i think we'll be um doing game night so i don't know i'll probably be hopping on engine impact every valheim if anyone else wants to join me yeah i'm taking away this time from the wife i'm gonna have to take and spend the rest of the night with her i gotcha not a problem i gotta make some food but we'll see yep same all right guys well i will see you guys next month and um thanks for coming out thank you you
Info
Channel: Memphis Game Developers
Views: 2,221
Rating: undefined out of 5
Keywords:
Id: sjLElfNBpyQ
Channel Id: undefined
Length: 105min 44sec (6344 seconds)
Published: Sat Oct 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.