Networking -- Godot 3 Tutorial Series

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody my carrot came from scratch with a brief hiatus of summer being over and Godot 3.1 taking form today we are resuming the Godot 3 tutorial series now I've been up a reminder on this one there is a home page available over on game from scratch as you can see here all the previous topics we have covered are already there you see we've already covered nodes G script programming 2d 2d animation 2d sprites to graphics keyboard mouse joystick input user interface development sound effects and music physics and tiles and tiles map so we've covered most of what you need to create a 2-d game and we'll be moving on to 3d games shortly but today we are going to cover networking now if you from this homepage which I will obviously link down below you can go in and get the source code and assets used in each example as well as a link to the video tutorial such as the one that we are watching right now on top of that if you are a patron backer first off thank you you folks are awesome but I'm also creating a book which I'll be making for pre-sale very shortly but patrons have been getting chapters as I released them as well as compiled versions of the book in progress as you can see the networking chapter that we're about to cover has already been made available to patrons eventually the book will be available for sale but if you're interested in grabbing hold of it now patrons do get that backing I'll do get that option also I appreciate the backing thank you very much alright well further ado let us jump into good Joe today we are going to be looking at all aspects of networking using Godot 3 and there's quite a few first off we're going to be looking at creating a very very simple client and a very very simple server going into a socket connection over using UDP following that up we were going to look at HTTP request this is you can basically create your own web browser for doing things like rest requests are using web services on the internet or for remotely downloading files if you need it to say download textures off the internet for a part of your application or if you wanted to sell update that functionality is built in then we're gonna look at a slightly more advanced client-server setup and where we're gonna create our own client and our own server and do connection between them so here are some data and then finally we are going to look at the networking that is built directly into G script so Godot has made networking simple for you for C across client and server using a set of keywords that are built into the GT script language so we're gonna cover all of that today quite a bit to get into so without further ado let's jump right in now the very first thing we're going to do is create a simple Network client now we're gonna be using UDP now before we get too far into this I should explain the difference when UDP is ba-dude user Datagram protocol not a really telling name there's UDP and there's TCP TCP stands for transmission control protocol and there are two competing standards for how to send data across the Internet the simple way to think about it is UDP is light fast and unreliable TCP guarantees that your packet gets where your packet is sent assuming of course the Internet in between works but it has more overhead and therefore is slower so if you don't care that the data makes it there and you want to run as fast as possible you use UDP if you want to make sure your data arrives you use TCP and you can use a mix of both and the nice thing is when we get to the higher level stuff you don't obstruct this away completely so it's something you need to be aware of but it doesn't really change the world too too much so let's look at first creating our simple UDP server script now I'm just gonna do this via copy and paste so let me just pull all this code in that way you don't get to watch me do typos after typo after typo so go on into the script editor here in a new project I just read we're gonna create a new script we're going to call this server that Gd and here is oh that does not look good okay one second I got a paste from a better source okay this is a much better iteration so basically what we are doing here is we are going to be creating a UDP connection you can see here with this packet peer UDP then what we do is start listening on the port now it's very important to understand to kind of concepts you need for packet based or socket based communications a socket it basically connection between two computers on a port and an IP address so you need an IP address which is kind of the unique identifying location of the server and you need a pour now you think of an IP address the same as a phone number so you know my phone number might be five five five five five five five five five five and then my extension within that phone number would be the equivalent of a port so you need extension five five five at phone number 5 5 5 5 etc so you can think of the IP address as an or IP v4 address at least as the unique identifying location of the computer you're trying to hit and the port being the value between 1 & 6 5 5 3 6 that is the unique individual you know extension 4 to communicate across so what we've done here is we've used port 4 2 4 - just because I like 4 - for certain reasons and we're connecting to 127 0.01 now 127 dot 0 dot 0 dot 1 is a special IP address it's called a loopback address it basically you're saying on this computer now if you're trying to communicate between two different computers you'd have to somehow get the IP address of the remote computer and then enter it with the loopback address this 127 201 you're saying basically on this computer make a request on this port so that's what we've done here we've crazy Klee started listening so we created a socket connection to start listening on our local computer on port 4 2040 242 and you can see hero's theoretically it should just work and we should start listening on that port and then we're gonna basically just listen until we're done so we're gonna be done when we get the data quick so when you type the word quit we're gonna shut down our server otherwise we basically just read in the socket we get the packet from the socket and the packet can basically thought of a piece of information to use another analogy this is basically like a letter sets in the mail and we turn it back into ASCII from its encoded format so basically we're creating a string out of it if that spring is quit we quit otherwise we print out our data to the the end to the terminal and then when we are finally done we close down our socket now that's important because if you don't close down socket sockets are blocking only one application at only one application can use a port at a time so unless you close down your socket the next you try to run this that port will be in use so we do shut it down when we were done and then we end things now you can if you're on a Linux or Mac computer you can easily send a piece of data over a port using a command line unfortunately in the windows world it's not so easy so this is server and we'll save that so for windows world we're gonna go ahead and create another script now and this is going to be our client and the clients just going to simply send some data copy that out so here file new script and I will call this client like so and in this case what we've done here is basically we create a socket connection just like we did the other time but this time we set destination address so we're gonna reach out and touch somebody the same address can st. port that we created and listened to on the other machine we send our message across so in this case it's gonna be your message here and then we quit now the problem was gonna have here is we can't send that quick commands we're gonna have to do a little bit of trickery so we've got these two competing DD scripts now we're gonna just go ahead and run these from the command line so the first thing I need to do is go ahead and go to temp slash actually here C colon slash temp slash o /b in / I built it from code so we're running you're just basically the path to your Godot executable and we're just running these scripts on the command line so once we've got that path to our executable we're gonna say run this script which is done using the - s command and then it's aware that I put this default slash temp slash here showing filesystem come on here open the file manager all right so there is our path so cut that down over here path to our script and then the first one we're obviously going to want to run is server like this so we now have our server running so you'll see here on the command line see listening on port 42 42 on localhost and we are now listing for a socket connection and we're gonna need another terminal window to actually make the request so once again see colon slash slash doe slash bin slash doe - s paste and [Music] plant so this is gonna send the data and let's go to our thing here so there you see our data was just received across the network so there is a very simple socket based a set of scripts really about as simple as you can make them now obviously we're gonna want to switch that your message here instead to be quick so we've updated our script I'll go back here we'll run our appliance script again now we're going to send that quick data over and it should be done and you see here we've now shut down our server so sending data across pack it's quite simple very straightforward this is probably the simplest example I could make now let's look at using an HTTP request 10 on back into our project we're going to open up our scene and this one's going to be a very soon we're gonna do this one entirely in Godot from this point on no more command line scripts but this is gonna be one of the most easy gooeys you've ever seen in your life speaking of which let's just grab a font and it back in here boom do I need that later so let's go ahead and create a button button and what we're doing now is just getting data from the server so there's our text let's do exactly that get data from server update that font so you can actually see it oh let me wait a minute do dynamic font edit said font now we bring in our data to create the size it 24 ah there we go so there's our button when we click the button we're gonna want to go ahead and get data so now let's go ahead and give our data somewhere to go just throw a label in here this like that alright and I gotta stay consistent with how I wrote it so what did I call this guy I called it label oh yeah that's straightforward enough all right so what we're gonna do is when we hit this get data we're gonna go ahead and predictably enough go get some data from the internet now we're gonna need a URL to use here some kind of web services and there are a thousand thousand thousand web services there might be a few billion open at this plot type but things like Facebook Twitter a lot of ad networks all work with web requests so one example one I have found is this guy this is a simple web service example service so the URL we're using is Jason placeholder dot type I code comm and we're using the service / - dues and we're gonna get record what and you see here when you put that in you get this raw jason file back nothing really special going on but this allows you to basically the service you want to call is to dues the parameter you're giving it is 1 so if you wanted to you see you get a different result back that's the gist of how services work or how a rest-based web services work so we don't want to be able to request this data across the wire so let's head on back over here we will add a script to our button and sure and now the code and once again I'm going to do this with the power of copy and paste so what we have here is when we are already oh yeah so we're wiring up the request completed signal so when our web request is done this function is going to be fired and at the point where it is fired we get a couple of variables back we get the result we get the response code we'll get the headers and body of the basically the text back so you're getting all your HTTP headers and the big ones we're really interested in here are your response codes these are your standard HTTP style response codes so like 404 is not found 302 is a redirect what one we really want is 200 200 means everything was a-ok so that's why we text it if the response we got back was 200 then we're just gonna go get to the body and turn the body from a UTF encodings string into a string that we can then apply to the text field of our newly created label and then your big thing here you'll see is in the event that we press our button we make an HTTP request and send it up and of course I need to actually add that so we need to go on back over to our editor and 2d mode let's get out of full screen mode here for the child I think oh did I make that a child yeah I made a child a button that doesn't make a whole lot of sense but I'll stay consistent alright so HTTP request is a node type that is built directly into the Godot engine and in theory we should be good to go so let's go ahead and run this first run so I need to set up my scene okay code runs data from the server and no results okay no errors give me one second to figure out what I did oh I think I'm just an idiot that's it I'm back over here let's grab our button and actually let's wire up our button pressed I think that's all I did wrong so hook it up here on button pressed well connect it that hits our on button press so now when we press our button it should actually in theory actually do something so now let's test that out awfully there's no more stupid errors and here we go data and there you see so what that did basically went across the internet grabbed some JSON data that we are now spitting out right here now there are also jason encoders and decoders built into big dope so if you want to turn this back into actual dictionary of data values you can easily but this is one way of getting data across the internet a priest before but you will find if you search for rest Web Services there's an absolute huge ton of these now at the same time this same control that we've used here I'm not going to cover today but HTTP requests can also be used to just straight out download a file so you can just say okay go get this file at this URL and save it locally and then you can use it just like any other file in your project so if you want to make some images or textures or things that are remotely accessible on the server or if you want to give the end user some level of customization you can use it via HTTP request to grab and download those files now to be a little careful though when you add this functionality if you're on iOS because Apple in their walled garden they sometimes prevent this kind of functionality well that is simple HTTP request one thing to be aware of this example did not require HTTP if you do require HTTP you have to give an SSL certificate and a great thing is this functionality is built into Godot but we are not going to be covering it today so do be aware if your URL is an HTTP URL you need to use it a court actually in this case I didn't because what I had to do here was basically pass in this false otherwise you need to pass in your your SSL certificate so that the line you're on can actually be encrypted okay so that was HTTP request now I'm gonna go ahead and create another demo with a very simple server and a very simple client that we're gonna build directly in Godot itself now we're altima doing kind of the same thing we did when we made our basic server before but this time we're going to let Godot take care of a lot of the niceties for us it's going to be using an underlying package called a net an open-source package that Godot has done in implementation of so what we're gonna do is create two separate projects one for a client one for the server and you don't need to separate it out this way it's quite common to have a client and server be ultimately the same executables but in this case it made some sense to me so that's how we're gonna go into ahead and separate these two out so right away let's just go ahead we'll reuse this project actually you know what let's do it from scratch so let's go back to our project list and first things first we will go ahead and create our project C colon slash temp [Music] server YouTube so this is going to basically be the computer that we are connecting to we're not gonna do a whole lot here the server is going to be able to send messages out to the connected client and that's kind of about the extent of it but this is going to show you how to manage connections handle connections that'll slightly lower level and how data can be sent across using a neck so now we need to go ahead and create our server we're gonna create a 2d scene like this we're gonna head and add a label we will name the status and we'll add another label and we will call this one label user cap and that is kind of the extent of our UI so UI there but status down here a bit more now let's attach the script to that first the status label so we're gonna go ahead create a new script and attach to it sure and this is gonna be where we set up our networking I know in the grand scheme of things that makes absolutely no sense but in this case it works quite for me and what we're doing here is basically recruiting our network and then we are basically and start listening so this is kind of like what we did before we're gonna create a server that listens on it given port we have an IP address which is your own IP address in this case and then there's a bunch of callbacks that are called when certain things happen so you see right here we've got we created our network and this time we're using network multiplayer Anette as the class for our networking like I said this is an implementation of the Enoch class open source library it is possible to use or create your own networking so there's a layer of abstraction there that could do is providing but the only implementation they currently have is this II met one and then we start things off we create our server this is a lot like what we did before but we're just not dealing with it at the port level we're gonna say listen on port 42 42 and limit it to a maximum of two connections obviously you could Jack that number way up if you wanted to have more people connecting you'll notice here though that networking is inherited from the root tree this is important new treat controls it holds a class called Oh at the heck is it packet peer so it inherits from a class called packet peer it will automatically be part of your tree and the root tree is very important to networking in Udo so basically we're creating our network right here done we're going to see the very similar thing in our client code so well now what we've done is wired up two signals we're gonna listen for connected and disconnected signals in the event that connects we basically just print that out to the we update our total users and we print this out to the status string or to ourself you can see over here we're doing it again you get that you can get the number of people that are connected by calling it tree yet network connected peers so that networking is all wired into the root of that same tree very important to remember that so when someone connects we print it out on someone disconnects we print it out and I think that's it server so in theory we should be able to run our server after I save it so server excuse me alright not the most exciting thing you've ever seen but they're our server is now theoretically running let's go and run our other so now we need to make our client so fire up another copy of Godot and once again I'm all doing this entirely local computer using 127 loopback just because it's a lot easier to demonstrate demonstrating networking over a screen capture his tricky otherwise so now we're gonna go ahead and create another new project I'll throw this one and Wyatt whitey create that fuller create our project and here we go this client is about as simple as they're gonna get we're going to have a pair of buttons button artists button the button alright there's but one yeah we filled up the button connect and there let's grab our font there connect yeah you can read that I'll leave it as it is and then we will oops right-click duplicate and this obviously is disconnect like that and where did my controls go oh there we go now we're gonna do this as a serious of scripts attached to our project so here in our root node when we load up we're automatically going to connect to the server so let's go ahead and do an attached script here sure and we want to do is this so when we are added to the scene when we were ready we basically create a network multiplayer Anette exact same thing as before this time instead though earlier you'll see we did a create server this time we are doing a create client we're connecting to the IP address the loopback address again again if you were using network machines you would use the remote address in this particular case but then also you would have to worry about firewall issues all that stuff not going to deal with it and we're connecting on port 42 42 once again we use the set network peer just to pass in our newly created network so you'll see the code works identically client or server the only real difference being this call or right here and we handle the connection failed event by basically spitting out that a connection error occur and now we can go ahead and wire up our buttons so what we want to connect here attach a script to that guy create and zoom pretty straightforward when we press our button we create the client we're basically doing again the same as Connect before so disconnect this connects probably the more important one to show so a detached script when you disconnect what you do is simply say set network here and you pass him null this will effectively close that port close that connection and you are off to the races so now the only final thing I really need to do is remember to actually wire these guys up and wire this guy up all right so in theory our client is now ready okay so now we have a client that when this scene is shown it will automatically connect oops I forgot to label my text that's confusing all right so over here our server you'll see there shows we have a new user connected and it shows the unique ID of that connection that unique idea is going to be important later on if you want to communicate specifically to just one machine you use that that detail right there so we're now connected we can disconnect and you will see here it will say it's disconnected and we now have 0 users connected so pretty straightforward pretty easy to work with we'll leave our actually I think we're gonna need to shut them both down so we'll shut down our server will shut down our client I'll change this before it gets too confusing so this button instead does a disconnect from now on all right that makes sense so now we can do is give the ability to send some information from the server to the client and this is done via all right so we need a GUI for that so let's go over to this guy yeah all right man we're gonna need to do a little bit of work here so let's go back over here we'll update our wonderful user interface so we're gonna go ahead and add a new child call a line edit which is basically a text box add that into our world this is the data we're gonna send I will call this text to send this is where we enter our text that we want to send and finally we will go ahead and add a another button so I did not want to make you a child there you go button send date then this guy is now going to need a script attached and to send data we simply do this so we're basically encoding the data so first we get the data from the text field and then we encode it to you can use ASCII or utf-8 so we've encoded it down to bytes and then we use this built-in class that is off of the root called multiplayer which is an instance of multiplayer API which is an abstraction of cool and handy stuff you need to do with multiplayer such as sending bytes so this function will send the data across to the server no sorry to the client so all you to do is wire that guy up like this so now our server should be able to send data let's go over to our client and give it the ability to receive data so here go ahead and label put that down here like this we will call this guy did I call this guy they've all server data so so now we need to do is come back in here so we're in this is the when a connection occurs so so far we've only wired up connection failed well now we're need to actually handle another signal that comes in on the network and that is actually it's on multiplayer so we want to wire up to handle this guy so basically when network appear packets arrive we're gonna call this function and in that function oh we are going to do is call this so when that packet comes in we get the ID of the server that sent us the packet and the packet data itself what I'm gonna do here is I'm gonna switch this out and instead this will be label data text equals like that label server data is not oh all right so there we go basically now we've wired it up so that when a packet comes in we print the contents of that packet out to our text field now of course there's no hair error-handling here there's no multi packet so if the packet was too big things get split into multiple packets I'm not dealing with any of that stuff so if you want to dig into it with more detail if you started getting into more complicated stuff do be aware there is more involved I just don't want this video to be outrageously long so I'm leaving a lot of things out in that regard but you get the gist of it you should be able to extrapolate from there so we got our client going so let's fire it up I thought I already did this okay so our client is now running over here let's go ahead and fire up our server over here there we go it's client server there's my button mmm I did something wrong that's it did I not add any text to my button I create the button oh I just sort of stopped mid button okay let's actually make our button work so inspector send data sorry my bad did I wire this guy up I did okay so now we have an actual button to press and let's go ahead start that over again so let's run our server run our client so server is up and running you'll see there the client already started so you see there's one user connected and that user is that connection right there so sure our client there's our client ah so head back over to our server and I'll say how's it going over there and send the data and cup boom all right what did I do wrong I'm now to get index on null okay guess it helps to actually use the same name so what did I call this guy text to send text send all right so I will pause and get everything fired up okay and there you see when you don't have the mismatched names the data is sent across from the server to the client so we can say whatever we want really whatever we send data and there you see the data is being sent across again this is very very simple example but you are seeing two computers communicating with each other using a somewhat low-level version using Enoch now what we're gonna do is get into the GD specifics of the GD script extensions like domain basically what you could do is synchronize a scene between machines this kind of gets confusing at times but hopefully it will make sense when we are done so I'm gonna just jump in shutdown this guy and we're gonna create a brand new project so let's go over this one we'll click to the project list so this example is going to be just one application no more of this a separate executable for client and server even though that is a nice logical way to think about the separation instead what we'll do is we will call this one 1 slash temp we'll call this network name demo and we'll create that out and all we're gonna do is basically create a network game that runs across two computers where you can control movement on one guy and it'll update on the other your troll commute movement on the other guy and you can update it back on the original and that's about all we're going to try and accomplish here but first thing we're going to need to do is create a lobby so that's what we're gonna start with this is where your networking would generally come in this is how you determine who is the client and who is the server kind of like you do with any multiplayer game someone hosts it someone connects in this case we are going to host we're still going to be using the loop back because it's all gonna be operating on this one computer so let's start by making our lobby we will call this guy lobby lobby and then what we're going to do is just basically create two buttons so first button this and default in all right so this guy will host our game that change that font up so you can see okay thirty two points okay that looks good so this is our host game button let's call that I think that's what I called it every time I minimize the screen I lose this guy yeah so we got button host and we'll just go ahead and duplicate this guy and button joy well the greatest lobby you've ever seen in your life but it is most certainly a lobby now what we're gonna do is go ahead and create a script and attach it to our lobby this and now we start implementing our server so where's my mouse there it is ah get rid of all this junk and the first thing you're going to need to do is handle when a network connection actually happens so that is in our ready function we implement this and now we're what we're doing is wiring up the network pure connected to a function called player connected now we should probably go ahead and actually implement that so this is one a player connects to the server when the player connects to the server you notice an ID is passed in this is the same ID we saw print out in the previous example this is a way of uniquely identifying a client on the network and what we want to do is save it in our global heywhat global well let's make that so now we need to create a new script I will call this Global's Gd all I'm doing is creating it as an auto load if you haven't already figured out how to do Auto loads I did this earlier in my GD script 101 chapter so Global's Gd okay so now we got to implement this guy it's about as simple as they get all we're doing is setting the other players ID to negative one to start so we've got Global's available to us they saved my scene that's fine save my script that's fine we need to actually configure that Auto low so you go into projects you go to auto loads you go to add oops you browse to it first so our GD script is automatically loaded so Global's will load when our project starts so this is just basically a global available variable of our connected players if you had multiple players obviously you would just create this as a table or an array of players but in this case we're going to hard-code it to just one player so we're gonna have the server and we're going to have the client and that's it so back to our code so what you see here is basically when the game starts when someone connects to us look the network peer connected event will fire we save their ID and then this is critical basically and this will fire on both machines what we've done here is we've pre-loaded game TSN a scene we're about to make in a few seconds we then attach it to the root we make it the active scene and we hide the lawdy so pretty straightforward basically when up here connects we automatically start the game up now let's go back and handle those buttons it's over the 2d on host press node signals on pressed and we'll wire this into Lobby and we'll connect it there so now we want to implement this function so on button host press yeah wording is the same I will copy and paste again when we host our network exact same thing we did or earlier on basically we're creating an e net account we create our server if everything works fine then we'll hide the join button and disable the host but now we can just see which one is the server visually and this key one here is we also set the network to the host then this is the exact same code that we did earlier on and we're gonna find that the button join is also almost identical so we're gonna go ahead connect that into our lobby but in that particular case the join button we use this code and same logic once again we're doing a create client instead of a create server but already the same thing that we already covered earlier at this point in time we should in theory have a complete game setup now all we need to do is actually have a scene that we can move to so we're gonna go ahead and create a new scene to be a 2d scene like this and let's go back to it and we'll save that scene so this is our game they do plural yeah game so head on back over to our scripts so that is this that is what when our player connects the one that we're going to switch to is the game scene that we just created hopefully this error message will go away now that that is there I don't know how long that's going to take but we should be good to go so let's test things out so we're gonna need and this is where networking kind of gets kind of crap what we need to do is build an executable version to run as either the client or the server because you can't easily have two instances of Godot running the same application where that word it getting a little pissy so we're gonna have to go ahead and build this I haven't actually done this on this machine yet so I'm gonna have to bring in the export package I'm going to cover this process at a bit of a later date so we need to add preset and we're just gonna create a Windows desktop execute well I'm not gonna show you this part this takes a bit of time so I'll be right back okay so a lot of magic just happened off camera but basically I just installed the export templates that allow me to compile for the Windows platform now I'm something I'm gonna cover at a later chapter so if you don't know how to do that I will do a tutorial on this shortly or you can find one online but basically I build my executable so basically just do an export project this will create an executable for us I've already built it so we are now good to go so now we've got a version we can run which I'm going to go ahead and do now so this is the compiled version of my game so here we've got one instance I'll bring this guy up here and head on back over here and we've got our other instance alright so we now have room for our client and we have room for a server so I'm gonna create server over here now so we're waiting on so hosting Network you can see right there so now our client is going to connect to the server and boom we've just loaded up our game now do keep in mind that our game is complete and utterly empty so now let's work on that so let's shut this down and get back to creating our game so game over here there's not gonna be a whole lot going on in game in fact nothing is gonna be going on game so our empty game like this it's all going to come down to the script so let us create that script so here is our game I didn't know if I name this thing anything I don't think it matters but err we'll call this being group just so we know and we will go ahead and attach a script to this guy like this so game and here is the code we are going to use and what we're doing now is instancing two objects one is our local player the other is the remote player and we're gonna have to create player dot t SC n in a second so that's gonna be the next scene we're gonna create so what we've done here is pretty much just created a new instance of a seed that is our player character our player is just gonna be the Godot logo nothing to get too excited about but the key thing to understand here is this guy so setting the network master is this is basically saying on the network the person who owns this machine is this person or more specifically this machine now remember I told you all about those unique IDs that we've got when we connected well this is why we saved them so in the case of our server we're gonna use our local ID we can get that by calling get tree get Network unique ID so the ID of the server will be used in this case but the server is also populating the remote one and this is where our global this is why we save the unique network ID of the client so if you had multiple clients you would basically populate this one in a list but the key thing is that remote client is going to own its own instance of the player ok so that's it that's all we're going to do is when our scene is populated we created the two player object so now we're going to need to go ahead and create that player class so new scene empty switch over to 2d so this is a 2d scene and we'll call this player and let's save that guy what's your issue well can't find player all right that's because scene save this scene as what player all right so we've got our player in the world all this guy really has is a sprite attached to it that and that sprite is just going to be our default I so I need to actually stuff that you'd actually select it first icon and then BOOM so our player is just the good old logo nothing really magical happening here but let's attach a script to our player so player Duk Gd and this is where the magic is going to happen let's get rid of all of you and now let's move over and grab our new networked code all right so our code is in I will come back and look at it in just a second let's make sure that everything is working so first we're gonna go ahead and do an export create a project so there is either a client or a server doesn't really matter and okay as we built an executable so we'll run a version and switch back over to Windows and we will run another instance all right so we have two instances this one will be our host it's kind of important to be the host for the one that you're running from the debugger if you want to see the output results and here we'll join the game so there what you see is not what I wanted okay so this one works we can see our end result this guy did not work so I made an error somewhere give me one second to figure out what I did wrong okay seems to be a bug with the exporter for some reason I developed this code on Mac OS doesn't exist on Mac OS it does on Windows but it's I'm running a beta build of the exporting templates so perhaps it's just that I won't get into the details but I switched out the image that we're using and set the export templates and blah blah blah so we should be fine now so go ahead and run both so in this case I'm going to host our player on the client I'm going to join the game and now you will see over here I move around and you will see that version is being moved on the other person's computer and then over here the same deal so we've actually set it up so we have synchronized game objects between two games with a basic lobby system pretty much everything you need to get started with networking using the good old game enter now let's take a look at some of the details now coincidentally I can also hit the Q key and boom it all shuts down and I'll show you how all of this works now so we gotta head over back over to scripting and this is all attached to our player so this is the object that we instanced in our game so we created a player for of the remote and local machine and each network computer is in charge of its own player object that's very important to understand and that was set using this set network master so now let's look at how the code works pretty straightforward for the most part in no constructor so what we're doing here is in each pass for the game loop we're moving in a wrap so the first thing we're going to check is basically do I own this game object and if you do own the game object we want to pull for input if you don't it means as a network game object and all you want to do is move it to whatever position it's at so you can see the move by are being set this way so what we're doing is if we hit left or right we move five in whatever direction we've come with this kind of code over and over and over again you'll notice also if we hit the Q key we call the function call to shut it down this is what exited the code on all of the machines attached and the key magic is this call right here RPC unreliable and what we're doing is we're invoking the function set position using RPC RPC is a way of communicating communicating across machines called remote procedural calls you can think of it as a way of making a function call able across the network and we have set the position function we have created a set position function that is network callable so how did we do this well when we declared it we call it slave now you'll notice there's a bunch of magic words going on here there's slave master sync and then remote which I didn't use and we look at exactly what those mean but basically all we've done here is we've called set position on all attached clients and that's what this call right here does now those calls are the magic those are where all the network verses to do these keywords this is what explosives functions for being called from other connected computers and you can basically look at it this way a slave is code that will run on the remote computer a master is code that will be called if you are currently so if this is true if you the network master that code will run here so this is basically being sent to everybody we're going to look at how to minimize that in a second now this one sync is kind of magic this is for running it both on the remote and on the local machine and then the final one is remote which would cause the code to only run on the opposite machine so if you call a function that says if it was remote function shut down that code would only run on the connected clients it would not run on the local machine so that is your your four different options for synchronization you've got sync which is on all machines remote which is on only faraway machines master which is which runs on the network master and then slave which is basically everyone who isn't the network master it's you're using this combination of keys of code you can basically synchronize across different machines now one thing that's kind of important here is notice we've got this shutdown call and a lot of times you're not going to want to have everybody and their dog able to issue commands like this you don't want everybody to be able to do code that you only want to confined to the server and the server and the network master are different things network master it's just the owner of the game object between all the share of the instances the server is the authoritative one so if you want to make it so that only the server can shut things down using the Q key we can change this one out to basically just go if and now we want to use the function is network server and that will only return true if you are the server now the server again was defined by the one that is called using create server as opposed to create client so this makes the server authoritative it makes it bit only the code the only the server can run that particular code and using that if statement is basically how you'll be able to limit it so that only authoritative stuff can run on the server for example you want to cut down on cheating and you want to make it so only of that kill player code can run on the server you just put it behind and if is network server check and you are off to the races now the final thing that we're going to cover is this call this is the this is what actually sends the request across of wire what this says is RPC unreliable call to set position so we're looking for a function that has been tagged as networkable so either sink or remote or slave that will call that function across the wire and here is the set of parameters going to it now one important thing is this will actually broadcast to all connected clients now in this case we only have one client one server so it's only gonna go to one person but if we wanted to strictly limit it to just one person you put the ID here and then only that computer will be connected so if you want the server to selectively talk to or only talk to one machine you just pass its ID in with these calls now this RPC unreliable this underscore unreliable part is also important now remember back when we started all this I said there was UDP and TCP and you don't took care of the difference for you well this is how it takes care of it unreliable is a UDP call if you want to call the exact same thing just use RPC the same file parameters you can also pass an ID into it and this will use TCP instead of UDP for making the RPC call now in addition to RPC we've also got our set like this and our set unreliable these also use your PC but instead of calling a function on the other end what you do is pass in the name of the variable you want to update and then you pass in the new value of that variable and then again in this particular case you have the option this will this will update that variable and all connected machines you can also pass in a specific ID and communicate with just one machine at a time I don't know that's a lot to take in I know this whole master master slave sink and remote stuff is a little bit confusing hopefully the RPC and RPC unreliable in our set and our set unreliable makes sense because of the UDP and TCP stuff we already covered but this is a daunting amount of information on that topic I'm already at 50 minutes I'm gonna cut it there one last thing I do want to mention to you is if you head on over to Godot site like so and go over to downloads you'll notice there is a special version available right now on Linux only called server and that's going to be coming to Windows and Mac OS in the future and this is a special version of the Godot game engine that doesn't have the UI or tools built in so it means there's no rendering done on that and so what you can do is you can create your game exactly like we've done here you have all the logic there but it doesn't have the same overhead so if you've got a dedicated server you can use this coding structure and have it synchronized with all the rest but all it is going to do is the server stuff so all the rendering stuff the UI stuff this stuff is all not included in that build so it can host more connection so if you're looking at creating a MMORPG or you have your own dedicated Lobby or networking you can use the headless version with server version of Godot that's just a version of Godot built with the slash server a tag in the build process I'm gonna be covering building Godot in more detail later on but I should already down video on it in the past but I'll make sure I cover server builds in that I've been informed by the developer team that this builds for the Windows server are going to be coming soon as well so if you want to have a server version or a headless version of Godot without all the the UI stuff taking up resources when it doesn't need it on the network end that is an option as well okay that is it that is coming up on an hour we covered a whole lot but hopefully you found that useful hopefully there was definitely a lot of information covered so if you do have questions let me know down below but that is networking in Godot a number of different ways from simple socket servers to slightly more advanced socket servers using a net to HTTP requests to you know using the GD script extensions to do summarization but we created a lobby we created a network game it isn't the sexiest Network game you ever saw but players were updating on different machines so if you have any questions please do let me know again also if you want to get early access to the book that this material covered it is up on patron as is all the projects that this was based on are on the patreon drop box so your support is certainly appreciated there and you guys watching this video is definitely appreciate it hopefully you found something to like hopefully I kept you awake after an hour long all right talk to you all later goodbye
Info
Channel: Gamefromscratch
Views: 49,308
Rating: undefined out of 5
Keywords: Godot, Networking, RPC, GameDev, Game Development, Tutorial, UDP, TCP, HTTP, Lobby, GDScript, Sync, Remote, Master, Server, Client
Id: JuRhRhJ2914
Channel Id: undefined
Length: 55min 20sec (3320 seconds)
Published: Tue Sep 11 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.