Our new component contains an array of actors, storing them by pointer as well as declaring functions that add or remove items to the array. These functions are simple wrappers around the TArray add/remove functionality, but allow us to optionally do things such as checking whether the array is within a specified size limit before going ahead with storing the item.
InventoryActor is a base class that can be used for all of the items that can be taken by a player.
In the PickUp function, we need to disable the actor when it is picked up. To do that, we have to do the following:
- Disable actor ticking
- Hide the actor
- Disable collision
We do this with the SetActorTickEnabled, SetActorHiddenInGame, and SetActorEnableCollision functions.
The PutDown function is the reverse of this. We enable actor ticking, unhide the actor, and then turn its collision back on, and we transport the actor to the desired location.
We add an InventoryComponent to our new character as well as a function to take items.
In the constructor for our character, we create a default subobject for our InventoryComponent. We also add a NotifyHit override so that we are notified when the character hits other Actors.
Inside this function, we cast the other actor to an InventoryActor. If the cast is successful, then we know our Actor was an InventoryActor, and so we can call the TakeItem function to take it.
In the TakeItem function, we notify the Inventory item actor that we want to pick it up, and then we add it to our inventory.
The last piece of functionality in the InventoryCharacter is the DropItem function. This function checks whether we have any items in our inventory. If it has any items, we remove it from our inventory, and then we calculate a safe distance in front of our player character to drop the item using the Item Bounds to get its maximum bounding box dimension.
We then inform the item that we are placing it in the world at the desired location.