An InventoryComponent
enables its containing Actor
to store InventoryActors
in its inventory, and place them back into the game world.
Make sure you've followed the Axis Mappings – keyboard, mouse and gamepad directional input for an FPS character recipe in Chapter 6, Input and Collision, before continuing with this recipe, as it shows you how to create a simple character.
Also, the recipe Instantiating an Actor using SpawnActor in this chapter shows you how to create a custom GameMode
.
Create an
ActorComponent
subclass using the engine calledInventoryComponent
, then add the following code to it:UPROPERTY() TArray<AInventoryActor*> CurrentInventory; UFUNCTION() int32 AddToInventory(AInventoryActor* ActorToAdd); UFUNCTION() void RemoveFromInventory(AInventoryActor* ActorToRemove);
Add the following function implementation to the source file:
int32 UInventoryComponent::AddToInventory(AInventoryActor* ActorToAdd) { return...