Make A Game Like Pokemon in Unity | #55 - Using Items & Bug Fixes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This is part 55 of my Pokemon Game tutorial series in unity. In this video, we'll write the code for using items. So from the inventory, the player will be able to select an item and use it on a pokemon.

If you're new to it, in this series, we'll look at how to make a Turn-Based RPG like Pokemon in Unity. It covers common RPG mechanics like Turn-Based Battle, Experience/Level Up, Party System, Status Effects, NPC/Dialogues, Saving Loading. These are implemented in a modular way, so you will be able to use it in other projects too etc.

Here is the entire playlist of the series.

👍︎︎ 2 👤︎︎ u/GameDevExperiments 📅︎︎ Jul 04 2021 🗫︎ replies
Captions
hey everyone welcome to part 55 of my pokemon game series in unity so in this video we'll write the code for using the items so if i go to my bag and if i use a portion on this charmander you can see that it's hp has been restored and we get this dialogue over here so if i press it to continue it'll go back to the item selection screen and here you can see that the number of portion has been reduced it was five before but now it's four since we used one and also if we try to use the portion on a pokemon that already has full hp then you won't be able to use it so yeah we'll implement all this in this video special thanks to all my patreons for making the 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 some issues that we currently have so in the previous video we cleaned up our party screen a bit so that we don't have to call set party data every time we open the party screen instead it will be automatically called when any change is made to the player's party so this is done by using this on updated event so we are invoking this event whenever a new pokemon is added to the party okay but there is one more place where the players party is updated it will be updated when we load the game right so if we go inside the player controller when we load the game we are setting the pokemons of the player party alright so when we do this we also have to invoke the on updated event so what i'll do is i'll invoke this inside the setter of the pokemon property so inside the setter i'll car on updated dot invoke so now whenever we set the pokemon's property the party stream will be automatically updated so next let's look at another issue since you're not setting the party data every time we open the party stream we have to make sure that the changes in hp of the pokemon and all is reflected in the party stream so first let me show you the issue so if i run the game so let me start a battle and try to take some damage all right so now i've taken some damage but if i open the party screen you can see that it's not reflected here so if we are not setting the data every time we open the party stream then we have to make sure to handle cases like this so we can handle this by using observer pattern so let's implement that so i'll go to my pokemon class and here similar to the on status change given i'll also create another even car on hp changed so using an observer pattern will make things easy in the long run for example when you use an item like portion it'll automatically change the hp in the party stream we don't have to manually change the ui every time we change things so now we have to invoke this event whenever the hp is changed so right now the hp is only changed from one function that is update hp so yeah it should be slowly changed from here so here we'll also invoke the on hp changed function all right so here we also have an hp changed boolean variable so this is used to update the hp in the heart but we'll remove this soon and we'll also use uh this event in the hud also okay so now we are firing this event so next we have to subscribe to it from the party so we should actually subscribe to it from the party member ui so party screen is made up of six party member ui slots and that's where we have to subscribe to the hp changed event so we can subscribe to it from here but first i'm going to clean this up a little bit so i'll copy these three lines and i'll paste it in another function car update data okay so update data will be called from here so here we have to use underscore pokemon since pokemon is just a parameter so let me do that all right so now i'll rename this function to something like init because if the function name is set data and then if you're doing things like subscribing to an event that doesn't make sense right so i'll call this init instead and from here i'll subscribe to the on hp changed event and when this event is fired we just have to set the hp but i'll just simply call update data function since that'll set php for us so yeah it doesn't matter if you want you can just use a lambda and set just the hp so now whenever we change the hp it should also be reflected in the party string ui so let's go ahead and test this now okay let me start the battle real quick let me try to take a damage okay so now if you go to the party screen you can see that the change is reflected here so that is working fine so with these issues fixed let's start implementing using items so if i select an item and if i select a pokemon from the party stream we should use that item on the pokemon all right so to implement item using i'll go to my item base script so this is where i'll create the use function for using the item so here i'll create a function called use so this function is going to return a boolean and this will indicate whether the item is used or not sometimes you won't be able to use the item for example if the pokemon's hp is full then we won't be able to use the portion so in such cases we'll return false so the use function will not have any implementation inside the item base class but it will be implemented in the subclasses right so we'll make this function virtual so that they can be implemented from the sub classes and in here i'll just return false so that we won't have any error so we won't call item based or use directly we'll be calling it from the subclasses so it doesn't matter what the implementation is over here so now let's open up our recovery item script so in here we will override the use function so if you type overwrite and hit space it will show you the functions that you can all write so basically we can override our functions that are virtual in our base class so that's why you can see use over here so i'll just override the use and in here we'll write all the logic for using a recovery item so let's start by implementing a simple item like portion and let's make sure that works and after that we'll implement the rest of the items so for portion there will be an hp amount so i'll check if the hp amount is greater than zero if so we can use the portion and restore the pokemon's hp and by the way there is one thing i missed the youth function should take pokemon as a parameter and that will be the pokemon on which the item should be used okay so let's add pokemon as a parameter so first i'll add it to the item based script and then i'll add here also okay so now we can restore the hp of the pokemon by hp amount but before i do that i'll check if the pokemon's current hp equal to its max hp okay so if this is the case that means pokemon's hp is already full and we don't want to use the portion on that pokemon so here i'll just return false and we won't use the item otherwise if there is some hp that we can restore then we can try to do that so to update the hp right now the only function we have is update hp so if you look inside this function this actually takes a damage and it reduces it from the hp but in that case we want to add a value to the hp right so an easy way of doing it would be to pass the hp amount as a negative value but that might be confusing to read later so what i'll do is i'll actually rename this function to something like decrease hp okay that makes more sense for this function and i'll make a copy of this function and create another one car increase hp so in the case of this function i'll rename the parameter to something like amount and we have to add the amount to the hp right so now we can call this function to increase the hp so let's do it from here and restore the hp of the pokemon by hp mount all right so using this we should be able to create items like portions and super portions and all so we have an error here that's because we are not returning anything from this function so let's just return true from here all right so now we have a function to use the item so next we should actually call this when the player selects an item and a pokemon from the inventory ui so let me open up inventory ui and here when we select the pokemon to use the item on right now we are not doing anything so from here we have to call item.use right so i'm not gonna call item.use directly instead i am going to create another function inside of our inventory so this function will be called use item so this will take the selected item and selected pokemon as the parameters so for the selected item we only have the index of selected item so here i'll take an integer for the index of selected item okay and then i'll take another parameter for the selected pokemon so now first we have to get the item from its index so i'll say slots of item index why did i name it index item it should be item index all right so this will give us the item slot at this index and then we can just say dot item to get the actual item and i'll just store it in available car item so next i can just call item dot use and to this function we have to pass the selected pokemon okay so remember this function actually returns a boolean so let's store that in a boolean variable so now we are using the item so next if the item is used then we should remove or decrease its count from the item list so i'll check if the item is used and if it's used we should remove that item so for that i'll create another function called remove item and this will take the item that we should remove so to remove the item actually we won't be removing the entire slot of the item instead we'll be just decreasing its count right for example if there are five portions then we'll just decrease its count and make it four so first we need a reference to the slot of the item so we can get that by using slots dot first so to use first you have to import the link namespace so we have to get the first slot in which the item is equal to the item that we want to remove okay so i'll just draw this slot in a variable all right and we have to reduce the count of the item in the slot all right so we can get the count by item slot dot count and i'll just decrement it by one okay so i'm getting another we can't decrement this so when we write the property like this it'll only have a getter and we need a setter to actually change its value so i am going to define the property like this and i am going to define both the getter and the setter all right so now the error should be gone so we are reducing the count of the item and once we reduce if the count becomes zero then we can remove that slot entirely right so i'll check if item slot dot count equal to zero and if that's the case i'll just remove the slot from our slots list so i'll say slot start remove and i'll pass our item slot okay so this function will remove the item for us so let's call it from here and let me just pass our item so now we have the function to use the item but there's one last thing i want to do i want this function to return the item that is used so this will return a item base instance and if the item is used then we can simply return the item and otherwise we'll just return null indicating that the item is not used so returning the item used will allow us to show dialogues like the player use portion so that's why we are returning this so now let's call this function from the inventory ui let me bring it over here all right and in the on selected i will call inventory dot use item so for the item index we can just pass the selected item variable all right and for the selected pokemon how can we get that so if you look inside our party stream oops all right here you can see we have a property called selected member so this will return the member that's currently selected in the party stream so let's go ahead and use that for the selected pokemon so i'll say party stream dot selected member so now let's go ahead and test this and hopefully it should work okay so right now all my pokemon has full hp so i won't be able to use a portion so first let me reduce the hp by starting a battle okay so yeah i produce my hp a bit so now if we look at our party stream the charmander has lost some hp and we can try and use our potion to restore that so before i restore let me go ahead and save the game so this will allow me to test the portion again and again and i won't have to start a battle and take damage every time for testing the portion so now let me try to use the portion so i'll select portion from here and i'll select this charmander okay so now you can see it has restored the hp of the charmander so if you go back okay this is not updated it was five before so after using the portion it should be four so there is some issue in the code let me check so the issue is we are changing it from the slots list so we are changing it from here but we are not actually updating that in the inventory ui right so here we have a function called update list so we can use that to update the list in the ui so we can call that function from the remove item function but we don't want to create a dependency to the ui class so what i'll do is i'll use the observer pattern just like we did before so here i'll create an event call on updated okay and when we remove the item we can invoke the on updated event so next we have to subscribe to this event from the inventory ui so in start i'll subscribe to inventory dot on updated event and when that event is fired i'll call the update item list function okay so that should update the item list for us so let's go ahead and test this so i'll just load my last save so that some hp will be reduced from my charmander so now let's go ahead and use the portion on the charmander okay so it restored the hp and if we go back you can see that the count of the item has been reduced to 4. all right and by the way if we try to use the potion on a pokemon that has full hp then we won't be able to so i press set but you can see that the item is not used and the count is still full so next we should show a dialogue when the player uses an item so if the player uses a portion then we should show a dialogue like the player use the portion so let's go ahead and implement that so from our inventory ui once we use the item we should also show a dialogue so let's take a look at our dialog manager script so in here we have a function called show dialog but you can see that it takes a dialog object as the input so this function is used to show multiple lines of dialogues and after showing each line if we press it then it'll show the next line so in our case we just want to show a single line of dialog so we don't want to create this dialog object and all that we just want to pass a text and show that so let's create another simple function for this so this function i'll call it show dialog text and this will also be corrupting okay so this function will take a string instead of taking the entire dialog object so the implementation of this function is going to be pretty similar to this one so first i'll set is showing to true so that other components can check if the dialog is being shown or not so we don't need any of these lines since it's just a single line of dialogue we don't have to cache anything we can directly make the dialog box active and call the type dialog co routine to actually type the dialog so i'm going to make the dialog box active first and then i'll call the type dialog core routine all right so to this i will pass the text input and i'll use shield return here since we want to wait until the dialogue is complete okay so next once dialogue is complete we can go ahead and make the dialog box inactive and after that i'll just set it's showing to false all right so this will show the dialog and after showing it it'll go ahead and hide the dialog box so there's one more thing i want to add to this i don't want to hide the dialog box as soon as the dialogue is complete instead i want to wait for the player to press the z key and only after that i'll close the dialog box okay so this will give the player enough time to read the dialogue so we can do that by using another co routine but since we might not need this in all cases i'm going to add a boolean parameter called wait for input and i'll set it to true by default so if this is true then we have to wait for the player's input before closing the dialog so here i'll check if wait for input is true and in that case we should wait for the player to press the z key so i can do that by using the weight until function so to this function i'll pass a lambda and this lambda will return the value of input dot get key down and it will check for the z key all right and since we want to wait until this happens we have to use field return all right so this line of code will make the function wait until the player processes that key all right so next let's call this function to show the item used message so from the inventory ui after the item is used i can call the show dialog text corrupting that we created but what i'll do is instead of calling the code routine from here i'll put all the logic to use the item in a separate corrupting so that we can wait until the show dialogue routine is complete so i'm just going to cut this and here i'll create a new co routine called use item and let me paste the code to use the item here so once we use the item this function will actually return the used item so let me store that in the variable okay and if the used item is not equal to null then it means an item was used so i'll call dialog manager dot show dialog text function and we can simply show a text like the player used this item so i'll get the name of the item from used item dot name so one thing you can do is if you want you can add a custom use message for each item so along with name and description you can also add another field called use message or something like that so for the portion the use message will be something like restore the hp and for ether it will be like restore the pp so yeah you can use a separate field for that but i'm just going to keep this simple and i'll just simply show a message like the player use the item so next in the else of this condition if it comes to else it means used item is another and the item was not used so in that case we can simply show a dialogue like it won't have any effect or something like that and by the way since we want to wait until this function is complete we have to use a yield return okay let me also put it here so now we'll show the dialogue after the item is used so after that once the dialog box is closed we can also go back from the party stream to the item selection screen so we already have the close party screen function for doing that so let me call that okay so there is one last thing that i want to do at the start of the score routine i want to set the state to inventory ui state dot pc so this will just make sure that the player won't be able to spam the z key and use the item multiple times all right so let's go ahead and call this function from the on selected action so i'll use the start core routine here and i'll call the use item coding okay so let's go to unity and test this so one thing you have to do before you test is you have to make sure that the dialog box is below the inventory ui so if i make the dialog box active right now you can see that it's not visible that is because the inventory ui is on top of it so if we bring this down now you can see that the dialog box is on top of the inventory ui so we'll keep this here and let's just disable this by default and let's test the game now so again i'll load my last save so that i can test the portion on charmander so this time let's just try using the super portion so if i use it on charmander you can see that this message will be shown and if i press z i'll go back to the item selection state so let's also test the other case so if i try to use it on a pokemon with full hp then i'll get this message and i won't be able to use the portion so here you can see the count is still one so awesome now we have code for using items so next we just have to write the implementation of all the other types of items so that's going to be pretty easy and i'll be doing that in the next video so thanks a lot for watching guys if you found this video 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: 1,272
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, unity item system, unity using item, unity inventory tutorial
Id: f735bgRCVA0
Channel Id: undefined
Length: 33min 56sec (2036 seconds)
Published: Sun Jul 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.