Thursday, December 20, 2018

Tunics: A Replicated Complex Inventory System in Unreal Engine









This is a project i created during my 2018 winter break. The project is created in unreal engine using C++. I worked closely with my friend, Nicholas Chmil. You can find his breakdown here. I was responsible for creating the inventory, while Nicholas(not me) handled the animation blueprints and adding in content from the unreal store. Everything is properly replicated, so multiple players can share items and  manage weaponry together.



Each item has a constant presence in the world, and can be dropped or thrown at will. Ammunition can be stacked and unstacked with the right and left mouse buttons. 

The Breakdown:

As Stated before, each item has a constant presence in the world. Items in the inventory are rarely spawned or destroyed (only when separating stacks and applying attachments). I did this to keep the actual inventory simple. It is just an array of  structs,which include:  a pointer to the item in the world, a boolean to determine weather or not this slot is full, and an integer to reference the array index of this slots parent.

The parent slot is used for items that are larger than one slot, for instance if i were to drag the shotgun into slot 0, slots 0 through 4 would have slot 0 as their parent. This works on both the x and y axis. As you can see in the GIF above, the ar4 takes up a 4x4 slot.

The item actors (which firearm actors inherit)  holds all the information for the item. Ranging from the animations for the item to the size it takes up in inventory. The most interesting variable held by the item actor is its stack size. This makes it so you can drop an entire stack of items as one item. When items are unstacked, a new actor is spawned into the world. Below is the inventory category for items.
The full item header can be found Here
and the firearm class can be found Here

The complex inventory component holds the major functions called by the player and ui.
The most interesting feature is the ignoreslot argument additemtoslot function. This makes it so the player can add an item to a slot even if it overlaps its previous slot (you can see this happening with the ar4 in the GIF above). I do this because the item is not removed from a slot before it is added to a new slot. If you would like more info on how the complex inventory works, you can check out the breakdown i did on a complex inventory in unity Here

This is the blueprint responsible for when a drop is detected on an inventory slot. First i cast to my custom drag and drop object, which holds information about the item like the  slot it  came from and its aitem pointer. i then branch off if it is splitting a stack because i need to preform special operations when splitting like creating a new actor in the world. After that it is as simple as removing the item from the slot, checking to see if it fits into the new slot, and if it doesn't we just re add it to the old slot.

Full Image


Below is the full code for the character and the inventory component.




ComplexInventory.cpp

Tunics Character.cpp



               

1 comment: