Ep. 126: Serialization - Game Programming

The Cherno · Intermediate ·10y ago

Key Takeaways

The video discusses serialization in game programming using the Rain Cloud library, covering topics such as serializing and deserializing data, hot code replacement, and separating data from code. The video also provides steps for setting up the Rain Cloud library and using it to serialize and deserialize data.

Full Transcript

hails of guys my name is ash owner and welcome back to episode 126 of game programming so it's been awhile since the last episode we had the whole Civilization series there in between so the first thing I want to start off this video with is just saying that if you guys haven't watched the Civilization series I highly recommend you go watch that right now because that's part of this series as well I just wanted to make it a standalone series so that people not watching game programming could also watch the Sarai's Asian series and you know enjoy that so there'll be a link in the description to the playlist for all of those videos that series is complete basically the last time you would have remembered that we left off well I mean we created like this net player thing and we just ended up having this kind of fake player here in in the middle here of our level and the biggest thing that we were kind of trying to solve I guess was being able to send data of course because we're doing multiplayer now we've started we've started our long journey towards implementing multiplayer and the biggest kind of aspect of multiplayer of course but from the actual network code is the actual transmission of data right so we need to be able to both send and receive data in some kind of format so what we did in that Sarai's Asian series was create that format and create a whole library really that would allow us to serialize and deserialize data right that's the whole idea of that right so we created this thing called rain cloud because it's kind of like rain which is the name of our game plus cloud because you know it's like on the cloud it's like a network thing anyway it was pretty funny it was pretty good name anyway but the wall that aside and we created rain cloud and that was like our library that we could use to basically set a bunch of like fields like we could pretty much like I mean ultimately you could copy pretty much any objects higher like any object that we have in our game we could kind of I guess convert into this format and then send that over the network to someone else who could then kind of receive that data D Sarai's it then actually use it right and that's the whole idea of how multiplayer is going to work of course it's just going to be basically packing the data we want to send into this format sending that format as a bunch of packets and then you know converting that format into something that we can use on the other side okay that's it so that being said go ahead and watch that series if you haven't already because it'll teach you a lot probably but if you like what I'm gonna do right now is I'm going to pretend that that series never happened I'm just going to basically go to github.com full slash the Cherno four slash java civilization which is the actual github repository this is again i'll leave a link in the description to this as well but basically this is all of the code that we have this is all we need in we've actually got two things here we've got sandbox which is um kind of an example use case which we might actually refer to because i have kind of forgotten how to use this in a while but then of course this rain cloud stuff is actually the code that we're looking for so we're gonna set that up right now inside our rain project because we want to be able to use that library to well you know to convert all of our objects to love it all of our data into this format so that we can then send it so the first thing we'll do is i mean you can clone the github repository if you want i'm just going to download it as a zip file because I don't really care about maintaining any kind of like this is never gonna be updated probably but also I just literally just want the rain cloud civilization project here so I'll copy that I'll go to my workspace which is basically just the location of well this particular workspace you'll notice I only have one project in here that I would recommend that you well essentially a workspace in Eclipse works kind of similarly to how a solution does in Visual Studio right if you're familiar with that for some reason then basically that that's kind of how it works right so every time you start a new project like if it's a if it's a fairly if it's a fairly large project like this one is for example you'll probably have a bunch of different projects in your kind of workspace which all kind of link together right so like if I for example wanted to make some tools like to help us edit levels or like a level viewer tool I would also make like it as a separate project inside this workspace so the way that I treat workspaces is essentially like visual studio solutions in the sense that this is a collection of projects that relate to kind of one you know one main project right and in this case that project is rained so that being said you can see that inside this workspace I only have that one project and on what I'm going to do is I'm gonna copy that rain cloud civilization thing and paste it into in here right and so this actually just contains the project is zooming go like class path projects but pretty much everything I want here and then all like all I really want to do now is since I'm already in this workspace and I can close that other thing all I really need excuse me only aura all I really need to do is just create a new Java project which has the same name right and it'll automatically configure everything and import that project into this like workspace because the other thing that's missing right now is I mean inside I'm not a hundred percent sure where this is in Eclipse because Eclipse like just loves to like look at all this stuff there's so much stuff going on here but somewhere in here it's got a list of projects that are inside this workspace and all of all we really need to do is just add this existing project into there so the the best way I found doing that is just making a new project with the same name rather than importing an existing project which I mean I mean I remember back in the day used to be terrible maybe it's good now but this is also easy so name it the exact same thing as this folder which contains your source directory and your class path on your project and then just hit finish okay cool so you should be able to browse the code and everything should be fine we should have pretty much everything that we I mean this is just a library that this doesn't have like a main method or anything you might be able to run this but you also don't need to run this we're just going to use it in our other project so the big question now is how do we actually get you know this rain cloud civilization how do we link it to this rain project so that we can actually use it inside there's some inside this you know this project or inside our game so the way that we do that and again I covered this in like the first a second or I don't know I covered this in one of the utilization episodes as well but it's really easy you just need to right-click on the project on rain in our case hit properties you've got to go to your Java build path projects and then just add this rain cloud civilization as a project to this okay so basically this will just set it as one of the required projects like for the build path overall for the building essentially so if we hit OK you'll notice that whatever name space we had here or whatever package we had in here which was combed off the Cherno dot like a rain cloud for example you can see that now it's actually like accessible okay so what we'll do is well I mean right now we'll just test this out I didn't want to make this episode too long in fact I'm gonna try and make shorter episodes and put them out more frequently rather than longer episodes less frequently because I feel that I'll keep you guys more engaged and also it'll be easier for me so what I'll do is just for this video we'll we'll kind of just I guess use this so I'll go back to the example real quick so in the sandbox we've got main I think was just really just or Sam walks these look at this him anyway okay so here we go so what we did was okay so the the approach that we took was creating actuals it was like an actual mythical serialize and deserialize in every single class that kind of needs to be serializable which is fine and then we just did so created a database we created an object we out of certain strings fields blah blah blah and then we serialize it to a file and then the loading side of that is so I'll refer to this so we'll go ahead and let's just let's just for example grab our I don't know what something okay let's just sterilize like this cratons width and height just for fun just to test this out really so we've got I'll make a quick private void save we'll call that so we'll make a database we'll call it screen remember this is just the name of the database not the file we will add an object so I guess we need to create an object first we'll call it dimensions or resolution about that we'll add two fields to this so we'll add a field called width actually we need to create this using hace field into just the two integer fields one will be called width and we'll have a value of whatever width is will have height as well and I'm probably missing got an extra bracket there extra parentheses so then we'll add the object to the database and then we'll just do database serialize to file we'll put this in our resources may be data and I'll call this screen beam or something okay so done so if I launch the game now looks like we get an error because isn't final file specified that's odd does this follow me to talk because the direction it needs to exist yeah this is a bit of mine I think the directory just needs to exist so if we go back here rez data then that works okay cool so now you can see that inside data we should have the screen up in thing and if we open this and like sublime text or something you know well actually I don't know why I ever did that's pretty useless isn't it in our little hex editor h XD how to recommend there you can see we've got screen well of last a DB with height set to whatever and then if we wanted to deserialize that just for fun then we can make a quick load thing so for example let's quickly bring this out into a private void set size right and I hope this isn't like some kind of okay that's fine we'll cut this and we'll do set size so we'll do this I'll see database DB called RC database dot easterlies and file now I believe what this does is if we give it res data screen bin I believe what this will do is unfortunately not I was hoping it would just okay well we can modify it but then I hope you want to find it oh I assumed we can just return like now or something if it doesn't work out so in this case if we do something like this instead of that catastrophe then we'll just say that if DB equals null then we will take the existing worth and higher which we have already set up by this width thing and hunt otherwise or in fact if TB doesn't equal null here's what we will do instead we'll just do with equals I guess scale is something we probably want to save as well just scales done so we'll do something like with equals DB dot so I guess I should get the object right so ask the object objects RC object dot that's not what I want at all DB don't get or find object name will be resolution object I have no idea how to use my own API find field like yes and then how do we get as an integer I'll get int right so we'll find the field of width height and scale done okay so we'll deserialize them if it exists if not then whatever okay so now the purpose of this is let's okay let's do the safe first just quickly so that we save scale as well but the idea is that if we were to you know do the save here or something I think I made that load yeah we'll get overload if I run this now hopefully what you'll see is the same result right however now it's actually reading it from the file so if I was to edit this now this is going to be a little bit difficult to edit because of binary but we'll do it so let's say where's the width width and then was it was the actual width I guess this is the actual width no idea I guess there's these four bytes of the ends other width okay so whatever let's just change DC to like AC and save that I'm no idea what its gonna do okay that worked kind of okay that did work the reason that it didn't work full is just because of our thing you can see that the window that I opened was less wide yeah which is the idea because I reduced the width but the reason that we had the crash was because this buffered image and all this stuff that gets created actually gets created based on the size so we need to fix that and we can fix that pretty easily just by doing this right for now at least this will kind of set the size I guess so if we run this now hopefully okay you can see it works right we've still got like rendering errors that's to be expected because we don't support resolutions like that but you can see that by doing something like this we can of course change this so AC let's make it really small let's make it like three or something because it's nice and narrow now right so I think DC was the initial thing okay great so you can see kind of how we can now serialize data very easily it's incredibly easy to both save data and then also load data and of course if we didn't have this file like if this file word should get deleted for some reason then it would just automatically whatever this is so you can see the file doesn't exist right now actually would have gotten recreated so far doesn't exist right now and we ended up with a very wide screen okay cool so anyway that's the way that that works no matter normal you can see how we can easily kind of use this realization in library to you know even do really really simple things the other really cool thing that we could do is ultimately use this as like a data kind of tool so to speak right so in other words instead of us instead of us taking in certain parameters like we're not really modeling gameplay right now but if we were then this is just a desire to like inspire you guys I guess but like for example wetware you know working on particle animation instead of having to have all these values all these magic kind of values here that we use all the time that you know are only really tweakable this isn't as big as the problem in Java by the way because really it just means that if we want to tweak them we can just tweak them hit control s and it'll automatically like hot replace our code for us but the idea is that obviously if this was like C++ or really anything except for Java then having like as their I'd just like a sterilization file that we could just reload that had all of our data and there would be awesome because it means that we can kind of even like even go as far as to create a tool that we could just use to edit values inside this database instead of having to use a hex editor or something to tweak certain parameters and then see them applied in the game in real time right but again with Java we can kind of do the hot code replacing so it's not as big of a deal but it's still pretty cool and the reason you would want to do stuff like that is because of course it just means that you're separating your data from your code which is pretty much always a good idea because you know for example if we were on to it essentially the whole reason you you want to separate data from card so to speak is because like daughter is more or less like an asset right like certain parameters in your game for example like the statistics of enemies or something and like potentially you know like how powerful certain weapons are for example that stuff is not really at all the programmers job right it's it's usually like done by the designer or something and a those people usually you know don't don't want to be editing source code even if they can write code they don't want to be editing source code but also it means that if you kind of separate those not only does it mean that they can kind of edit a certain they can use a certain program like a certain tool or a certain even just like notepad or something to edit certain values and tweak them outside of the game source code it also means that they are literally separate files and so they can be updated without actually changing the binaries of the game which is also important right so the idea is that tying data took kotas not always it's not it's not ideal and by doing something like this it also means that you know we to an extent you know all you have to do is just you know that's screened up in file you replace it with something else and it will do whatever that file says right so it becomes really really convenient and of course as we explore networking and multiplayer we also realize that it also means that we can send data pretty easily you can see the like saving and creating a new database and saving you know three fields until it was as simple as this right and I mean that creates a file that creates quite a tiny file as well like that bin files probably like I don't know first we can probably check if we were really keen screened up in I'm picky you eighty four bytes right tiny it could be a lot smaller of course I mean if you you you could go to the extreme of just storing like anyway the point is that we have we have this format now that we can use and we'll we will be extensively using that for networking all right cool so if you guys enjoyed this video please hit the like button a few things I want to mention real quick is if you want to discuss this video in more of more or less like a real-time fashion then I highly recommend that you join my slack I've got a slack account here or a slack team all you have to do is enter your email address and you'll be able to join essentially like what is an instant messenger so here's like an example of the game programming channel it's got a lot of trapped I've got quite a few channels here but basically it's all it's all totally like real-time the game every channel isn't very active against because I haven't probably been putting out too many videos but there are 122 people here so if you guys have any questions we just want to talk about this episode or maybe you have some other questions or problems you're having with your game programming source code then join slack using this if you haven't already and then just go to join the game programming channel and you'll be able to talk about game programming also if you guys want access to the source code of game programming I mean obviously rain cloud civilization is up and get help for free but if you want to access the rain source code then all you have to do is go to my patreon and pledge at least five dollars per month and if you do so first of all what supports me making more game programming videos but also it gives you access to this private github repository where every single you perform saying all the source code over here right you also get to see like pretty much every commit as it goes along so for example in episode 125 you can easily like diff the changes from last episode and it's just really really convenient because yeah I mean if you're into that then it's it's definitely a good way to help me out and also get rewarded for that alright that's it I hope you guys enjoyed next episode will hopefully be in within a week I do want to start punching these out a bit more often and yeah hope you guys enjoyed this and I'll see you next time goodbye you

