Make A Game Like Pokemon in Unity | #65 - NPCs Giving Items

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to part 65 of my pokemon game series in unity so in this video we'll create npcs that can give items to the player so if i go speak to the npc he'll say some dialogue and he'll give us some items after that okay so we'll look at how to implement this 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 let's implement the feature where the npc can give an item to the player so it's going to be pretty similar to the pickup so let me go to the scripts and in the inventory i'll create a new script called item giver all right so this script will be attached to the npc's that can give an item so let's go implement this so in the script first we need a variable to store the item that we want to give so i'll create a variable of item base and then i'll create a variable for the dialog to be shown before we keep the item okay so before the npc gives an item we need to show a dialogue right so we can store that in this variable all right so next i'll create a public function and this function will be a core team and i'll call it give item so this will take a reference to the player okay so in this function first we need to add the item to the player's inventory right just like we did from the pickup so let me do that from here i'll say player dot get competent inventory to get the inventory and i'll call add item and pass our item okay so once we add the item we need to set a boolean variable to indicate that this item is used so let me create a boolean variable over here and by default its value will be false but once we give the item we will set it to true so next we need to show this dialog so actually we need to show the dialog before giving the item right so let me call the show dialog function okay so the npc will show this dialog before giving the item so next after giving the item we can show another dialogue saying the player received this item so since this is a single line dialog i'll call the show dialog text function and for dialogue i'll say player received this item all right so that's all we need to do to give the item so next let's call this function when the player interacts with an npc so i'll open the npc controller and in here first we need to grab a reference to the item giver all right so let me cache a reference from the awake okay so now in the interact function i'll check if the item giver is not equal to null then we can give the item by calling item giver dot give item okay so here we need to pass the player controller so i can get that from initiator dot get component player controller okay so if the item is not equal to another we can keep the item but we also need to check if there is actually an item inside the itemgiver script so this should not be null right so we should also check that and then we should also make sure that this item is not already used okay so what i'll do is i'll create a public function called can be given so this function will return true if item is not equal to null and the item is not already used okay so we can simply return this value so now in the npc controller we should also check if item giver dot can be given is true so now we'll only call this function if the item can be given and otherwise we can simply show our default dialog like before okay so let's go to unity and test this so we need to add the itemgiver script to our npc so i'll add the script from inside the prefab all right and by default in the prefab i won't fill any values so by default the npc won't give an item okay but if we want to give an item then we can assign an item to the slot so let's say i want to give a pokeball and then we also need to specify the dialog that should be shown before giving the item okay so let me just write dialog here okay so now the npc should give this item when they go speak to him so let me try speaking to the npc okay looks like it's not working so let's check if we made any mistake in the code okay so here's the mistake so the give item function is a core routine right so when calling it we should use yield return otherwise it won't work all right so let's try testing this now okay all right so now you can see the npc is saying the dialogue that we put inside the item giver and after the dialogue he will give us the pokeball okay so that's working fine and now if i speak to the npc again you can see that he won't give us the item anymore instead he'll say the default dialog that is in the npc controller okay so that's working fine so we can easily create npcs that can give item by assigning values to the itemgiver script and in case you don't want the npc to give an item then you can just leave these fields empty in the item giver and the npc will behave like a normal npc and show the dialogue inside the npc controller okay so let me get rid of this in pc so next what if you want to give multiple items so for example let's say we want to give five pokeballs to the player how can we do that so to do that in the itemgiver script we can add a new variable for the count of the item that we want to give so here i'll create an integer variable called count and then then adding the item to the inventory we can pass the count of the item as the second parameter okay so now since we can have multiple items we need to make some changes to the dialog so this will be the default dialog so let me store it in a variable called dialog text all right but if the count of the item is greater than one then we need to change the dialogue a little so instead of saying player received the item we'll also append the count into this all right and i'll just make this plural okay so let me pass the dialog text variable here all right and the one last thing i want to do is the can be given function should only return true if the count is greater than zero right if the count is zero it means we don't have any item to give all right so let's go to unity and test this so in my npc i'll change the count to 5. and by the way we can just set the default value of count to 1 because that will be the most common value and we don't have to change it every time we attach the item give a script okay so let's go back to unt and test if the npc gives five pokeballs all right so currently we have five pokeballs with us so let's go talk to the npc now okay first you'll see the dialogue and here you can see he gave us five pokeballs okay so now if i check the inventory you can see that we got the five pokeballs and now in total we have 10 pokeballs okay so next the one thing i want to change is when the npc gives the item it says player received five pokeballs right so here instead of saying player we should show the name of the player right so we have the name of the player in player controller so we just have to use that instead of saying player okay so here i'll say player dot name and let me also change it over here okay so let's also do the same for the pickups so in here we don't have access to the player controller we just have the transform of the player so let's get the player controller from the transform we can simply use the get component okay and to get the name we can call the name property okay so let me show this in a variable called player name and let me use it over here all right so let's go to unity and test this okay let me go speak to the npc so now it's showing the name of the player so next let me try taking the pickup all right so here also it's showing the name of the player so in case you don't remember the name of the player is assigned in the player controller so yeah over here you can see we have assigned the name so that will be the one shown in the dialog okay so i'll stop the video here if you think this video is 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: 923
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: heTCV98f6MM
Channel Id: undefined
Length: 14min 57sec (897 seconds)
Published: Sun Sep 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.