Make A Game Like Pokemon in Unity | #64 - Adding Pickups

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome to part 64 of my pokemon game series in unity so in this video we will implement pickups so here we have a pickup and if i go take it you can see that i'll receive the item inside the pickup and it will be added to my inventory all right so let's 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 all right so let's create pickups so let me open the hometown scene and i'll add the pickup in here so let me add a new sprite and call it pick up let me just reset its position and for the sprite i'll use the pokeball sprite all right so it's not visible right now because the sorting layer is default so i'll change it to foreground and now it should be visible okay so let me place it somewhere over here and i'll make sure it's at the center of the tile so next i'll add a box collider to it so that we can detect collisions all right so if you want you can go ahead and resize the box glider but this looks fine to me so i'll just leave it like that so the pickup is something that the player can interact with right so just like npc and trainer pickup is an interactable object so i'll put this in the interactable layer just like we did for npc and trainer okay so next i'll create a script for the pickups so inside script in inventory i'll create a new script called pickup okay let me just get rid of this so pickup is an interactable object right so it's going to implement the interactable interface all right now we are having an error because we have to implement the interact function if we implement this interface right so i'll go ahead and implement it all right so to check if the interaction is working i'll write a debug.log statement here and we have an error here since this function is a core routine we have to use at least one heal return statement in this function or we can also use yield break so let me just use that and now the error should be gone all right so let's go to unity and attach the script to the pickup game object okay and let's test if the interaction is working okay so when we test we have this issue the inventory ui is active by default so let's go to our essential objects prefab it's inside main so let's open this prefab and let's make the inventory ui inactive by default okay so now we should be able to test it all right and let's try interacting with the pickup so yeah here you can see the interaction is working so next when we interact with the pickup we should get the item inside that pickup right so i'll create a variable to store the item in this pickup so this will be of type item base okay so next in the interact function we should add this item to the player's inventory right so if we go inside the inventory script right now we don't have any function to add an item to the inventory so let's create a function for that okay i'll call this add item and it'll take the item that we want to add and we can also take the count in case we want to add more than one item so i'll make the count optional and give it a default value of one all right so in this function we need to add the item into our item slots right but to get the item slots first we need to find the category of the item since each category has different slots we need to find the category first so for that let me create another function called get category from item okay and this will take the item as the input and by the way this should return the item category right item category is this enum over here so if item [Music] is a recovery item then we can return the recovery item category so i'll return item category dot items so yeah recovery items is just called items right so let me return that category and else if item is a pokeball item then i'll return item category dot pokeballs and otherwise we can return [Music] category dot teams since that's the only category left okay so let's use this function from here to find the category of the item so i'll call this function and pass the item as the parameter so this will return the category as an enum right but what we want is the index of the category so let me convert it into an integer and let me store it in a variable car category all right so next we can use this category to find the slots of it so we can do that by calling this function so let me copy this and paste it over here so now we have the slots so next we need to check if this item already has a slot in the current slots so for that i'll say current slot start first or default and we need to find the slot with the item equal to the item that we are trying to add right so let me store this in a variable called item slot so if this item already has a slot then it will return it otherwise it will return not okay so next we can check if the item slot is not equal to null then that means the item already has a slot so all we have to do is just increase the count of the slot okay so i'll say item slot dot count plus or equal to the count that we want to add otherwise if this item doesn't have slot then we'll have to add a new slot for it right so i'll add a new slots to the current slots by using the add function right so we need to specify the item and the count while creating a new slot so we have an error over here so it's because the item property doesn't have a setter right now so let's add a center for this property so i'll define it in this way and define getter and setter all right so now the error should be gone so we have added the item to the inventory so once we add we need to call the on updated event so that the changes will be reflected in the ui all right so i'm going to invoke this event and that's all we have to do now we can call this function to add an item to the inventory so let's go to the pickup script and inside the interact function we can add the item to the player's inventory so we can get the inventory from the initiator the initiator will be the player right so i'll say initiator dot get component inventory so this will give us the inventory and now we can call the add item function and pass the item okay so this will add the item to the inventory but we need to handle one thing we should not be able to use this pickup multiple times right so i'll create a boolean property called used so this property will be false by default and we only want to use the pickup if it's not already used right so i'll check if the value of used is false and only then i'll add the item to the inventory okay and once we add the item we can set used to true so that it can't be used again and we also need to destroy this pickup right we don't want to see the pickup in the scene anymore so to destroy a game object you can use the destroy function but i don't want to use that the reason is because when we save the game we should be able to save whether the pickup is used or not right so if you destroy the pickup then you won't be able to save its state so instead of destroying it i'll just disable its sprite renderer so that we won't be able to see it in the game okay so i'll use get component to get the sprite render and i'll just disable it all right and i'll also disable the box glider off the pickup so that the tile will be walkable okay and finally we need to show a dialogue saying the player found this item so let me do that by the way this should be false since you want to disable it so let me change both of them to false alright so let's go to unity and test this so in the pickup we need to assign an item so let's say this pickup has max portion inside it so i'll choose max portion for the item so let's test this okay so right now if we look at the inventory we only have one max portion but if i go and take this pickup okay we have an error so i found the issue after some debugging so if you click on this error you can see this is the line that is causing the issue so the issue is when we try to access the height of the item slot ui it returns a null reference exception because rect transform is null so let me explain what's happening so in the inventory once we add an item we are calling the on updated event so this will update the item in the ui so if we look at the inventory ui script here you can see when on updated event is invoked it will call the update item list function so this function will delete all the items.ui and it will create new ones okay so what's happening is when we are doing this we are trying to access the height before the rec transform is assigned from the awake okay so because of that red transform is still null and we'll get another reference exception so we need to make sure this line is executed before we use the height property okay so how can we do that so here you can see right after creating the item slot ui we are calling the set data function right so if you move this line into the set data function then we can ensure that the rec transform will be assigned right after it's created okay so that should solve the issue for us so let's go to unity and test all right so as you can see that we only have one max portion in the inventory so now i'll try to pick up this item all right that worked we don't have the null reference exception anymore and now if we check our inventory you can see that we have two max portions all right so the pickup is working fine but there is an issue that we need to fix so let me show you the issue so when i pick up this item this dialogue will be shown and when this dialogue is shown i'll still be able to walk around okay so let's look at why that's happening so let's look at our code for showing dialogues so here we are using show dialog text function and in this function we are not invoking the on show dialog and on closed dialog events okay so if you look at the show dialog function that shows multiple lines of dialogue here we are doing it right here we are invoking both events but we are not doing the same in show dialog text function so let's go to the game controller script and check what this events are doing all right so here you can see when this event is fired we are changing the state to dialogue so if we don't change the state then the player will still be able to move okay so we should call that even in the show dialog text function the reason why we didn't call it until now is because until now we were just using it from the inventory ui so we didn't have to worry about the player moving when the dialog is shown but now since we are using it from the pickup we have to make sure to invoke the on show and on close events okay so at the start i'll invoke the onshore dialog event all right and when we close the dialog i'll invoke the on close style argument all right so one thing to note is that in the game controller when on close dialogue is fired we are just changing the state back to free roam right but we can't do this now because now we are also showing the dialogue in the inventory ui right so in that case it should go back to the back state instead of going back to the free roam state okay so what we have to do is before changing the state dialog we have to store the previous state in a variable and then we have to restore the state from on closed dialog okay so let me create a variable to store the previous state actually we can use this variable over here we are using this variable to store the previous state before we pass the game so we can use the same variable before we show a dialog okay so let me just rename this to something like previous state so that it's more clear and then in the show dialog function before changing the state i'll store it to the previous state variable and in beyond close dialog instead of changing the state back to free roam i'll just change it back to the previous state all right so that should fix our problem so let's go to unity and try testing this okay so let me go take the pickup all right so now when the dialogue is being shown i can't walk when i press the arrow keys okay but once dialog closes we can walk like usual so that is working fine 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: 1,105
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 pickups tutorial, unity pickup system, unity pickup item
Id: wDLaMrZpJo4
Channel Id: undefined
Length: 21min 48sec (1308 seconds)
Published: Sun Sep 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.