Python Online Game Tutorial #5 - Sending Objects With Pickle!

**Network Class Setup and Player Object Management**

The position is okay so next what we'll do is instead of saying data equals read pause and decoding we're gonna get rid of this dot decode and we're gonna put pickle dot loads okay and actually when I'm sending this player object my bad here guys we got to do pickle dot dumps okay and then we're just gonna have to import pickle up here so import pick alright sweet okay so pick a lot loads pickle dumps and then it's obviously instead of pause player equals data we're gonna say player players player equals data and again same concept as before what's gonna happen is the what II called the client is gonna send us a player object we're gonna replace the existing player object with that new player object and then we're gonna send back the other player objects like the other clients okay so now what we're gonna do is just change these pauses to be player players like that okay same thing here players and then when we send it back what we'll do is we will just turn it into a object right so we'll just do that pickle Don dumps and send it back so we'll say pickle dot dumps reply like that and that should actually be about it.

**Server Setup and Player Management**

So now what we're gonna do is essentially we've set up our network class so that we're gonna be able to send that object down it we've set up the server so we're gonna be able to receive that object data we're gonna modify the objects we're storing in the list here and then we're gonna send back the other ones to the other client so in clients all we have to do now is set up player 1 and player 2 and then send that data so really straightforward it's very similar to before what we're gonna do first of all I'll just say p1 we're actually just P I guess is equal to and get P okay because in the network class remember what we're doing is when we initially connect so let's go back to server we're just gonna send the initial player object which is going to be whatever player it is so 0 or 1 so let's just say that this clients player object is gonna be and get P ok and then we'll say actually I think that's all we have to do for yeah that is all we have to do for that inside this while loop.

**Client Setup and Game Logic**

Now what we're gonna do is every frame we're gonna send this player object which we'll be updating with P dot move and we'll just get the reply and say that that's p2 so we're gonna do is when I say p2 equals and then in this case we'll just say P dot or not P and don't send P and that's all we have to do and notice here that we don't have to do with all this like make what do you call it make pause read pause all that stuff and I believe this should be working if I didn't make any mistake so let's cross the fingers and let's try this out waiting for connection let's go to client one run that okay and client two running that and now notice that these rectangles are the same color and watch what happens if I move this red one see how it's red on the other screen as well now that already shows us one of the of doing it this way is that we can store information like color as well not just position and if we want it to store maybe like there is a text attribute on each of these players we could store that as well but we wanted to store more information in the player like a health or something like that be a lot easier to do that by just sending the actual player object that has like an unlimited amount of attributes rather than just sending that little tupple that has like five six or eight which is the position.

**Advantages of Using Objects**

The reason I went through the trouble of showing you the other way is to show you the massive advantage of doing it this way and just to give you kind of perspective if you're making something really simple and you don't use any objects that's how you can do it with string data but I think this way is a lot easier and we've just cleaned up quite a bit of code we've gotten rid of a bunch of functions and moving forward this is gonna make things a lot easier for us so with that being said this is gonna be it for with this video the next videos I'm gonna scrap most of what we've done and we're gonna start working on a legitimate project it's gonna show you what much more in depth how we can do a legitimate game how we can store a lot better things on the server because right now all we're storing is two players but realistically we probably want to store a ton of information and make like a really cool game so that's what we're gonna be doing

