Godot Multiplayer - Player Data | Godot Dedicated Server #4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this godot multiplayer tutorial i'll teach you how you can create a player container on the game server that will be storing all the player data that we'll be needing in the various functions both client and server side let's get started by now you're probably sick of this picture of this network architecture because you want to get coding you want to get your game done so i got you covered now in this tutorial we'll focus on the front end we'll focus on the connection between the game server and the clients at the end of this tutorial series we'll want to have stored the player data on authentication server as that is the safest place to put it however we haven't established a connection between the authentication and the game server yet so we'll leave the back end of the player data for another tutorial now let's get coding on the game server the first thing that i've done is i've created this new scene it's a simple node called playercontainer with a script and that script only has one variable called player stats for your game you're probably gonna need more player inventory player money player skills you name it whatever you need for the sake of this tutorial we'll just be taking the stats as an example i've saved this new scene in my file manager on the scenes instances that's because we're going to be creating an instance of this scene for every player that connects to the server how we do that well for that we're going to go back to the main note the game server and as you see i've already added a new node here player verification we'll get into that in a moment let's first see how we actually get to that node whenever a peer connects and remember this is the game server so when a client connects to the game server it has already been through the gateway through the authentication server positive authentication back to the gateway and then it connects when it connects or when anything connects a peer connected signal is fired and then the function pair connected will run we already were printing the player connects um from a previous tutorial now we're also going to be taking the player verification process and we're going to run the function start and we push the player id that comes out of this signal that player verification process is nothing more than get note player verification so basically we're going to run the start function on this node right here so let's switch to this script under the player verification note we have this big uh box of yellow text this is where we'll do the token authentication or token verification i should say that i've explained in episode number three for now we're just going to be assuming a positive token verification that way we can start coding all kinds of things and we can worry about the back end later so assuming a positive token verification we're going to be creating a new player container and again we push the player id under the create player container we simply first define a new variable which will be an instance of the player container scene the player container scene is basically a pre-loaded resource right here of the player container that i just showed you the first thing we'll do once we got that incest is we'll change the name so the name player container will be changed into the player id converted to a string that is going to be very important to easily access all the information stored in that container later on in the code in all kinds of places the moment we have that instance renamed we're going to be adding it as a child to the parent so we're going to be adding it to the game server and normally when you have an add child command you only use one of argument which is the instance but now we're going to use two arguments because by default the second argument is false if you want to know more about that you can go into the documentation of add child and you can read for it yourself this true or false basically says if the when you add this to the note to the scene tree whether human readable names should or should not be used and with true we're going to be using human readable names and it means that this name we have given it to the to this instance here is for 100 certainty going to be used otherwise there's a slight chance that godot might change that name and if it does it will screw our code up quite badly so that's why we got the true argument here then we're going to be defining that just added player container as a variable call player container and then we're going to run the function to fill the player container with the data that we need to go in there and we use the player container as an argument here so we're going to select that player container that we just created we're going to equal the player stats that's the variable on the container and then we for now use server data test data stats that is nothing more than a little piece of data i've added here to the server data but this data normally has to come from the authentication server but again we don't have that back end yet so we're just going to be making use of this temporarily data set and you could do the same if you think like well i first want to code my game a little bit get my feet wet get used to this multiplayer api before i get into data storage and sql and all other kind of stuff you can just temporarily during your development have a little data set like this right here before you actually start getting into the whole networking part so that might be a choice to make sure you don't have to learn a thousand things at the same time so with that we have our player container so if i'm going to be running the server and i'm going to be running the game we're going to be logging in with our password and username we log in we are successfully logged in now if i now go to the server and i'm gonna go to the remote scene explorer to see which nodes we have we now see our game server player verification combat and here we see our player container with the pair id where our stats are now going to be loaded and now of course we have to start working with these stats and do something fun with it there are two things that i want to do with this player data the first one is i want to extract data from the game server to the client that is necessary for example when the client or player opens this stat user interface panel we need to populate it with the right values and these values are only known by the server so we better extract them from there the second scenario that i want to show you is when the server is actually needing some of this player data in case of calculations or mechanics or formulas so whenever we shoot that ice pair that i showed you in episode number one the server calculates the damage right now based on the spell type but what about we also include a modifier based on the stats of the specific player that fired the ice pair in the first place those are the two scenarios i'll show you let's get started first the player stats whenever this panel is loaded into the world the ready function on its script will run under that ready function we immediately call the game server singleton with and we'll run the fetch player stats function which will start communicating to the server to retrieve those stats now there's not a couple of things rna so let's go over them i have defined all the labels that hold the values of the stats into variables so i can easily call them down below here this load player stats function is going to be called whenever the data is cus has come back from the server so this is going to be called by the singleton and of course then the stats will be on there having a look at this game server singleton so we're still on the client we have two functions we have the fetch player stats that we were calling which calls the server and we have the remote function that will be called by the server when it's done with processing the request which will then get the node map graphical user interface player stats and we'll run that load player stats function that we just had a look at now if we switch to the game server and have a look here we now have this remote function on the game server script so that is right there we have the player id that we defined because we need to know which pair was actually requesting his stats then we define the player stats as get note string player id dot player stats and this is where it's so important to rename the player container to the peer id because now i can very easily access with a hundred percent certainty the right node for that player that just connected so we're going to be requesting the player stats that's that single variable that we filled up in the container and then we rpc called the player id again we run the return player stats but which which will then of course load them so if i restart the server and i restart the client i'm gonna do a quick login with our test account we log in and if i press c for the stat panel now you'll see that our strength fatality dexterity intelligence and everything are now loaded based on that data that has come from the game server so that's this stat now let's have a look at ispear and how we can upgrade that with a modifier to add that modifier to our skill damage we have to be changing the fetch skill damage function on the server so i'm on the game server right now now the first small change i've made compared to episode number one where we have created this function is that here i was calling get note combat and then to get this combat note right there but just like i've done with the player verification process right here i've defined that get note combat as a variable combat functions so it's easier to call as we'll probably be needing it a couple of times in our multiplayer game so we call the combat function or comment note combat script right there we call the fetch skill damage and we push that skill name that we got from um from the player however we also need that player id in order to get the right stat for the modifier so i'm also going to be calling the player id or pushing the player id to that fetch skill damage function right there then i'm going to be switching to the combat script and just like skill name this also needs to know you're going to be receiving a player id right now and right here i'm going to be adding a little piece of code so right now we got the basic damage that we've programmed in here in episode number one now we're going to multiply that by a tenth of get note um get the parent get string to player id and then we're gonna get the player stats and we wanna only get the intelligence part within that player stats so we're going to be multiplying our i believe 86 damage with a tenth of the intelligence so if we have a quick look at the server data intelligence is 24 so that would be 2.4 so 2.4 times 86 is going to be i think a little bit more than 200 then we simply return this damage with those small changes i'm going to be re-running my game server re-running my client i again have to be logging in we'll log in and now when we fire a spear right here in the output you can now see that we do 206 damage instead of 86 so that worked and this is another great way or great example how you can use those player containers to access that information now one last thing that we have to do is whenever this player disconnects if i then have a look at the game server again go to the remote you see that the peer container is still here but that's of course not what we want because this now right now the player session is over the players i don't know he went off go do something fun maybe have dinner it's not logged in anymore but if we continue to do this we're just going to have more containers and more containers and more containers and at some point we're going to have a problem because our server will crash because it's out of memory or it will by accident have a new peer id that is going to be matching an old peer id so we need to free these resources up we need to get rid of this container so let's work on that to clear our memory of all those containers i'm here on the main game server script we have been creating this a new player container whenever a pair connects so it actually makes a lot of sense to clear the memory whenever a pair disconnects so when a player disconnects and we have that player id that comes with that signal we simply get the node the string of player id and we create free the player container now of course we also have to be saving that data so we probably need to expand on this function but right now our player data is a simple piece of data that is not really in any sort of database yet and it's not even connected to the backend with the authentication server so before we start coding all kind of elaborate save and auto save functions let's first work on that back end in a future tutorial and we'll focus on how we can store and save account data probably with that episode or after that episode that was it for today guys hope you like it if you did smash that like button hit subscribe don't forget that little bell icon to make sure that you don't miss out on any future episodes of this multiplayer tutorial series in the next tutorial we're probably going to taking a look at the token verification part of the back end it's not the most fancy the most cool or the most exciting piece of code we'll probably have to be riding but we have to get it done at some point so i hope to see you there and until then keep on gaming keep on coding see you later guys
Info
Channel: Game Development Center
Views: 7,132
Rating: undefined out of 5
Keywords: Godot Multiplayer, Godot Player Data, Godot Multiplayer Tutorial, Godot Multiplayer Player Data, Godot Server, Godot Dedicated Server, Godot Multiplayer Server, How to make a multiplayer game, Godot MMO, Godot Beginner Tutorial, Godot 2d Tutorial, Godot Tutorial, Godot
Id: y_3f_QXmvU8
Channel Id: undefined
Length: 13min 59sec (839 seconds)
Published: Sat Oct 03 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.