Inventory

Adding Inventory Items

  1. Open or create a new Inventory Database asset. In the desired folder, right-click and select Create -> UHFPS -> Game -> Inventory Asset.

  2. To open the Inventory Builder window, double-click the asset or click the Open Inventory Database Builder button.

  3. In the Inventory Builder window, click on the plus (+) button located on the left side near the Inventory Items label to add a new item.

In order to select an item icon, you need to click on the Select button, which will open up the sprite selection window. It is important to ensure that the Texture Type item icon is set to Sprite (2D and UI), otherwise you won't be able to find the icon.

  1. Define the amount of space that an item will take up in the inventory by adjusting the Width and Height slider in the Item Grid View.

If the height of the image is greater than the width, you can invert the orientation of the image so that the icon will fit the entire space it occupies in the inventory.

  1. If you have created a new Inventory Database, assign it to the Inventory component located in the GAMEMANAGER object.

  1. To pickup your newly created item to the in-game inventory, change the layer of the object to Interact and add the Interactable Item component to it. Then, change the Interactable Type to Inventory Item and assign the pickup item with your item.

The list of items is automatically generated from the Inventory Asset reference from the Inventory component. Therefore, it is necessary to have the Inventory component added to the scene and to have the Inventory Asset assigned.

You can use the item title as an interact or examine title by toggling Use Inventory Title or Use Examine Title in the Item Settings.

You can define Item Custom Data that can be used when using the item. To define the Item Custom Data, simply write whatever you want in JSON format.

To access the custom data, you just need to have a reference to an InventoryItem that represents the item in the inventory. To get that reference, call the GetInventoryItem function from the Inventory component.

Here's the code snippet for that:

using UHFPS.Runtime;
using UnityEngine;

public class InventoryTest : MonoBehaviour
{
    public ItemGuid Item;

    private void Start()
    {
        // get reference to the InventoryItem
        Inventory inventory = Inventory.Instance;
        var inventoryItem = inventory.GetInventoryItem(Item);
        var itemData = inventoryItem.CustomData;

        // get custom data using JObject
        var json = itemData.GetJson();
        string name1 = json["name"].ToString();
        int age1 = (int)json["age"];

        // get custom data using GetValue
        string name2 = itemData.GetValue<string>("age");
        int age2 = itemData.GetValue<int>("age");
    }
}

You can also have nested JSON data that you can access using the JObject reference:

using UHFPS.Runtime;
using UnityEngine;

public class InventoryTest : MonoBehaviour
{
    public ItemGuid Item;

    private void Start()
    {
        Inventory inventory = Inventory.Instance;
        var inventoryItem = inventory.GetInventoryItem(Item);
        var itemData = inventoryItem.CustomData;
    
        // get custom data using JObject
        var json = itemData.GetJson();
        Debug.Log(json.ToString());
    
        string name = json["name"].ToString();
        int durability = (int)json["properties"]["durability"];
        string type = json["properties"]["type"].ToString();
    
        Debug.Log($"Name: {name}, Durability: {durability}, Type: {type}");
    }
}

To change a JSON property value, use the JObject reference directly, but instead of casting the value to a type, simply set the value. After changing the values, use the Update() function to save the changes in the custom data.

using UHFPS.Runtime;
using UnityEngine;

public class InventoryTest : MonoBehaviour
{
    public ItemGuid Item;

    private void Start()
    {
        Inventory inventory = Inventory.Instance;
        var inventoryItem = inventory.GetInventoryItem(Item);
        var itemData = inventoryItem.CustomData;
    
        // set custom data using JObject
        var json = itemData.GetJson();
    
        json["name"] = "lantern";
        json["properties"]["durability"] = 50;
    
        // save json string
        itemData.Update(json);
    }
}

Inventory Item Selector

To open the Inventory Item Selector, derive from the IInventorySelector interface and call the OpenItemSelector() function on the Inventory component.

Here's a code snippet to open the item selector:

using UnityEngine;
using UHFPS.Runtime;

public class InventorySelector : MonoBehaviour, IInteractStart, IInventorySelector
{
    public void InteractStart()
    {
        // open inventory selection
        Inventory.Instance.OpenItemSelector(this);
    }

    public void OnInventoryItemSelect(Inventory inventory, InventoryItem selectedItem)
    {
        var itemData = selectedItem.CustomData;
        var json = itemData.GetJson();
        Debug.Log(json["name"].ToString());
    }
}

In the OnInventoryItemSelect() function, you will have all the necessary references with selected item, so you can perform any desired actions with the item.

To check if an item is a specific item, use the ItemProperty or ItemGuid fields and check if the selected item GUID is equal to the item reference field.

using UnityEngine;
using UHFPS.Runtime;

public class InventorySelector : MonoBehaviour, IInteractStart, IInventorySelector
{
    public ItemProperty RequiredItem;
    // public ItemGuid RequiredItem;

    public void InteractStart()
    {
        Inventory.Instance.OpenItemSelector(this);
    }

    public void OnInventoryItemSelect(Inventory inventory, InventoryItem selectedItem)
    {
        if (selectedItem.ItemGuid != RequiredItem)
            return;

        // do something
    }
}

Combinable Items

  1. To define an item as a combinable item, enable the Is Combinable toggle in the item settings.

  1. Go to the Combine Settings and add new combinations.

  2. To trigger the combine event on the player item after combining it with another item, enable the Event After Combine toggle.

The combine event will only trigger on the second item, so you have to make the combination for example from Battery -> Flashlight.

Also make sure that the second item (Flashlight) is Usable and the Usable Type is Player Item.

  1. In the player item script that derives from the PlayerItemBehaviour, override the CanCombine() and OnItemCombine() functions.

using UnityEngine;
using UHFPS.Runtime;

public class Flashlight : PlayerItemBehaviour
{
    public ItemGuid BatteryItem;
    private bool isEquipped;
    
    // define when you can combine battery with flashlight
    public override bool CanCombine() => isEquipped;
    
    // this function will be called when you combine battery with flashlight
    public override void OnItemCombine(InventoryItem combineItem)
    {
        if (combineItem.ItemGuid != BatteryItem)
            return;

        // do something on combine
    }
}

To equip a player item that can only be combined, enable the Select After Combine toggle. After enabling this toggle, you can select the Result Player Item.

To mirror the combine settings with the combine item, click on the Mirror button. This will transfer the current combination settings to the second item.

You can also create craftable combinations. To enable crafting, just toggle the Is Crafting property. This allows you to set the required amount of the current and second item to craft the result item. The combination will work as normal, but combining items will only be enabled when the required quantities are met.

Last updated