Multiple Object Deserialization in Unity (using Chess)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in the last video we which is like there we looked at a chess game and we got our images and we civilized our data for our sort of base game or so starting starting state if you like so where all the pieces are and in this video we are going to deserialize that data and place those pcs on the the board inside the world and we're gonna try and get that done all in less than an hour you know me in the long videos sorry folks I am actually this is actually I'm recording this over an entire day anyway this month after the feed this is where we left off last time we have our serial Iser we have a debug application diva script application I'm a debug script and all it does is it creates our default save game we want to be able to load that save game back in and then we want to display that information so what we're going to do is I'm gonna back to our chess serializer and you see that this is where our save game is so all of this we're gonna take her and we're gonna put in say we're going to refactor this and put this inside one function so I'm going to call this function literally public and string get save location and I am going to take all of this out and I'm going to paste this in here and then I'm going to do returning full path and then I'm gonna say I think I may be able to get interrupted again so our strength fool path is gonna be equals get save location okay the reason I want to do that is because I want to have a load game that takes the data that gets the data from the serialized file and then creates our board game from it our board from it so my a public board Lord game and I don't need a path because the path is going to be set in stone to be this location here so astraying full power it calls and get save location so we have two choices here we either have the file exists or the file the file exists and we load it or the file doesn't exist and we return a new board okay so we can check see if file exists all paths do load file else return create new game so right now then we're gonna read squiggly line in because not all code paths are gonna return something at this stage and it'll tell you that here not all called paths returned a value so we're gonna have to fix that so if the file exists we're gonna have to do our or decent thing in Reverse so we lured all the text first of all file dot read all text and the path is full path and then we're gonna create a board from that which we don't need we can actually just return it back here and then we do return and then it's going to be juicing utility from Jason and then it's board and then Jason so that will then load everything in there or it will return just a pliant game so we didn't really need to do the savegame thing but I'm glad we did the savegame thing because now we have our test data stored there okay all right so now we need to create a we need to create a class that will then take a board and then create all the pcs from it okay all right so each piece is a type each piece has a position okay okay okay I just think here what is the best way to do this well what's our way to do this I'm the main the best way to do this I really want to have something that's got our pawn Knight rook Bishop Queen King I want to have that functionality in there and I want to be able to choose different colors or whatever it is I want to have for our pieces so I think I'm just gonna go with a sort of basic thing just know so I go to here and do our we're gonna call this board renderer or renderer so now the highfalutin name for what it actually is okay oops get up there as a board renderer is gonna take n it's gonna take in a board and then it's going to display the value display the pieces on the board so we need to know what shapes what sprites were going to use so I'm just going to do this brute force just now we can change this at some point later on but this is really the boat serialization rather than about how you organize these particular values so I'm just going to do public black on right rook Bishop pulling and Ric Bishop link I suppose you should do them in a kind of order but okay so that's the black pcs and I'm gonna do exactly the same thing but for the white pieces there is a great way to do this if you hold that in the Alt key you can actually block a rectangle and then whatever you type in there gets duplicated across multiple lines so I universe anywhere by the way you can just make over because otherwise if you don't have the the Alt key you can see that it just selects the characters and basically in our rule so hold down the Alt key select everything up to highlight the entire warden of black and then type in WH ite and you see that we now have the same values but this time for the white pieces so our board renderer I'm gonna put this on a debug chess object board renderer and you see we have our black pawn route make bubble of upper bar and then we're just gonna copy these values across so I'm gonna choose blanks not really gonna show up against their board here because our board is actually black so I'm gonna choose red for our black piece II saw a red pawn or a red Knight there's our bishop as a rook and here is the queen and there is the King and I will do the same thing for the white pieces as well so we have our pawn we have our Knight our Bishop we have a rook and we have our queen and we have our King there we go and then we have a public void so we're gonna initialize our board with our initial pieces and all we're gonna do is we are going to I think I think what we want to do is we want to map these values to make it a little bit easier for our serializer soul and we've got it let's leave our public initialize just there but I also want to create a dictionary string and I want to link that to sprite so I'm gonna call this right you see the dictionaries got squiggly lines against that that's because it needs a generic collection against it okay so I want what I want to do is I want to map a small pea to this sprite here small r2 this rook and large capital P large capital P to pawn capital P capital R to rook white rook and so on and the way I do that is I'm going to void void await straights it was new sprites and then I'm just going to go through and cycle through all of these values so I'm gonna say sprites pawn equals black pawn sprite okay so it's that there and then I'm gonna do exactly the same thing again and I'm gonna copy and paste that in there and again I'm gonna do my alt trick where I highlight the the rectangle I'm going to choose white then I'm going to change these to capital letters so P R a B Q K okay so all I've done is I've done a map between my my pieces my piece types which is serialized of it so it got RP r RK that we set up and inside our chess serializer and I'm mapping that to the physical representation what its gonna look like on the screen and then for my initialization what I want to do is I want to create a new game object with a sprite renderer and then set the sprite renderer to be that particular type but really I should probably create a prefab so that's what I'm gonna do so I have let's create a 3d object really hope you like the sprite let's create a sprite object okay so our sprite object is going to be called chess piece okay we're gonna change this anyway it doesn't matter chess piece and it is a sprite renderer but it has no spray attached to it and we'll just look at position zero zero zero so it's basically gonna be at this point here but there's no sprite attached to it and what we're gonna do is we're gonna create a a game object that's gonna be cold and whatever the name of the type is we're gonna use that they are so going to see lots of peas and lots of small ours and all that kind stuff so that's our chess piece there and then for our board renderer you see we have all these values here we're also gonna have a game object piece template so this is gonna be a piece template so if we want to do something special with our piece then we can create we have this piece template that we're going to use which is our prefab so we're gonna drag our chess piece onto our piece template and this is what we're gonna create we're gonna create this access the sprite renderer and then use the sprite renderer to display our object so initialize all we need to do that is we just need to go through all the pcs in the board and add a new game object based on that particular piece okay and we're gonna take this up in the next episode because we really want to when we initialize it we want to delete all the objects that we created before in fact you know what we want to do we want to actually have we really want to create 8 plus 8 plus 8 plus 8 which is 32 game objects that we're going to reuse for each that we just reuse so let's do that instead so we have our piece template and then we have up here we're gonna have game object and we're going to call this pieces and pieces we're going to initialize to be new game object which is like I said going to be 32 because there's eight pawns for each side and then we have our back row of the the nobility so that's 8 plus 8 16 plus another 16 start to 2 so that gives us our 32 game objects so for each of these game objects we are then going to apply those values here so for now bring in mind that we can't have more than well I guess we can have more than 10 with 161 no we can't we can't have more than 32 pieces on a chess board if I'm wrong right me below but I'm pretty sure we can more than 32 pieces in a chess board and so I'm just going to do for I in I equals 0 I less than board dot pieces don't count I plus plus so for example if we if we decide that we're going to load in a a game from you know The Times crossword the the tangs puzzle page has a chess problem in it let's say we load that into memory then it only usually has like an end game so it's like you know three moves to checkmate there's only going to be a handful of pieces on there so this the this will fix things up what we maybe want to do is to do clean up pieces and move bored so we want to move them off the board at some point later but I'll leave that just now so the reason why I'm doing this is because we know there's only gonna ever be 32 pieces on a chess board so we don't need to allocate anymore we can actually just say we are always going to have 32 pieces so this means that our memory allocation is going to be fairly straightforward it's going to be 32 we're gonna have 32 objects the matter what and what game we're playing we're always gonna have 32 objects or less but we're sorry we may have less than 32 objects on the board but we're always gonna have 32 objects and memory which is not a lot okay so for each of these we're gonna see that our remember that just allocates a space for our game objects so this is gonna be null so we're gonna say if pieces I equals null then we're gonna create a new game object pieces I good new game object pieces oops I really cannot type pieces I thought name equals and then we're gonna call it whatever the name of the pieces so it's gonna board pieces thought I thought type okay and then actually you don't want that inside that if statement we're still on iris lady of still we're going to set the name of the object so this is us just saying that if they object at this position in Cedar re is null then we want to create a new game object in there because this is just allocating it it doesn't create a new it doesn't create 32 new game objects it just creates space for 32 game objects so in here we want to see if that slot is not filled we want to add an item to that object then we want to set the name for it and then what we want to do is you want to access the sprite renderer and we want to see this sprite is gonna be where are we or sprite and then it's gonna be our board actually let's cut back in this a little bit actually it's gonna be pcs in thought name isn't it because it's her type so our type remember is gonna be a pawn a rook or king or whatever is so we just do that there and then we're done for naming it so we're saying that our straight is the name of the sprite in our dictionary so this is our map between our P or R R K or BR q RK and then we just need to do our transform which is going to be a transformed or position which is also stored inside here as well so we have pieces i dot transform both position equals both dot pieces i dot position okay all right so just to go back through this again we have our awakes this is like a constructor in unity so we create our dictionary that map's our piece type to the actual physical representation so although it says black pawn black group black knight we can't actually specify that it's going to be porn or a red light or a red bishop and suddenly for the white pieces there as well and then we allocate an array of 32 to our pieces these are our instances so we know we're never gonna have more than 32 in in this particular game anyway and then we're going to create a public we also have our template as well so our template is actually well okay I made a bit of a mistake so it's not game object it's actually we want to instantiate this template here so instead a new game object we want to say instantiate and then it's the original object so we want to make a copy of our object cuz you may want to do other things with it like tidy this up because if you're nice to pass in just the piece and then let it figure things out so we'll let me do that in the next video okay so we have our piece there and we instantiate it and then we set the name of the piece we get the sprite renderer and then we assign it to her dictionary of sprite we use this the the name of the piece to access the value in there and then we set the position there okay so that's everything done there so the debug now we need to say avoid button Lord okay we haven't created this yet but we're going to create this so board will they gain equal serializer dot load game and then because debug is attached to the same object as board renderer I'm just gonna say get component board renderer initialize and then pass em loaded game okay and I'm gonna create a new button just by copying the old one and I'm gonna change the name of this to blood and I'm then gonna change the you see this has create button here so this is another reason for copying is is that I've set this part up already and all I need to do is just change it from debug button create to a debug button load game and I think that's everything done so I've created that there and there are debug chess has a board renderer which also has a chess piece all these pieces are set up so yeah let's let's play the game well let's load things in here so create is gonna create our game so that's gonna give us our default pieces and then load should load everything into memory and you'll see that hopefully fingers crossed when we run run this will see a whole slew of objects getting created here and we should see the values getting populated here so let's see if that works in three two it's very exciting is that what okay not quite we're missing a whole bunch of pieces here because it says the key was not found in the dictionary well that is not good okay well let's see why that is so we have type K take you night queen okay okay what we can do is we can see if so this is us debugging it there if sprites maintains key this is a top name else debug start log and find tight oh it looks like we might be yeah be unity okay it looks like we may be havin to do some debug in here okay ah you know why because I think I took the wrong values and oops so it's not lowercase key explorer takes n it's uppercase n for that that's why let's make sure I've got that right in here yeah have a good lowercase n or case now Oh silly mistakes well this is why pencils have erasers so let me reload this this is our chess pieces this is exactly what we wanted we have our chess pieces here so we have our queen there King the rook is all there everything totally serialized totally deserialized we now have everything looting from memory and the great thing about this is that when you take that away your scene is no longer cluttered it doesn't have all the junk in it you don't need to have 50 pieces in there it's all it's all coming from the data it's it's not quite but it's getting there it's almost data driven so yeah so I think we will we'll stop that there it's another successful way to to deserialize data such as deserializing multiple positions and multiple object types and Inter disk and then from disk so we have a serialization deserialization make sure you get the the chest types rate remember it's n for Knight not K for my case King yeah rookie mistake if you liked this video please give a thumbs up if you didn't like the video then thumbs down but let me know in the comments below what's the room for improvement less stuttering in less oming I guess and I just did it there can't help myself now thank you very much for your time I know time is important so until next name thanks for watching bye
Info
Channel: Sloan Kelly
Views: 981
Rating: undefined out of 5
Keywords: unity, c#, c-sharp, coding, programming, game dev, gamedev, game development, chess, serialization, deserialization, save game, made with unity, madewithunity
Id: PYj1IAz_rhY
Channel Id: undefined
Length: 29min 43sec (1783 seconds)
Published: Wed Oct 26 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.