How To Make A Multiplayer Game In Unity 2021.1 - RPCs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This post appears to be a direct link to a video.

As a reminder, please note that posting footage of a game in a standalone thread to request feedback or show off your work is against the rules of /r/gamedev. That content would be more appropriate as a comment in the next Screenshot Saturday (or a more fitting weekly thread), where you'll have the opportunity to share 2-way feedback with others.

/r/gamedev puts an emphasis on knowledge sharing. If you want to make a standalone post about your game, make sure it's informative and geared specifically towards other developers.

Please check out the following resources for more information:

Weekly Threads 101: Making Good Use of /r/gamedev

Posting about your projects on /r/gamedev (Guide)

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

👍︎︎ 1 👤︎︎ u/AutoModerator 📅︎︎ Apr 15 2021 🗫︎ replies

Nice! This will save me a ton of time

👍︎︎ 1 👤︎︎ u/Levi777L 📅︎︎ Apr 16 2021 🗫︎ replies
Captions
okay so hello welcome back to another unity multiplayer tutorial today we'll be having a look at remote procedure calls or rpcs which is how you'll be typically communicating between the clients and the server this allows you to send a message from your client to the server to execute some logic and then the server can also do it back to the clients i hope you're looking forward to it so let's get started so as always there'll be links down below in the description to the github so you can get access to this project and also the relevant pages on the documentation so you can go and research into it further if you wish to do so so in the project i've duplicated in the tutorials folder the previous tutorial about connection approval so that we still have this ui for a password hosting and joining as a client and i've got a copy of it over here for remote procedure calls and it's simply just the same scene with everything set up the same way the only thing that's different is if we go into the scripts folder we've got ourselves an empty script called particle spawner all there is in it is just a reference to a prefab which will be some particles and if we go back into unity to the prefabs we have an extra prefab called particles i'll drag that in and you'll see it's just this really simple blue effect it really doesn't matter what it is but our goal for this tutorial will be to hit a key and see that particle effect spawn underneath our player and all the other connected players should also see it too but underneath our player and when they hit the key everyone should see it under their player so we're going to need communication between the client and the server and then back to the clients because clients cannot communicate directly with each other everything has to go through the server so let's delete the particles from the scene and if we go to the player prefab you'll see here i've added that script the particle spawner script and i've dragged in the particle prefab there that's all there is right now and that means we can start with the coding so like i was just saying about needing communication between the clients and the server we're going to need to be using mlapi mlapi.messaging and of course unity engine at the top make sure you've got those for this all to work and then to actually unlock those features such as sending messages back and forth we'll need to inherit from network behavior instead of monobehaviour now don't worry network behavior itself inherits from monobehaviour so this can stick on a game object just like we've done previously we've got it in a prefab and if we look down here we've got all this extra data we can check uh the id of the owner client we can check are we a server what the current tickers are we the owner of this object and various other things as well as methods down here now let's back out of this and go back to our script so by inheriting this the only requirement is that the actual object that it's on has to have the network object component which it already does actually because it's the player prefab that we spawn in when you connect so by having a network object component this component here can inherit from network behavior and all should be good so let's start working through the logic step by step so like we said we want to hit a button and do something so we'll go into the update loop because we have to check every frame whether we've hit that certain key so private void update every frame we want to check a key so we're going to say if not input dot get key down get key down and we'll use the space key so keycode.space return so we'll say here if we're not hitting the space key then don't care just return but the issue is this is attached to our player and let's say we've got free players this will be running for all three of the players and i don't need to check if i hit spacebar for the two other players or however many other players there are i only need to check if it's my player so before this line we can actually return if it's not belonging to us so we can say if not is owner which is part of the network behavior then we can also return here so the first thing we're checking anyway is does it belong to us if it doesn't belong to us return then we say are we pressing the space key if we're not pressing the space key then return but if this player does belong to us and we are pressing the space key we want to send a message to the server to say hey please spawn in the particle prefab so of course with the rpc you can do whatever you like here you could get it to play a sound effect spawn a particle effect like what we're doing uk to do 10 different things and then spawn a particle effect at the end you can write whatever logic you like because it's just going to be a normal c sharp method if we make over here private void and we'll call it spawn particle and we need to actually end this with a server rpc because that's just a requirement by mlapi into a method here and the one other thing we need to do is add an attribute so on the line before the method inside of square brackets you want to type server rpc like so now if we take this method and we call it up here so what actually happens here is the client will execute all the stuff in update and then it will reach the spawn particle server rpc method and instead of executing it on the client side which would normally happen because it's a server rpc this will actually just send a message to the server and then the server will run this method and by default only the owner can call this now we have our own check for this but that's not full proof someone could easily hack the client and send this if you're not an owner but the server knows if you're the owner or not and by default it only allows the owner to actually call it if you want to disable that you can say in here require ownership equals false which means that literally anyone can call this rpc but we want it on by default and it makes the most sense for most scenarios in the game this is also a relatively simple rpc we're just calling it and then doing some logic here you can if you wanted to send across let's say the number 10 and here it can take in an int number like so so you can actually send data across and we'll be using that in future tutorials when we need it but what we're doing now we don't actually need it so you could read more into that on the documentation if you'd like to do so i don't want to just cover every single use case in this one tutorial we're going to keep it simple first and you can add more you could add a string word and then of course you could say 10 and then pass in a string and that would all work and the server would receive that now you can't just pass through any data type you like there are limitations which are written out on the documentation you can extend it to have custom types but of course that's a topic for another tutorial so for our simple example here what does the server want to do it actually just wants to broadcast the message to all the clients so let's now make a client rpc so private void spawn particle client rpc and if you didn't guess it already we're going to add the client rpc attribute and the way this method works is very similar to the other one it's just reversed so a client will call this and the server will execute now if the server calls the client rpc it will then run this method on all clients that's the only difference really is there's only one server but there are many clients so all the clients will run this method down here and this is where you want to spawn in your particle effects so instantiate the particle prefab and we'll just do it at this player's position with this player's rotation let's say and that's really it for the full flow there is a little bit extra we'll be adding here because if we just look back at the flow make sure we're the owner if we hit space then spawn some particles or tell the server send a message to the server to say i want to spawn particles the server receives it and it could do checks here it could just say return and it would it would never do it would never actually do the action um in our case we do want to do the action so we will then call the client rpc which will then run on all the connected clients and they will all spawn in the particle effect with this in its current state we could actually test it and it might seem to work completely fine so we can go back into unity let it compile and we'll do a quick build so i've done a few builds i'm going to host connect as a client connect as a client and let's say i hit the bottom one and hit space you'll now see the particle appear under my character for all the connected players i'll go up to the top right hit space you see it for the middle one top left hit space you see it for the first one i can also span it you know it works pretty fast and yeah that's what you'd expect so that all works but there's a little bit more we can do because let's say we are the player hitting the key now what we do is we hit the key tell the server server tells our clients then we spawn it in and for ourselves we don't need to wait for the server to tell us that we've spawned it in because we know we're already doing it anyway all the other clients do have to wait but we don't so what we can do here is we can actually say just spawn it in for ourselves at the start and if we left it like this it would work kind of but we'd see an issue where we spawn it in but then when the server tells us we spawn it in again so we actually spawn it in twice so all we need to do is down in the bottom method we just say here if we are the owner just like we did at the top if is owner return and the reason we return here is because we've already spawned it in for ourselves so we don't need to do it again so now this is instantiate is only running on all the other clients and that makes it more responsive for the player because you don't want to have to wait until it comes back if you've got a gun in your game and you're going to shoot even though the bullet damage will be calculated on the server side the muzzle flash and the smoke and everything that can all be done immediately on your own machine so it's something like this now there's one more thing we can change which is reliability and by default the rpcs are reliable and what that means is that if there is a failure in sending the rpc it will just try and send it again and it will keep trying until it succeeds and for most things you might want this if they're important but for just spawning in particle effects or playing sound effects or those kind of things it's really not that important if there's an issue just forget about it right it's okay so what we can do is we can say here delivery equals and then it's an enum so rpc delivery dot and then either reliable or unreliable so we're going to say here unreliable because by default it is reliable and i can just copy this and paste it down here so it means that our message from the client to the server is unreliable if it fails then don't bother sending it again and same for the server sending it to all the clients if it fails for some reason then just don't worry about it so this is better for performance assuming there's an issue with sending it but at the same time you technically might miss a call of it if there is an issue but you're not too worried because it's just a particle effect and with these changes now we can go back into unity and test it all out as its final version and one other little thing i've done and i'll be doing it for all tutorials now most likely is adding comments to the various logic in our code so that if you're looking at it at a later date you can read through it and it should all still make sense so you can see why we do various things in here so the final test to make sure it's all still working with those changes of reliability and adding the owner check i'll hit space everything still works you actually see on my own it's very responsive it's literally immediate because in the same frame as sending the message i will spawn it in for myself and then everyone else gets it when it gets to them so you see over here player two works player free works everything is all good so yeah that's it for this tutorial we now have messaging back and forth between clients and the server it's really up to you to implement it based on whatever genre of game you're playing you'll be using this all the time regardless of the genre so it's a very useful tool to know how to use you can't really build a multiplayer game without it really if you enjoyed the video or found it useful then please leave a like and subscribe let me know down below what you want to see next and also feel free to share this with other people you think that might find it useful thanks as 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 patreons and special thanks to francisco libra liz kimber sahila verdo die cat from garfield david mcdermott evan maxey yaris letter casey cutting command lauren simpson melvin mike troupe rec san marcos offgrim andrew chris diplock fury and dario if anyone else is able to help support the channel monetarily link to the patreon is down below if not there are also 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 thanks again for watching and i'll see you in the next one goodbye
Info
Channel: Dapper Dino
Views: 21,430
Rating: undefined out of 5
Keywords: unity multiplayer, unity multiplayer tutorial, unity multiplayer 2020, unity multiplayer 2021, how to make a multiplayer game, unity multiplayer game tutorial, unity networking, multiplayer networking, unity multiplayer game development, how to make a multiplayer game in unity, new unity multiplayer, new unity multiplayer system, rpc, remote procedure call
Id: 6zBsPSww2u4
Channel Id: undefined
Length: 12min 38sec (758 seconds)
Published: Thu Apr 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.