Inventory
Adding Inventory Items
Open or create a new Inventory Database asset. In the desired folder, right-click and select Create -> UHFPS -> Game -> Inventory Database.
To open the Inventory Builder window, double-click the asset or click the Open Inventory Builder button.
In the Inventory Builder window, click on the plus (+) button located on the left side near the Inventory Items label to add a new section.

To add a new item, right-click on the section and select Add Item.

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 you have created a new Inventory Database, assign it to the Inventory component located in the GAMEMANAGER object.

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.


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());
}
}
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
To define an item as a combinable item, enable the Is Combinable toggle in the item settings.

Go to the Combine Settings and add new combinations.
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.
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
}
}


Last updated