Skip to content

Container Fragment

Summary: Container Fragment
  • Handles where an item is stored in the inventory, by establishing a container and a position within the container.
  • Can be used to determine the best container and position for items.
  • Items can define their initial storage values using the Container Memory struct.
  • Should be added after the Stack Fragment, so it can evaluate all stacks created for new items.
  • Can be used to set additional containers linked to the item, representing its own storage.
  • Supports for moving and swapping operations.

The Container Fragment, represented by the ItemFragment_Container class, is responsible for managing where items are stored in an inventory. An item is stored in a given container and at a position inside this container.

You can define an item placement in three ways:

  1. By defining an item's default values when the item is added to an inventory.
  2. By allowing the fragment to pick the best placement available.
  3. By setting any container and position via the fragment's appropriate setter functions.

When configuring default items, you can define their initial container and position within the container. The fragment will prioritize these values when evaluating new items, but will ensure they make sense, considering if the container has slots, if the position is taken, or even if the item and container are compatible. If needed, the fragment will modify the placement. If no proper placement is possible, the item will be configured to be dropped instead of added.

Options one and two are also relevant when dealing with the Stack Fragment, which evaluates new items and breaks them into appropriate stacks. You want the Container Fragment to run after that evaluation so it can properly place all newly-generated stacks before the item is added to the inventory.

Container and Stack fragments

Make sure to place the Container Fragment after the Stack Fragment! You want the Container Fragment to assign all stack instances to the appropriate containers and positions.

When configuring the Container Fragment for an item, you can also define additional containers that must be added to the inventory. This is usually necessary when you are creating items that are containers themselves inside the inventory, such as a box or a pot. Keep in mind that items that could be equipped, such as a backpack, probably want to grant containers via the equipment, and not just by being stored.

The following operations can be executed by the Container Fragment:

  • Move Item: Allows a drag-and-drop interface to move an item to a new position or container.
  • Swap Item: Allows a drag-and-drop interface to swap two items that cannot be combined.

Even though the Container Fragment isolates all container logic, the concept of storage is important to the Inventory Manager Component. However, by design, this component should never interact with any specific fragment directly, as they may be extended or even replaced in a specific project. Because of this, the fragment implements the StorableItemInterface, which exposes all important functionality to the Inventory Manager Component.

If you plan on creating your own Container Fragment and do not want to extend from the original class, make sure to implement the correct interface. However, it is highly recommended to extend the original class to leverage its memory design, operation logic, and other features.