"WEBVTTKind: captionsLanguage: enhey guys and welcome back so in the last video we successfully set up our server and clients so that you can see that we can do something like this where we move a square on one of our clients and it's moving around on the other now I would prove this by bringing my laptop up and downloading the code on there and like but right now I don't have my laptop on me otherwise I would do that for you guys but essentially this does work anywhere on your network feel free to try this out if you have another computer download all of the code like all the client code that you need onto that computer run an instance on there then run an instance on another computer and you'll see that it actually does indeed work ok it doesn't have to be on the same machine now there's a quite a few issues that we may run into when we're doing this so I've set this up essentially to be kind of like an example program or like an example problem to give you guys an idea of the way we go about doing things in terms of server and network but it's really not ideal the way that we've coded things so far now I did plan this for to do what we're about to do but essentially I'm gonna redo what we've just done in a much more elegant and nicer way that's going to allow for a better scalability of this program ok so what we're gonna do is I'm gonna redo it here I'm gonna show you how we can actually send physical or not physical but like send objects to the server and from the server back to the client rather than just string data and then in the next videos we're actually gonna scrap all this code that we've written and we're gonna start fresh and code like a networking game there's gonna be a lot more complex than just a few squares moving around the screen so that's my plan for this series let me know what you guys think of that I know it might be little bit frustrating to get rid of this code but now that we understand how a lot of this works it's gonna be really fast to rewrite it in a much more elegant way so what I need to first start by doing it's just taking this player class ok and I'm just gonna copy it into its own file so really straightforward I'm just go to new Python file I'm gonna call this player with a lower case and then just copy that player class in there and just imports pygame up here ok import potter game now I'm just going to go back into client we can delete this player class now and what I'm gonna do really basically it's just from player import player like that okay and that's the first step now remember I said we were gonna send objects so that actually means that we're not gonna need this read pause and make pause thing and it's kind of annoying how we've had to well take that tupple object decompose it turn it into integers and then change object properties and then when we want to send something we got to put it into a string and we gotta send it in just the pain and we don't want to have to do that especially when we're sending tons of different bits of information not just that same positional data right so well you're actually gonna delete this so we can delete this client number I don't know why I have that there and we're gonna start just making some modifications in terms of sending data and receiving data so it'll start on the client side and then we'll go over the network the server side and fix some of that so wherever we see like read pause and make pause we can just get rid of that for right now we don't actually need any of that we're not gonna need this p2x stuff we don't need P to not update well we'll get rid of all this for right now you know actually let's get rid of start position let's get rid of P and let's get rid of P 2 and we're gonna recode all this okay so actually um P to P I can stay there P done move is fun okay so we got rid of all that and you can see we've just cleaned up this file bit and we'll start working with some more stuff in a second now what I want to do actually is go to this network file that we have and we're gonna start making some modifications in here as well so what we're gonna be doing is we're gonna be sending objects so that means we're gonna send like an instance of the player class and that's actually what we're gonna be sending instance of a player class to our server as opposed to sending like string data and then updating the object on or then updating this string data on the server and then sending it back and then updating the object that's just a lot of work it's a lot easier just to send the actual object so we can do that using something called pickle okay now it's a weird module name but it comes default with Python and this allows us to do something that's called serialized objects and that just means we turn into byte information which is like all the zeros and ones send it over the body codes send it over the network and then we can decompose that turn it back into an object and use that it's really easy to do that so what we're gonna do here is we're gonna modify a few things in our network class so first thing instead of having self dub pause we're gonna say self thought P okay this game equal to self dot connect and so saying get position will this to get P and then we returned self dot P okay and that's all we need to modify for that but now in the connect in the send we're gonna change a few things as well so since we're gonna be getting object data what we have to do in the connect is we have to decompose that object data so to do that you do pickle dot loans okay now what this stands for is a sense for load byte data okay and we'll talk more about this as we keep going through but that's essentially what it means and same thing here in send instead of encoding this data what we're gonna do and I guess decoding as well is we're going to dump it into a pickle object and then send it so to do that we're gonna use I say pickle at least one pickle yet don't don't dumps like that and we'll just put data in there okay and then when we receive we'll do the same thing as before we'll say pickle dot loads and then we'll load that in so now essentially what we're doing is we're going to be receiving an object decomposing that object getting the actual object not the bytes form of it and then we're sending it we're gonna first like what do you call it encrypt it like sent it into that byte information and then on the server side will decompose that as well okay so we'll go through that but also we need for the network side so now let's go to server and start making some modifications so same thing here we no longer need this read pause and make pause functions we're not going to be using those and we don't need this pause list either we're gonna change this actually to be players and it's gonna be equal to two new players so notice that we're gonna actually store the player object on the server as opposed to on the client side and this is not only like safer because it means that the player technically can't really mess with the player objects they can only like do commands to update them but it's also just like it's gonna be a lot easier and you guys will see how it works we I said play and we'll do another instance of player and in here what we're gonna do is we're just gonna create two new players so you say zero zero 50 50 and we'll give it a color in this case the first color will be red so we'll say red green blue like that and for the other player we'll start them out a hundred a hundred like before with a fifty fifty and then we'll make his color blue why not so do that okay so now you notice that we're getting a little error for player here just cuz we forgot to import it so you say from player import player like that and that's why I made this new file by the way just so that we'd be able to see it from the server side and the client side as well and then wherever we're doing this like send encoding stuff we're gonna change this so let's do that now actually so instead of con dots and instead of encoding some string information we're just gonna send the player object so what we'll do is we'll say players like this and then player right so exact same kind of concept as before in that we're gonna send the initial like starting position of the player we're like the but in this case we're just setting the initial player object which means any information that's stored in that player will be given to the client as opposed to just the position okay so next what we'll do is instead of saying data equals read pause and decoding we're gonna get rid of this dot decode and we're gonna put pickle dot loads okay and actually when I'm sending this player object my bad here guys we got to do pickle dot dumps okay and then we're just gonna have to import pickle up here so import pick alright sweet okay so pick a lot loads pickle dumps and then it's obviously instead of pause player equals data we're gonna say player players player equals data and again same concept as before what's gonna happen is the what II called the client is gonna send us a player object we're gonna replace the existing player object with that new player object and then we're gonna send back the other player objects like the other clients okay so now what we're gonna do is just change these pauses to be player players like that okay same thing here players and then when we send it back what we'll do is we will just turn it into a object right so we'll just do that pickle Don dumps and send it back so we'll say pickle dot dumps reply like that and that should actually be about it so let's go back or sorry there's something we need to do in client um so now what we're gonna do is essentially we've set up our network class so that we're gonna be able to send that object down it we've set up the server so we're gonna be able to receive that object data we're gonna modify the objects we're storing in the list here and then we're gonna send back the other ones to the other client so in clients all we have to do now is set up player 1 and player 2 and then send that data so really straightforward it's very similar to before what we're gonna do first of all I'll just say p1 we're actually just P I guess is equal to and get P okay because in the network class remember what we're doing is when we initially connect so let's go back to server we're just gonna send the initial player object which is going to be whatever player it is so 0 or 1 so let's just say that this clients player object is gonna be and get P ok and then we'll say actually I think that's all we have to do for yeah that is all we have to do for that inside this while loop now what we're gonna do is every frame we're gonna send this player object which we'll be updating with P dot move and we'll just get the reply and say that that's p2 so we're gonna do is when I say p2 equals and then in this case we'll just say P dot or not P and don't send P and that's all we have to do and notice here that we don't have to do with all this like make what do you call it make pause read pause all that stuff and I believe this should be working if I didn't make any mistake so let's cross the fingers and let's try this out waiting for connection let's go to client one run that okay and client two running that and now notice that these rectangles are the same color and watch what happens if I move this red one see how it's red on the other screen as well now that already shows us one of the of doing it this way is that we can store information like color as well not just position and if we want it to store maybe like there is a text attribute on each of these players we could store that as well but we wanted to store more information in the player like a health or something like that be a lot easier to do that by just sending the actual player object that has like an unlimited amount of attributes rather than just sending that little tupple that has like five six or eight which is the position so the reason I went through the trouble of showing you the other way is to show you the massive advantage of doing it this way and just to give you kind of perspective if you're making something really simple and you don't use any objects that's how you can do it with string data but I think this way is a lot easier and we've just cleaned up quite a bit of code we've gotten rid of a bunch of functions and moving forward this is gonna make things a lot easier for us so with that being said this is gonna be it for with this video the next videos I'm gonna scrap most of what we've done and we're gonna start working on a legitimate project it's gonna show you what much more in depth how we can do a legitimate game how we can store a lot better things on the server because right now all we're storing is two players but realistically we probably want to store a ton of information and make like a really cool game so that's what we're gonna be doinghey guys and welcome back so in the last video we successfully set up our server and clients so that you can see that we can do something like this where we move a square on one of our clients and it's moving around on the other now I would prove this by bringing my laptop up and downloading the code on there and like but right now I don't have my laptop on me otherwise I would do that for you guys but essentially this does work anywhere on your network feel free to try this out if you have another computer download all of the code like all the client code that you need onto that computer run an instance on there then run an instance on another computer and you'll see that it actually does indeed work ok it doesn't have to be on the same machine now there's a quite a few issues that we may run into when we're doing this so I've set this up essentially to be kind of like an example program or like an example problem to give you guys an idea of the way we go about doing things in terms of server and network but it's really not ideal the way that we've coded things so far now I did plan this for to do what we're about to do but essentially I'm gonna redo what we've just done in a much more elegant and nicer way that's going to allow for a better scalability of this program ok so what we're gonna do is I'm gonna redo it here I'm gonna show you how we can actually send physical or not physical but like send objects to the server and from the server back to the client rather than just string data and then in the next videos we're actually gonna scrap all this code that we've written and we're gonna start fresh and code like a networking game there's gonna be a lot more complex than just a few squares moving around the screen so that's my plan for this series let me know what you guys think of that I know it might be little bit frustrating to get rid of this code but now that we understand how a lot of this works it's gonna be really fast to rewrite it in a much more elegant way so what I need to first start by doing it's just taking this player class ok and I'm just gonna copy it into its own file so really straightforward I'm just go to new Python file I'm gonna call this player with a lower case and then just copy that player class in there and just imports pygame up here ok import potter game now I'm just going to go back into client we can delete this player class now and what I'm gonna do really basically it's just from player import player like that okay and that's the first step now remember I said we were gonna send objects so that actually means that we're not gonna need this read pause and make pause thing and it's kind of annoying how we've had to well take that tupple object decompose it turn it into integers and then change object properties and then when we want to send something we got to put it into a string and we gotta send it in just the pain and we don't want to have to do that especially when we're sending tons of different bits of information not just that same positional data right so well you're actually gonna delete this so we can delete this client number I don't know why I have that there and we're gonna start just making some modifications in terms of sending data and receiving data so it'll start on the client side and then we'll go over the network the server side and fix some of that so wherever we see like read pause and make pause we can just get rid of that for right now we don't actually need any of that we're not gonna need this p2x stuff we don't need P to not update well we'll get rid of all this for right now you know actually let's get rid of start position let's get rid of P and let's get rid of P 2 and we're gonna recode all this okay so actually um P to P I can stay there P done move is fun okay so we got rid of all that and you can see we've just cleaned up this file bit and we'll start working with some more stuff in a second now what I want to do actually is go to this network file that we have and we're gonna start making some modifications in here as well so what we're gonna be doing is we're gonna be sending objects so that means we're gonna send like an instance of the player class and that's actually what we're gonna be sending instance of a player class to our server as opposed to sending like string data and then updating the object on or then updating this string data on the server and then sending it back and then updating the object that's just a lot of work it's a lot easier just to send the actual object so we can do that using something called pickle okay now it's a weird module name but it comes default with Python and this allows us to do something that's called serialized objects and that just means we turn into byte information which is like all the zeros and ones send it over the body codes send it over the network and then we can decompose that turn it back into an object and use that it's really easy to do that so what we're gonna do here is we're gonna modify a few things in our network class so first thing instead of having self dub pause we're gonna say self thought P okay this game equal to self dot connect and so saying get position will this to get P and then we returned self dot P okay and that's all we need to modify for that but now in the connect in the send we're gonna change a few things as well so since we're gonna be getting object data what we have to do in the connect is we have to decompose that object data so to do that you do pickle dot loans okay now what this stands for is a sense for load byte data okay and we'll talk more about this as we keep going through but that's essentially what it means and same thing here in send instead of encoding this data what we're gonna do and I guess decoding as well is we're going to dump it into a pickle object and then send it so to do that we're gonna use I say pickle at least one pickle yet don't don't dumps like that and we'll just put data in there okay and then when we receive we'll do the same thing as before we'll say pickle dot loads and then we'll load that in so now essentially what we're doing is we're going to be receiving an object decomposing that object getting the actual object not the bytes form of it and then we're sending it we're gonna first like what do you call it encrypt it like sent it into that byte information and then on the server side will decompose that as well okay so we'll go through that but also we need for the network side so now let's go to server and start making some modifications so same thing here we no longer need this read pause and make pause functions we're not going to be using those and we don't need this pause list either we're gonna change this actually to be players and it's gonna be equal to two new players so notice that we're gonna actually store the player object on the server as opposed to on the client side and this is not only like safer because it means that the player technically can't really mess with the player objects they can only like do commands to update them but it's also just like it's gonna be a lot easier and you guys will see how it works we I said play and we'll do another instance of player and in here what we're gonna do is we're just gonna create two new players so you say zero zero 50 50 and we'll give it a color in this case the first color will be red so we'll say red green blue like that and for the other player we'll start them out a hundred a hundred like before with a fifty fifty and then we'll make his color blue why not so do that okay so now you notice that we're getting a little error for player here just cuz we forgot to import it so you say from player import player like that and that's why I made this new file by the way just so that we'd be able to see it from the server side and the client side as well and then wherever we're doing this like send encoding stuff we're gonna change this so let's do that now actually so instead of con dots and instead of encoding some string information we're just gonna send the player object so what we'll do is we'll say players like this and then player right so exact same kind of concept as before in that we're gonna send the initial like starting position of the player we're like the but in this case we're just setting the initial player object which means any information that's stored in that player will be given to the client as opposed to just the position okay so next what we'll do is instead of saying data equals read pause and decoding we're gonna get rid of this dot decode and we're gonna put pickle dot loads okay and actually when I'm sending this player object my bad here guys we got to do pickle dot dumps okay and then we're just gonna have to import pickle up here so import pick alright sweet okay so pick a lot loads pickle dumps and then it's obviously instead of pause player equals data we're gonna say player players player equals data and again same concept as before what's gonna happen is the what II called the client is gonna send us a player object we're gonna replace the existing player object with that new player object and then we're gonna send back the other player objects like the other clients okay so now what we're gonna do is just change these pauses to be player players like that okay same thing here players and then when we send it back what we'll do is we will just turn it into a object right so we'll just do that pickle Don dumps and send it back so we'll say pickle dot dumps reply like that and that should actually be about it so let's go back or sorry there's something we need to do in client um so now what we're gonna do is essentially we've set up our network class so that we're gonna be able to send that object down it we've set up the server so we're gonna be able to receive that object data we're gonna modify the objects we're storing in the list here and then we're gonna send back the other ones to the other client so in clients all we have to do now is set up player 1 and player 2 and then send that data so really straightforward it's very similar to before what we're gonna do first of all I'll just say p1 we're actually just P I guess is equal to and get P okay because in the network class remember what we're doing is when we initially connect so let's go back to server we're just gonna send the initial player object which is going to be whatever player it is so 0 or 1 so let's just say that this clients player object is gonna be and get P ok and then we'll say actually I think that's all we have to do for yeah that is all we have to do for that inside this while loop now what we're gonna do is every frame we're gonna send this player object which we'll be updating with P dot move and we'll just get the reply and say that that's p2 so we're gonna do is when I say p2 equals and then in this case we'll just say P dot or not P and don't send P and that's all we have to do and notice here that we don't have to do with all this like make what do you call it make pause read pause all that stuff and I believe this should be working if I didn't make any mistake so let's cross the fingers and let's try this out waiting for connection let's go to client one run that okay and client two running that and now notice that these rectangles are the same color and watch what happens if I move this red one see how it's red on the other screen as well now that already shows us one of the of doing it this way is that we can store information like color as well not just position and if we want it to store maybe like there is a text attribute on each of these players we could store that as well but we wanted to store more information in the player like a health or something like that be a lot easier to do that by just sending the actual player object that has like an unlimited amount of attributes rather than just sending that little tupple that has like five six or eight which is the position so the reason I went through the trouble of showing you the other way is to show you the massive advantage of doing it this way and just to give you kind of perspective if you're making something really simple and you don't use any objects that's how you can do it with string data but I think this way is a lot easier and we've just cleaned up quite a bit of code we've gotten rid of a bunch of functions and moving forward this is gonna make things a lot easier for us so with that being said this is gonna be it for with this video the next videos I'm gonna scrap most of what we've done and we're gonna start working on a legitimate project it's gonna show you what much more in depth how we can do a legitimate game how we can store a lot better things on the server because right now all we're storing is two players but realistically we probably want to store a ton of information and make like a really cool game so that's what we're gonna be doing\n"