Make A Game Like Pokemon in Unity | #62 - TM & HM Items 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to part 62 of my pokemon game series annuity so this is the second part of teams and actions so in this video we'll implement etchings so hm items are reusable and we can use it multiple times and it won't be removed from the inventory all right so if i select a tm or hm item we'll show whether a pokemon can learn that move or not so here you can see both charmander and bulbasaur is able to learn cut but pidgey is not able to learn cut so pokemon will only be able to learn specific moves by using tm and hm items so 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 in pokemon games we can't use tm and hm items when we are in a battle right we can only use it outside the battle so let's look at how we can block the player from using teams when they are in a battle all right so we have to handle a similar case for pokeballs so for pokeballs we check the game state from the use function so the pokeball will only be allowed to use if the game state is battle but we can't do the same approach in dm the reason is because the code to use dm item is executed before we call this function right so if you look at the inventory ui in the use item function the code to use tm items will be executed when we call this function so we'll have to add another if condition here and only execute this function if we are outside the battle but i'm not going to implement it like that instead i'm going to implement it in such a way that we can easily mark what all items can be used inside the battle and outside the battle okay if we do like this it'll be much easier when we add new types of items in the future so to implement this let me open my item base script so in here we have a virtual function called used that we override from the subclasses right so similar to virtual functions we can also create virtual properties that we can override from the subclasses okay so here i'll create a virtual property and this will be a boolean called can be used in battle and by default i'll return true for it okay let me actually change its name to can use in battle so that it's a bit more shorter all right so next i'll duplicate this and create another property called can use outside battle okay so by default the value of both these properties are true but if we want its value to be false for any item then we can override it and set it to false okay so in the case of dm item we should not be able to use it in a battle right so i'll write the can use in battle property and i'll just return false okay and we don't have to override the can use outside battle property because we want it to be true for tm items right so since its value is true by default we don't have to overwrite it so next let's go to the pokeball item and in here we should not be able to use the pokeball item outside the battle right so i'll override can use outside battle property and return faults for it okay so in the case of recovery item we don't have to override anything because we want both of these values to be true right we can use recovery items inside the battle and outside the battle so no need to override these properties so let me just close this so next we need to check the value of these properties before we use an item so in the inventory ui when an item is selected this function will be called so in this function we should write those checks so first i'll check if we are currently in a battle so for that i'll say if game controller dot instance dot state is equal to game state dot battle that means we are in a battle and otherwise we are outside the battle right so next we need to check these properties of the item so for that first we need to get a reference to the item so we have the index in the selected item right so i'll call inventory dot get item and pass the item index and also the category index okay and let me store this in a variable called item so now if we are in a battle and if item dot can use in battle is false then we should not allow to use the item right so here we can show a dialogue like you can't use this item in battle so let me just copy the line to show a dialog and for dialogue i'll say this item cannot be used in battle okay and by the way if we want to use shield return then we should make this function according right so let me do that and now when we call this function we have to put it inside start corroding okay so let me go back to this function and after showing the dialog we can just use shield break to stop this core routine and we don't want to execute any of this code right so next there is one more thing that we need to do so since this is a core team and this will last for more than one frame we have to make sure that nothing else will happen when the score routine is running okay so for that we can simply set the state to pc and this will block everything else when this core routine is running okay and before we exit the core routine we can set the state back to item selection so that user can select another item all right so next when we are outside the battle we need to check for can use outside battle property right so i'm just going to copy this code and i'll change can use in battle to can use outside battle and for the dialogue i'll say this item cannot be used outside battle okay so that's all we have to do so now when we add a new type of item all we have to do is set the value of these properties like this and you don't have to change any code inside the inventory ui okay so now for the pokeball item we don't have to check for this condition we just have to overwrite this variable right so i'll remove all this code and just return true from the use function all right so let's go to unity and test if everything works so if i try to use a pokeball outside the battle you can see that we'll get this dialog so that's working so next let me try using themes from battle okay so you can see it cannot be used in battle but if we try to use it outside the battle it works as usual okay so that is working fine so next let's implement etchants so let me go to my dm item script and in here i'll create a new serialized variable called shm okay and let me also create a property to expose it all right so the main difference with hm items is that we'll be able to use the hm item multiple times right so it's reusable and we should not remove it from the inventory when we use it so what i'll do is in the item base i'll create another virtual property called is reusable okay and by default i'll return false for it okay i'm returning false because most of the items are not reusable only hms and key items are usable so i'll return false by default and then we can override it from the tm item so here i'll overwrite its reusable property and for this property we should return true if the item is an action and otherwise we should return false right so what we can do is we can simply return the value of this variable so if this is true then the property will be true and otherwise it will be false okay so now all we have to do is if the item is a reusable item then while using it we should not remove it right so from here i'll check if the item is not reusable item and we should only remove it if it's not a reusable item right so that's all we have to do so let's go to unity and create an hm item and test so inside resources first i'll create a move for the hm so let me just duplicate scratch and rename it to cut okay i'm not going to change any values right now but you can go ahead and fill in the correct values so now let me go to the team and etchings and create a new hm item okay i'll name this hm01 all right so this hm will be used to teach cut right and we should set shim boolean to true okay so let me give it an icon and description all right so let me add the section to the player's inventory okay i'll change the size of this to three and the third item will be hm all right so let's go ahead and test this okay so we have our hm over here so let me try using it on my pokemon okay so charmander learned cut but you can see that the hm is still in our inventory right it was not removed after we used it so now we can use the sachim on another pokemon if we want so yeah we can reuse it as many times as we want so next while showing the teams and hms in this list we should also show the name of the move right it's really hard to understand which move this dm or hm will teach if the mo name is not shown over here so to do that the hard way to do that would be to add the move name in the name of each item so i can add it like this but that's not a good way right it's redundant and it's something that can be automated so let's use the power of inheritance to automate this so what i'll do is in the item base i'll make my name property virtual so that we can override it from the subclasses so now let me go to the tm item class and i'll write the name property from here okay so here along with the name we can also add the name of the moon right so i'll just add it like this okay and by the way if you are not familiar with inheritance in c sharp base dot name will give you the value of the name property in the parent all right so whenever you want to get the value in the parent you can use the base keyboard okay so let's go ahead and test this all right so yeah here you can see the name of the move is also shown in the list and by the way you can do the same for the description if descriptions of all your dm management items are similar then you don't have to write it manually for each item instead you can just generate it like we did right now okay so next we need to handle few cases while using tm and hm items so right now by using a tm or hm item we can teach a move to a pokemon even if the pokemon already has that move okay so let me show you all right let me open up the teams and let's say i want to teach cut to my bulbasaur all right so my bubbles are learned cut but now i can use this team again and the bulbasaur will turn it once more okay so now in the moose bulldozer will have two cut moves so we need to block this if the pokemon already has a move then we should not teach it again so in the inventory ui script before we teach a move we should check if the pokemon already has that move okay so i'll check if the pokemon has this move by using the has move function all right so we can get the move from dm item dot move so if the pokemon already has this move then we can show a dialogue saying the pokemon already know this move okay and then we can just use heel break to exit out of the score routine so yeah let's try testing this all right so let me use cut on my bulbasaur so it learned cut but if i try to use it again it will show this message saying bulbasaur already know cut and we won't be able to teach it again all right so that works properly so next we need to handle another case so right now we can teach any move to any of our pokemon by using tm or hm right but a pokemon should only be able to learn a specific list of moves so for example we shouldn't be able to teach a move like thunder wave to charmander right so let's handle that next so the way pokemon games handle this is by keeping a list of moves that can be learned by tm for each pokemon okay so if you look at the details of a pokemon in a site like pokemon db here you can see each pokemon will have a list of moves that can be learned by using teams and engines okay so let's also add such a list in our pokemon class actually we should add it to the pokemon base class right so in our pokemon base right now we have a list called learnable moves but this is the list of moves that the pokemon can learn by level up right so here we need to create another list for moves that can be learned by using dms and hms okay so this will be a list of no base and i'll call this learnable by items okay so let me also create a property to expose it so i'll create a property over here so now before teaching a move using tm or hm we should also check if the move is in this list okay so from here before teaching a move we can check if pokemon dot base dot learnable by items dot contains this move okay so i think this is a bit long let's clean this up by moving this into a function so inside tm item class i'll create a new public function called can be dot okay and this function will take the pokemon to which we are trying to teach the move all right and from here we just have to check if the move is contained in this list okay so let's just return the value of this and by the way we don't have to use tm item over here we can use the move directly so now from the inventory ui we can simply call dm item dot can be dot function okay and if this returns false that means the pokemon can't be taught this move then we can show a dialogue saying the pokemon can't learn this okay and i'll just use yield break to stop coding alright so let's go to unity and test this so before we test we should assign values to new list okay so here we have a new list and let's say charmander can learn most like cut and flamethrower and by the way you can just multi select all pokemons and and assign the common tmos that can be taught to all pokemons okay so let's say all pokemons can learn quick attack and tackle so you can easily assign it like this you don't have to assign it one by one so i'll just select my bulbasaur and add few more moves to it so that we can test properly so let's say bulbasaur can turn cut and it can also learn fine whip okay so let's try testing now all right so if i select a move like sing and try to teach it to my charmander will say charmander can't learn sing and we won't be able to teach them all okay and by the way there is an issue after this dialogue we are showing it won't have any effect so let's fix that first all right so in the use item function this is where we are showing that message but we only want to show it if we are trying to use a recovery item right so i'll just add an if condition and that should solve the issue so let's go ahead and test okay so if i try to teach singh to my charmander it won't be able to learn it and now we don't have the other message okay so if i try to teach a move that the charmander can learn then it'll learn that more okay so this is good but it would be better if we show in the ui whether the pokemon can learn the move or not right otherwise the player will have to try and use the team on all the pokemons to see if the pokemon can learn that move so let's show that in the ui okay so i'll go to my game folder and find my party stream prefab so let me open that and enable it okay so here we have another prefab for party member okay so in here i'll add another text which we can use to show if the pokemon is able to learn the move or not okay so let me just duplicate my name text and i'll call this text something like message text because i want to use this text to show any messages that i have not just the able or not able message that we are trying to show now okay so let me bring this down and for the text i'll write something like not able just for getting some reference so now if we go out of this prefab you can see we have the text in our party member ui so let me actually change the height of the cell in the grid so right now it's 150 so let me change it to something like 170 okay so that looks better we can make this a bit more bigger and i'll just position it at the exact center okay so next let's go to the party member ui script okay and in here i'll also add a reference to the message text that we created now so let me just copy this and create a reference for the message text okay so let's go inside the party member prefab and assign the message text so next in the party member ui script i'll create a new function for setting a message to our message text okay so this function will take a string as the input and we can simply set that string to our message text okay so by default i don't want any message to be shown so from the init i'll call set message and pass empty string so that nothing will be shown in the message text so next in the inventory ui when we select an item and when we open the party screen if the item is a tm item then we should show if the tm is usable right so inside my party screen script i'll create a new function called show if tm is usable okay and this function will take a tm item as the parameter so inside this function we need to loop through all the pokemons so i'll use a for loop and i'll say i listen count okay so for each pokemon we should check if the tm item is usable right so i'll call tm item dot can be dot and for the pokemon i'll pass pokemons of i okay so if the move can be taught then i'll return able for the message and otherwise i'll return not april okay and i'll store this in a variable called message and i'll set this message to the member slot ui so we can get the member slot ui from member slots of i and i'll call the set message function to set the message okay so next i'll also create a function to clear this messages so let me just copy this function and i'll change its name to clear tm usable message okay so for this function we don't need a parameter and all we have to do is set the message to an empty string so that it will be cleared okay so let's go ahead and call these functions from the inventory ui so here while opening the party stream if the item is dm item then we can call party screen dot show fdm is usable and we need to pass the tm item right so we can convert the item into a tm item using the ascii word all right so next when closed party screen is called we should also clear any messages that was shown right so from here i'll call party screen dot clear dmu simple message by the way we don't have to name this clear tm usable message because this can be used to clear any messages that's shown in the slot so i'll just rename this to clear member slot messages okay so we are clearing it when we close the party screen and when we open the party stream we are showing the tm usable messages if the item is a tm item okay so let's go to unity and try testing this all right so if we try to use a tm or hm item we can see that both charmander and bulbasaur is able to learn the move but pidgey is not able to learn right pg can't learn cut okay so that's working fine by the way let's actually go back and try using a normal item so if i try using a portion then we don't have any message shown so yeah it's being cleared properly okay the messages are only shown for team and hm items yeah that works as expected but the ui is not really good you should go ahead and create a better ui but since this is a tutorial i'm not gonna spend much time on creating a good ui okay so we're done with dm and hm items so i'll stop the video here if you think these videos are helpful please leave a like and consider subscribing to my channel you can also support the series by becoming a patreon so thanks for watching and i'll see you in the next video
Info
Channel: Game Dev Experiments
Views: 886
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: G1RcevTA6Eg
Channel Id: undefined
Length: 37min 15sec (2235 seconds)
Published: Tue Aug 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.