Original Description

Source Code ► https://www.patreon.com/thecherno Serialization Series Playlist ► https://www.youtube.com/playlist?list=PLlrATfBNZ98cCfmH0xPebdVVMSYRQfyKi Twitter ► http://www.twitter.com/thecherno Slack ► http://www.slack.thecherno.com Stream ► http://www.twitch.tv/thecherno Website ► http://www.thecherno.com Facebook ► http://www.facebook.com/thecherno Want more? You can help out by supporting me on Patreon! https://www.patreon.com/thecherno ------------------------------------------------------------------------------------------------- Website: http://www.thecherno.com Twitter: http://www.twitter.com/thecherno Facebook: http://www.facebook.com/thecherno Subreddit: http://www.reddit.com/r/thecherno/ Steam Group: http://www.steamcommunity.com/groups/thecherno ------------------------------------------------------------------------------------------------- Outro music is by Approaching Nirvana: http://www.youtube.com/approachingnirvana
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from The Cherno · The Cherno · 0 of 60

← Previous Next →
1 3D Game Programming - Episode 1 - Window
3D Game Programming - Episode 1 - Window
The Cherno
2 3D Game Programming - Episode 2 - Game Loop
3D Game Programming - Episode 2 - Game Loop
The Cherno
3 3D Game Programming - Episode 3 - Arrays
3D Game Programming - Episode 3 - Arrays
The Cherno
4 3D Game Programming - Episode 4 - Drawing Pixels!
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
5 3D Game Programming - Episode 4.5 - How Rendering Works
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
6 3D Game Programming - Episode 5 - Playing with Pixels!
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
7 3D Game Programming - Episode 6 - Performance Boosting
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
8 3D Game Programming - Episode 7 - FPS Counter
3D Game Programming - Episode 7 - FPS Counter
The Cherno
9 3D Game Programming - Episode 8 - Alpha Support and More
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
10 3D Game Programming - Episode 9 - Beginning 3D
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
11 3D Game Programming - Episode 10 - Floors and Animation
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
12 3D Game Programming - Episode 11 - Rotation
3D Game Programming - Episode 11 - Rotation
The Cherno
13 3D Game Programming - Episode 12 - User Input
3D Game Programming - Episode 12 - User Input
The Cherno
14 3D Game Programming - Episode 13 - Render Distance Limiter!
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
15 3D Game Programming - Episode 14 - Basic Mouse Movement
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
16 3D Game Programming - Episode 15 - Textures + More!
3D Game Programming - Episode 15 - Textures + More!
The Cherno
17 3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
18 3D Game Programming - Episode 16.5 - Exporting Runnable Jars
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
19 3D Game Programming - Episode 17 - Small Adjustments + Birthday!
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
20 3D Game Programming - Episode 17.5 - Creating an Applet
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
21 3D Game Programming - Episode 18 - The Beginning of Walls
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
22 3D Game Programming - Episode 18.1 - A Few More Things
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
23 Episode 18.5 - Creating an EXE File in Java
Episode 18.5 - Creating an EXE File in Java
The Cherno
24 3D Game Programming - Episode 19 - Rendering Walls
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
25 3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
26 3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
27 3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
28 3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
29 3D Game Programming - Episode 24 - Making Our Launcher Work
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
30 3D Game Programming - Episode 25 - Writing and Reading Files
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
31 3D Game Programming - Episode 26 - Custom Resolutions
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
32 3D Game Programming - Episode 27 - Decorating the Launcher
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
33 3D Game Programming - Episode 28 - Continuing our Custom Launcher!
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
34 3D Game Programming - Episode 29 - Launching The Game
3D Game Programming - Episode 29 - Launching The Game
The Cherno
35 3D Game Programming - Episode 30 - Colour Processing In-Depth
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
36 3D Game Programming - Episode 31 - Sprites!
3D Game Programming - Episode 31 - Sprites!
The Cherno
37 3D Game Programming - Episode 32 - Sprite Mapping
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
38 3D Game Programming - Episode 33 - High Resolution Rendering
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
39 3D Game Programming - Episode 34 - Entities
3D Game Programming - Episode 34 - Entities
The Cherno
40 Genesis - My Game for Ludum Dare 24
Genesis - My Game for Ludum Dare 24
The Cherno
41 Vlog + Ludum Dare Results
Vlog + Ludum Dare Results
The Cherno
42 Game Programming - Episode 1 - Resolution
Game Programming - Episode 1 - Resolution
The Cherno
43 Game Programming - Episode 2 - Threads
Game Programming - Episode 2 - Threads
The Cherno
44 Game Programming - Episode 3 - Game Loop
Game Programming - Episode 3 - Game Loop
The Cherno
45 Game Programming - Episode 4 - Window
Game Programming - Episode 4 - Window
The Cherno
46 Episode 5 - Buffer Strategy
Episode 5 - Buffer Strategy
The Cherno
47 Game Programming - Episode 6 - Graphics Initialized
Game Programming - Episode 6 - Graphics Initialized
The Cherno
48 Game Programming - Episode 7 - Buffered Image and Rasters
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
49 Game Programming - Episode 8 - The Screen Class
Game Programming - Episode 8 - The Screen Class
The Cherno
50 Game Programming - Episode 9 - Rendering Pixels
Game Programming - Episode 9 - Rendering Pixels
The Cherno
51 Game Programming - Episode 10 - Clearing the Screen
Game Programming - Episode 10 - Clearing the Screen
The Cherno
52 Game Programming - Episode 11 - "Out of Bounds, Baby!"
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
53 Game Programming - Episode 12 - Negative Bounds
Game Programming - Episode 12 - Negative Bounds
The Cherno
54 Game Programming - Episode 13 - Timer
Game Programming - Episode 13 - Timer
The Cherno
55 Game Programming - Episode 14 - FPS Counter
Game Programming - Episode 14 - FPS Counter
The Cherno
56 Episode 15 - Tiles
Episode 15 - Tiles
The Cherno
57 Game Programming - Episode 16 - The Map
Game Programming - Episode 16 - The Map
The Cherno
58 The Walls 2 - Minecraft PvP Survival Map
The Walls 2 - Minecraft PvP Survival Map
The Cherno
59 Game Programming - Episode 17 - Key Input
Game Programming - Episode 17 - Key Input
The Cherno
60 Game Programming - Episode 18 - Controlling The Map
Game Programming - Episode 18 - Controlling The Map
The Cherno

This video teaches the basics of serialization in game programming using the Rain Cloud library, covering topics such as serializing and deserializing data, hot code replacement, and separating data from code. The video provides steps for setting up the Rain Cloud library and using it to serialize and deserialize data.

Key Takeaways
  1. Clone the github repository
  2. Download the rain cloud civilization project as a zip file
  3. Copy the project into the workspace
  4. Set up the rain cloud library inside the rain project
  5. Create a database object and add a serialized object with 'width' and 'height' fields
  6. Serialize the object to a file named 'screen.bin' in the 'data' directory
  7. Deserialize the object from the 'screen.bin' file and load it into the game using a 'set size' method
💡 Serialization in game programming allows for hot code replacement and separating data from code, making it easier to edit game parameters and send data over a network.
Up next
George Hotz | Programming | tinygrad, starting on CLOUD=1 | Part 2
george hotz archive
Watch →