Make A Game Like Pokemon in Unity | #66 - Saving Items

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to part 66 of my pokemon game series in unity so in this video we look at how to save the items in our inventory when we save the game so if i go take this item then i'll have two max portions in my inventory so now if i save the game and restart the project and load okay so once i load you can see that we still have two max portions in our inventory okay and also the pickup that we took is no longer here so let's look at how to implement all this special thanks to all my patreons for making this series possible by becoming a patreon you can support me and get access to the complete project files of the series the project files also contain some advanced features that are not covered on youtube so let's start the video so first let's fix an issue that we currently have so let me show you the issue so if we use a tm or hm to teach a move to a pokemon that already has four moves then we'll get this move selection ui but we won't be able to select any move from the small selection ui okay so this is because of a change that we made to the dialog system in the previous video so if we open the dialog manager script in the previous video we added the on show dialog and on closed dialog event to the show dialog text function right so this is what changes the state of the game so what's happening here is in our case when we show the most selection ui we are not closing the dialog right the dialog is still open when we show the most selection ui so what happens is the on show dialog even will be called which will change the state to dialog but once the dialog finishes the on close dialog event won't be invoked right since we are not closing the dialog in this case the close dialog function will not be called and this event will not be invoked so if it's not invoked then we won't change the state back to the previous date and the state will still be in dialogue so that is the reason why we are not able to select a move from the move selection ui so we can easily fix this by moving this event out of this function and just putting it over here at the end okay so now we will change the state back to the previous state even if the dialog is not closed right but this feels a bit weird we are not closing the dialogue but we are still calling on close dialogue event so what i'll do is i'll just rename this event to something like on dialog finished so that makes more sense right so now even if the dialog is not closed beyond dialog finished event will be invoked and it will change the state back to the previous state okay so now we should be able to select the move from the most selection ui so let's go ahead and test this okay all right so here you can see we are able to select a move from the no selection ui okay so that issue is fixed so now let's start saving our items and pickups in order so let's start by saving the state of the pickup so right now when we save the game we are not saving if the pickup is used or not okay so let me show you and by the way to save the game we'll have to run the gameplay scene we can't just save the game if we are testing an individual scene so make sure to run the gameplay scene if you want to test saving so as i said right now we are not saving the state of the pickup so if i go take the pickup okay and if i save the game and now if i restart the game and load you can see that the pickup is still there even though we took it before saving the game okay so while saving we have to save the state of the pickup so it's going to be easy since we already have a flexible saving system so let's go ahead and use it on the pickup so let me go back to the hometown scene and here let me first change the pickup to a prefab so i'll drag and drop it here to make it a prefab and i'll go inside the prefab and we have to mark the pickups as a savable item right so from inside the prefab i'll add the saveable entity script okay so this will mark this object as a savable object all right so if you go back the pickup that's in the scene should have a unique id generated which will be used for saving all right so next let's go to the pickup script and here we need to implement the i sayable interface all right and when we implement this interface we also have to implement the capture state and restore state functions so let me just implement both functions all right now let me just place it together okay so now in these functions we need to specify what all states must be saved and restored right so in our case the pickup only has a single state that's this boolean that indicates whether the pickup is used or not right so we only have to save that so in the capture state i'll simply return the value of the used boolean okay and then i'll restore it from the restore state function by converting the state parameter to boolean and i'll store it back to the use property all right so by the way if you don't remember how the saving system works you can go ahead and watch the video on saving system once again okay so here after restoring the state i'll check if the value of used is true so if it's true that means this pickup is already used right so in that case we can simply disable it by disabling its sprite and its collider okay so if it's used then it won't be shown in the scene all right so let's go ahead and test if this is working correctly so we have to make sure to run the gameplay scene okay and let me go take the pickup and then save the game all right and now i'll restart the game and load it all right so now when we load you can see that we don't have the pickup anymore okay so it's getting saved correctly so next we need to do the same for the item giver so we need to save whether the npc has given us an item or not right so once the npc has given us an item we should save that state by saving the game okay so let's go ahead and do that so first i'll open the npc prefab and in here i'll add the disabled entity script to mark this as a savable item and then i'll open the item give a script and here we just need to save the used boolean right so let me do the same thing that we did for pickups i'll just implement the isabel interface and then implement the capture state and restore state functions okay so in capture state we just have to return the value of used boolean and we just have to restore it in the restore state function okay so that's all we have to do in this case we don't have to disable anything so let's go to unity and test if this is saving correctly all right so let me go speak to the npc so he gave me a few pokeballs so now if i save the game and then restart it and load and now if i speak to the npc you can see that he doesn't give me the item anymore right so the state of the item giver is being saved correctly all right so next we need to save the items in our inventory when we save the game right so this is going to be pretty similar to how we saved our party so let's go ahead and implement that so the inventory is inside the player right so let me open the player prefab and in here we have the inventory script attached so the player is already marked as a stable object because it already has a stable entity script attached to it so we attached it for saving the party of the player so we don't have to attach it again for saving the inventory okay so let's go to the inventory script and write the code to save the items in the inventory so first i'll make the inventory implement the i savable interface all right and then i'll implement both the capture state and resource date functions so they'll be created over here so now what all should we save in the inventory so here in the inventory we have three list of item slots right so we have to save these three lists but should we save the entire item slot class so if you look at the item slot class it contains the item base and count right and if you go inside item base it has lots of data but we don't have to save all these data right we just have to save the name of the item and since the name is unique for each item we can get all the other details of the item from the name right so this is the same approach that we used for saving pokemons and moose we did not save the entire class instead we only saved the name of the object right so while saving the inventory we don't have to save the entire item slot class we just have to save the name of the item and its count okay so let's create a class for that called item save data okay so this class will have a variable for the name of the item and then it'll have a variable for the counter right these are the two things that we need to save and by the way i'm just making these variables public because this class will only be used for saving the data it doesn't have any other purpose right so i'm just making it public to simplify things so next we need to make this class serializable so that we can serialize and save it using our saving system all right so now we have the item save data class so next we need functions to convert the item slot to item save data and the opposite right so first i'll create a function to convert the item slot to item save data so i'll create a function get save data and it'll return item save data so in this function first we need to create an instance of the item save data class all right and while creating the instance we need to assign the item name and count so let me assign the name first so we can get the name from item dot name and we can directly get the count from this variable okay so now we can simply return the instance of save data all right so i just have a typo over here let me change it to save data so next we need a function to restore the item slot from the item save data right so we can actually use a constructor for that so i'll create a constructor for the item slot and it'll take item save data as the parameter all right and inside the constructor we just need to restore the values of these two variables right so restoring the item is a bit tricky since we only have the name of the item in the save data so we can't simply restore it like this right so we need a way to get a reference of the item from its name so for that what we can do is we can create a dictionary that contains all the items in our game and the key of the dictionary should be the name of the item okay so we have used the same approach to restore a move and pokemon from its name so let's look at how we implemented this for the move so that we can get some idea so over here you can see we have a constructor to create a move from the move save data and here to get the move from its name we have this class car db all right and if you look inside move db here we have a dictionary of all the moves in the game and we can pass the name of the move and get the reference to that move right so if you don't remember how this was implemented you can watch the video where we saved the party of the player so that's where we implemented all this so i'll put a link to that video in the cards all right so what we have to do is just like we did for the mo we have to create a db for the item right so here db stands for database so we have to create a database for the item just like this so let's go to unity and in scripts inside data i'll create a script called itemdb okay so this class doesn't have to be a monobehaviour and let me get rid of all the default code okay so i'm not going to implement the item db from scratch because it's pretty much the same as the implementation of modb and pokemon db right so i'll just copy the implementation of move db and make some changes to it okay so let me just close all the scripts that we don't need and let me open the move db script all right so i'll copy the implementation of movdp and paste it inside the itemdb so here we need to make lots of changes right so here instead of move base it should be item based and this should be called moves and in here we should say items instead of moves so we have lots of changes to make but let me show you a trick that you can use in these cases so i'll hit ctrl h and it'll open up this small window which we can use to replace certain text in our script okay so let's say we want to replace all the move with item so what we can do is in the search term which is the first field we can type move so here you can see it's already highlighting all the areas where we have written move and in the replace text we can write item okay so before you replace make sure that you have match case turned on so if match case is turned on then it'll only search for move with a capital m right if we turn it off then it'll also search for mo with small m so in our case we only want to replace mu with the capital m so make sure the match case is turned on and then you can click this button over here to replace all the instances okay so now all the instances of mo with a capital m is replaced with item with capital i so next we need to do the same for move with small m so i'll change the search term to move with small m and i want to replace this with item with small i right so i'll hit replace all again and that should replace all the instances all right so this is a pretty handy tool when you want to copy paste and modify the code it's much better than modifying all of it manually right but be careful it's also pretty easy to make mistakes while using the store so now we have the itemdb script so we can use this function to get the options to the item from its name all right so here i'll call item db dot get item by name and i'll pass the name as the parameter okay so in this way we can easily restore the item all right so let me also restore the count from the save data so we can directly do that all right so now we have functions to get the save data and restore it so let's use these to save our item so we have to save three list of items right so here in the capture state we can't directly return the three list we'll have to store those lists in some class right so here i'll create a new class called inventory save data all right and this class will contain three list of item save data so the first list will be for the recovery items so i'll just call it items and the second list will be for pokeballs and the third list will be for dms okay and let me just add the serializable attribute over here so that we can save this class so now from the capture state function we can create an instance of the inventory save data class all right and we can assign the three list so first i'll assign the items list so the items are stored in the first list called slots right so sloth is a list of item slot and we need to convert it into item save data right so for that we can call the get save data function for all the items in the item slot okay so here for all the items i'll call get save data function all right and we need to convert this into a list so here we are just converting a list of item slot to a list of item save data all right so let's do this for the other two list so i'll assign it to the pokeballs and here i'll use ball slots all right and finally let me just do the same for the teams okay so we have created an instance of inventory save data so we just have to return it from the capture state function okay so next in the restore state function we have to do the opposite of what we did in the capture state function right so first we can get the save data by converting the state into inventory save data and then we can restore all our slots okay so first i'll restore the recovery item slots so i can get the recovery items from save data or items but we need to convert this from list of item save data to list of item slots right so we can do that by using this constructor of the item slot okay so i'll say items dot select and i'll pass all the items to the item slot constructor to convert it into an item slot okay so let me just use the tool list function to convert this into a list so we have restored the recovery item slots so next let's do the same for the pokeballs so i'll change this to pokeball slots and here i'll use save data dot pokeballs okay and next let's do the same for the dms so let me change both of these to team okay so this will restore the items for us and once we restore we have to invoke the on updated event right once we restore there will be changes in the items and we have to update that changes in the ui right so we have to invoke the on updated event okay so we're done with the code for saving items in the inventory but we have an error over here so let me look at the error okay so here i was using the default constructor of the item slot but now since we have defined a constructor with a parameter we have to make sure to create a default constructor that doesn't have any parameters okay so now the error is gone by the way there is one thing that i missed when we create the item db we have to make sure to call the init function when we initialize the game okay so if you go to the game controller in the awake function you can see that we are initializing all our dbs so let's also initialize our itemdb from here all right so we're done with the implementation for saving the inventory so if you test this now saving the items will work but the restoring won't work okay but we are restoring everything correctly so it should work right so to explain why it won't work let's look at how we are getting these slots based on its category okay so this is a function that we are using to get the slots of a category so here you can see we have another list called all slots and in this list we are storing all our three slots okay so the issue is when we change the value of slots list like this the change won't actually be reflected in the all slots list okay so this list will still have the old slots so to fix this we can simply reassign the alt slots list after we restore all the slots okay but now you might be wondering we had made other changes to the slots right so here we are removing a slot from the slots list and here we are adding a new slots so in these cases we did not re-initialize the all slots list right so the reason for that is because when we make changes like adding a new element to the list or removing it that change will be reflected in the all slots list okay so this is a bit tricky concept to understand so here if i were to make a change like adding a new element to the slots list then that change will be reflected in the all slots list we don't have to reassign it but in this case we are not just making a simple change we are completely reassigning the value of this list right we are just assigning a different list to this variable so in that case the changes won't be reflected in the all slots list so if that sounds confusing to you you should read more about value types and reference types in c sharp i'll put a link in the comments where you can learn more about it okay for now just understand that if we reassign the slots like this then we'll also have to reassign it in the all slots list okay so let me just get rid of this and let's go to unity and test again so let me go pick up an item okay so currently we have two max portions in our inventory and we have five pokeballs right so if i go speak to this npc he'll give me another five pokeballs and now we should have ten okay so now if we save the game these changes should be saved right so let me just restart the game and let me load the last save okay so now if i check the back you can see that we have two max portions and 10 pokeballs so the item that was added to our inventory was saved successfully so i'll stop the video here if you think these videos are helpful please leave a like and consider subscribing to my channel that'll really help me out so i'll see you in the next video
Info
Channel: Game Dev Experiments
Views: 835
Rating: undefined out of 5
Keywords: make a game like pokemon in unity, how to make a game like pokemon in unity, make pokemon in unity, make pokemon game in unity, how to make pokemon game in unity, make a pokemon game in unity, unity pokemon game tutorial
Id: w3ZQ9iCJYEA
Channel Id: undefined
Length: 36min 0sec (2160 seconds)
Published: Sun Sep 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.