# How does the system work?

***

## Design structure

With how strict the performance requirements were for this plugin, I’ve had to design this inventory system slightly differently from other systems you might find either on the marketplace or Youtube tutorials. The biggest thing to keep in mind is that this system does NOT rely on widgets being valid or created to keep track of its data or run gameplay logic.

This is to achieve five things:

1. This greatly speeds up widget construction since all the data they need (for example where an item is inside the container and so forth) is already prepped.
2. Allows you to remove widgets from memory and still have the necessary data to find out important things like looting an item and finding a free space for that item.
3. Since widgets have way fewer references, there is less work to be done to manage widgets getting garbage collected properly.
4. Designers are able to create new widgets and prototype much faster since all the data they need about containers and items is always available from the component.
5. You have complete control over when you want to construct these widgets, meaning you can reduce lag spikes when starting your game or even speed up the time it takes for your players to load a level or area.

<mark style="color:purple;">**ContainerSettings**</mark> is an array inside of <mark style="color:yellow;">**AC\_Inventory.h**</mark> and this is your inventory. Each container hosts a set of items and dictates its dimensions, compatibility settings and so forth. Some settings are hidden as they are not relevant to designers.

When creating a widget for a container, the widget uses a <mark style="color:purple;">**UniqueID**</mark> to find out what container it is representing. This is typically referred to as "Binding" a container with a widget.

Items differentiate themselves from each other by using a <mark style="color:purple;">**UniqueID**</mark> as items can have the same name, same data asset and so forth, they can even share the same position inside a container while in-editor. A component can not give items or containers the same <mark style="color:purple;">**UniqueID**</mark> twice. This is essential for functions to work properly.

The component is designed to live on individual actors, not the player controller. Though there is no reason why it can't live on the controller, but I won't be actively testing it that way.

It is highly recommended to head over to the [AC\_Inventory](../classes-and-settings/ac_inventory) page and giving a read. Understanding the main component that makes this system work is crucial to understanding the in's and out's of the system.

***

## How are the infinite items inside of items achieved?

Since UE does not support recursive structs and recursive structs are difficult to manage, containers use a coordinates system explaining what item they belong to. The array is simply containers and those containers have an array of items. The only thing the compoonent needs to know whether a container is owned by the component or owned by an item is by checking <mark style="color:purple;">**FS\_ContainerSettings**</mark>**.**<mark style="color:purple;">**BelongsToItem**</mark>.

I recommend reading the code comment for the <mark style="color:purple;">**FS\_ContainerSettings**</mark>**.**<mark style="color:purple;">**BelongsToItem**</mark> property and then looking at <mark style="color:yellow;">**AC\_Inventory.h**</mark> -> <mark style="color:red;">**GetItemsContainers**</mark> for an example.
