Spawning Items From Inventory on Actor Death

Posted by Josiah Purtlebaugh on Wednesday, August 26, 2020

Summary of accomplishment:

  • Began abstracting Inventory as an Actor Component Blueprint
  • Monsters can now spawn their inventory as actors upon their death

A quick update today! After implementing the inventory, I realized that what I really wanted was to abstract the Inventory system to its own Blueprint to be added to other actors. But how?

The answer is the Actor Component Blueprint. This type of Blueprint can be added as a component to any Actor and allows the component to handle its own events, contain functions and variables, and tick. For the inventory this is perfect. I created a new Actor Component Blueprint called Inventory_BP and added a variable of type Inventory to it and called it, well, Inventory.

For starters, all I really wanted was to allow Monsters to drop Keys upon dying, so I wrote the event graph below that responds to a new custom event called DropAllItems.

Inventory Actor Component Blueprint

One thing to note: in the graph above, you can see in the For Each Loop that Iventory Items has a member called Class Ref. Class Ref contains a soft class referenced (meaning it may or may not be actually loaded into memory). I added this member to Item so that I could pass it to the Spawn Actor function. Because I am using a soft reference I expect that this code will run into problems if I try to spawn a class that hasn’t been loaded but that is a problem for another time.

Updated Item Struct

With this done, all I had to do was head over to Monster_BP and make a few adjustments. I added a call to the new custom event DropAllItems into the flow that processes monster death. Also, in the BeginPlay event chain I passed the Monster’s local Inventory variable into the Inventory variable contained in Inventory_BP. In the future I will set the contents of Inventory from a Construction Script so that I don’t have to juggle it in two places.