Make A Game Like Pokemon in Unity | #39 - Forgetting Old Moves To Learn New Ones

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This is part 39 of my Pokemon Game tutorial series in unity. In this video, we'll let the player forget the move of a pokemon if it already has 4 moves and wants to learn a new one.

If you're new to it, this a series where we'll create a Turn-Based RPG like Pokemon in unity using Design Patterns and Good Game Development Practices. I'd love hear what you think about it :)

Here is the entire playlist.

👍︎︎ 1 👤︎︎ u/GameDevExperiments 📅︎︎ Feb 13 2021 🗫︎ replies
Captions
hey everyone welcome to part 39 of my pokemon game series in unity so in this video we let the player forget to move if the pokemon has four moves and wants to learn a new one so for example charmander has four moves right now and if i level up to level 13 then he's trying to learn flamethrower but since he has four moves he'll have to forget one of his moves in order to learn the new one all right so we can select the move to forget from here so flamethrower is a new move and if we select flamethrower itself then charmander will not learn the new move but if we select one of its existing moves then it will forget that move and learn flamethrower so if i select crowd then it will forget crowd and learn flamethrower all right and now if we start another battle the charmander no longer has growl and instead it has the flamethrower so we look at how to implement that in this video special thanks to all my patreons for making the series possible by becoming a patreon you can support me in the making of the series and get access to some cool rewards like the complete project files of the series which also contains some advanced features that are not covered here so let's start the video so in the previous video we made the pokemon learn new moves when it reached certain levels but if the pokemon already has four moves then right now we are not doing anything so what we have to do is we have to let the player forget to move so that you can make space for the new mode so first let's create a ui that will let the player select a move to forget so let me go to unity and let me enable the battle system okay so i just want to make a small box that lists the current most of the player pokemon and the new move that it wants to learn so inside my battle canvas i'll create a new image okay and let me rename this image to move selection ui all right and for the image i'll choose the image of a dialog box itself okay and i'll change its width and height to 250 by 300 all right that looks okay to me so let me just reposition this so next i'll create the text fields to display the move names so i'll add a new text all right i'll just call this more text and i'll change the font to orange kit i'll make the font size 40 and let me just change the text to move okay let me make this big so that it's visible next i want to align the text to the center both vertically and horizontally okay so let me just place this text over here and we need to create four more copies of them so i'll use ctrl d to duplicate and create four more copies so totally there are five more texts so next i want to align these text vertically one after the other so you can do that manually by dragging each text but a better way to do that is to use the vertical layout group so i'll add a vertical layout group to the new selection ui okay so now you can see that all our mode text are automatically aligned so in the settings of the vertical layout group i'll change the child alignment to middle center so that everything is centered and i don't want so much spacing between each moose so let me just reduce the spacing let's say something like -20 so that's it for the move selection ui i just want to go with this simple ui but if you want you can make it big and show things like the power and maximum pp of the move and all that so next let's create a script for the move selection ui so inside my scripts folder inside battle i'll create a new script called mouse selection ui and inside the script we need references to the list of more text so i'll create a variable for that so we have to import you know the engine dot ui in order to use text okay and i'll name this variable mode text so next i'll create a public function to set the move names to these texts so i'll call the function set mo data so this function will take the list of current moves the pokemon has so i'll call it current moves and it'll also take corruptions to the new move all right this is because we have to show both the current modes and the new move in the move selection ui so first i'll set the names of the current nodes so for that i'll loop to the current moves list using a for loop okay and inside the for loop i'll say move text of i dot text equal to the name of the current mode right so to get the current move i'll say current moves of i dot name all right so this loop will set the name of the current moves to this list so next i'll say the name of the new move so this for loop will run from zero to current moves con minus one okay so it will set the text of all the move text from the index zero to current moves count minus one so the next index that we have to use is current moves dot count so i'll say move text of current moves dot count and i'll set the text of this to the name of the new all right so we're done setting the move names so let's go to unity and let's add the script to our move selection ui game object okay and we need to assign all these smoke text to the list so let me just lock this inspector in order to assign it okay so that's assigned so next let's go to our parallel system script and if the player pokemon already has four moves then we have to show the move selection ui and let the player select a move to forget so what i'll do is i'll create a new state called move to forget all right and just like for all the other states i'll create a function start that state so i'll create a new function called choose move to forget and this function will take a reference of the player pokemon and it will also take a reference of the new move so at the start of the function i'll set the state to bc so that nothing else will happen and then i'll show a dialog that says choose a move that you want to forget and after that we need to enable the move selection ui right so for that i need a reference to the move selection ui so let me create a reference here okay and we can go to unity and assign the reference so i'll just assign the more selection ui game object to the variable and the balance system script and now inside our choose note to forget function we can enable the move selection ui so i'll say move selection ui dot game object dot set active true all right so by default the most selection ui should be disabled and when we go to the state then we'll enable it so next after enabling it we have to set the move names to the most selection ui so for that we can use the set mode data function that we just created earlier so the function first we have to pass the current move of the pokemon so i can get that from pokemon.nose but one problem is pokemon.moose is a list of move class all right and it's not a list of move base class but in our set data function we are expecting a list of more base class so to get a reference of mo base it's actually pretty simple we just have to say move dot base right we can just do this if we want to get the move base from the move class but what can we do for a list of moves we'll have to call the dot base property for all the moves in the list right we can't simply say moves of 0 dot base we'll have to do that for all the moves in the list so how can we do that so there's actually a simple way to achieve this using link so what we can do is we can say moves dot select okay we'll have to import link in order to use the select function so inside select we can say x arrow x dot base and after the select we have to call dot to list in order to convert it into a list so basically what this will do is this will convert a list of move class into a list of move base class so if this sounds confusing to you i recommend spending some time to learn the basics of link so let's continue so we passed the first parameter which is the current moves of the player pokemon so the second parameter is the new move which we can just pass directly all right so this line will set all the more names in the more selection ui so once we show the most selection ui and set the move names we can set our state to move to forget state all right so now let's actually call this function when the player pokemon is trying to learn a new move but it already has foremost so here i'll remove the studio command and i'll call choose move to forget corrupting okay so here we have to pass the player pokemon and the new move it's trying to learn okay so this new move is actually an instance of learnable move class so we have to use new move dot base since we are expecting move base as the second parameter and by the way before calling this function i want to show a few dialogues so first i'll show a dialogue like the pokemon is trying to learn this move but it cannot learn more than four moves so here instead of four i'll use pokemon base dot max number of moves just in case you might want five or six moves as the max in your game so let's go to unity and test this so let me just disable the battle system and let me look at the most of my charmander so let me just add one more move at level seven so that my charmander will have moves by default so let me just add a quick attack and i'll set its level to 7 and just to make things easy to test i'll increase the xp yield of bulbasaur even further i'll make it 400 so beating a bulbasaur will produce more xp gain and we'll be able to level up just by beating one bulbasaur so let's try testing this all right so currently my charmander has four moves and when it levels up to level 13 it'll try to learn flamethrower so let's see what will happen so let me try to beat this purple saw real quick all right so the bulbs are painted and charmander should gain enough xp to level up all right and go to level 13 and now you can see it's trying to learn flamethrower but it can't learn more than four moves so we have to choose a move to forget so we are successfully showing the move selection ui so next we should let the player select demo from the ui and then forget that move in order to learn the new move so let's do that so in the handle update function if the state is moved to forget then we should call a function to handle the selection of the mouse all right we should create a function like this in order to handle more selection we already have a function for handling more selection so this function is used to select the move to use in the battle and in this case the move is arranged as a grid not as a vertical list so we can't use this function we'll have to create another one so what i'll do is i'll create the handle move selection inside the move selection ui script the battle system script already has lots of different logic inside it so it's better to encapsulate logic into other scripts so inside my move selection ui script i'll create a public function call handle move selection and i'll create an integer variable for the current selection all right and initially i'll set it to zero so next inside the function if the down arrow key is selected then we can increment the current selection all right and otherwise we can decrement it so next we have to clamp the selection between 0 and the maximum number of moves so i'll just use mathis.clam for that and i'll slam it between zero and the maximum number of moves which i can get from pokemon base or max number of modes all right so next we have to update the move selection in the ui so for that i'll create another public function call update move selection and this function will take the current selection as a parameter and inside it i'll use a for loop to loop through all the mode text so here the loop should be between 0 and the maximum number of moves plus one so all this is pretty similar to what we have done for the move selection and action section earlier so that's the reason why i'm not i'm not explaining this in depth so here inside the for loop if i is equal to the selection then we want to highlight that move text right so for that i'll create a variable called highlighted color and i'll set the color of the move text to the highlight color all right and otherwise i'll just set the color to black so now from the handle move selection we can call the update mode selection function and pass the current selection as the parameter so now let's call this function so inside the handle update if the status move to forget then we can car move selection ui dot handle no selection so this will let the user select different moves from the mouse list but one thing we have to handle is right now after going to the move to forget state we'll go down and we'll end the battle since the pokemon actually fainted so we should not end the battle until the player selects a new move so to do that below this i'll call wait until coroutine and inside this i'll say i want to wait until the state changes from the mode to forget state okay so we will wait until the player selects a move and only after that we will end the battle so let's try testing this so before we test we need to set the highlighted color from the inspector so let me change it to something like blue and i need to change the alpha so that it will be visible all right so let me test if it's working so yeah once the timer will level up it'll try to learn a new move and our move selection ui is shown but i'm not able to select different moves using the down and up arrow keys so let's check what the issue is so yeah here is the issue in the handle move selection i am incrementing the current selection if the down arrow key is pressed and otherwise i am decrementing it so whenever down arrow key is not pressed the selection will keep decrementing and that's why it's not changing in the ui so instead we should actually add a condition here saying else if up arrow key is pressed only then we should decrement the current selection okay so this should solve that issue and another issue that i noticed is the flamethrower is not fully shown so it does that we can pause this and then we can try resizing this text to see if that comes so yeah here you can see if i make the text big and then on pause the game the flamethrower is being shown so what we have to do is we have to increase the size of all the text in our move selection ui so let's do that okay i believe that should be enough and just to test it we can try changing the text of this so yeah that's fitting and in case if it's not fitting then you can reduce the font size so i'll just change this back and i'll disable the move selection ui disable the battle system and try to test the game again so now you can see that flamethrower is being shown and i'm also able to select different moves by using my up and down arrow keys so that's working fine so next when the player selects a move and presses it the pokemon should forget that move and learn the new move right so let's look at how to do that so inside our handle move selection function if the user presses the z key then we should forget the selected move learn the new move show dialogues and go back to the running turn state but the problem is we can only do all those from the balance system script right we don't have reference to the player unit dialog box battle state and all that inside the move selection ui script so how can we solve this problem so what we can do is in this function we can take an action as the parameter so let me import system in order to use action and i'll call this action on selected and it'll take an integer as the parameter so this action will be invoked whenever a move is selected and we will pass our current selection to the integer parameter all right so let's invoke this action so i'll say on selected dot invoke and for the parameter i'll pass the current selection so next let's create this action from the barrel system script so inside the handle update we also have to pass the action when calling handle mode selection right so let's define the action here so in the parameter of the action we'll get the index of the selected move okay so here also we have to add the integer to show that this action takes a parameter so now let's implement this action so first of all when a move is selected we have to disable the move selection ui right so i'll say move selection ui dot game object dot set active false all right so next we have to make the pokemon forget the selected mode right but what if the player selects a new move itself so let's say the flamethrower is a new move that the pokemon can learn and what is the player selects flamethrower itself so in that case the pokemon should not learn flamethrower and it should keep all its existing moves so there are two cases that we need to handle so the first one is if the move index is equal to equal to 4 4 is going to be the index of new move so instead of writing 4 i'll write pokemon base dot max number of moves just to keep it clean and if that's the case then the pokemon should not learn the new move right so let me write a comment over here and otherwise pokemon should forget the selected move and learn the new so in this case you don't have to do anything all we have to do is show a dialogue saying the pokemon did not learn the new move so one problem is we don't have reference to the new move from here all right we have a reference of the new move in the choose move to forget function so this is a new move but we don't have a reference to it in the handle update function right so what i'll do is i'll create a local variable called mode to learn and in the choose node to forget function i'll store the reference of numo into the variable we created all right so now we can access it from move to learn variable so in this case i'll just show a dialogue saying the pokemon did not learn the new move alright so this is all we have to do in this case so in the second case we have to replace the selected move with the new move right so i'll say playerunit dot pokemon dot moves off move index so this is the selected move and i'll replace it with our new move okay so i'll assign new move off move to learn all right so here i'm creating an instance of the new move and i'm replacing the selected move with the instance of the new move so even in this case i want to show a dialogue so first let me grab a reference to the selected move okay and i just need its base and now i'll show a dialogue like the pokemon forgot the selected move and learned the new move okay so that's all we have to do and after this condition i'll reset the move to learn to know and i'll also set the state back to running turn so that we can continue the battle all right so this is all we have to do when the move so let's pass this action to our handle move selection function all right so now you can see the error is gone so the handle move selection function does not know about what needs to be done when the move is selected and all it has to do is invoke the provided action then a move selected all right so this is handled in a clean way so here when we set the state to running turn what will happen is in the handle pokemon fainted function they will stop the bait until corrupting so here you can see that it'll wait until the move is not equal to move to forget state so when we change the state to the running turn it will stop this wait until core routine and it will execute the rest of the logic so one more thing i want to add here is after we come back to the running turn state i want to wait for let's say two seconds all right so if we don't do that then the user won't have time to process all this so it's better to wait for two seconds before ending the battle so let's go to unity and test this all right let me start a battle real quick okay and let me beat this bulbasaur so that i can level up to level 13. all right so now the charmander is trying to learn flamethrower so we should get our selection ui so let's say i want to forget growl and learn flamethrower so i'll sell crowd so you can see that charmander forgot crowd and learned flamethrower so now if we start another battle you can see that in our moves list instead of crowd we have flamethrower alright so that's working fine so i'll stop the video here if you think this video is helpful please leave a like and consider subscribing to the channel that will really help me out so i'll see you in the next video
Info
Channel: Game Dev Experiments
Views: 2,386
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: INwMSzOrDsc
Channel Id: undefined
Length: 35min 39sec (2139 seconds)
Published: Sat Feb 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.