cptscrimshaw
Member
Hi there! I'm trying to figure out how to use the item action of an item in the player's inventory (when the inventory isn't open), through code. What would be the best way to go about that?
Thanks!
Thanks!
public void ManuallyCallDropItemAction()
{
//To call an item action directly in code you will need 3 things
//1) The ItemInfo
//2) The ItemAction
//3) The ItemUser
//First get the item you want. Here we'll get the first stack of apples we find.
var result = m_Inventory.GetItemInfo(InventorySystemManager.GetItemDefinition("Apple"));
//if the result does not have a value we do no have Apples in the inventory
if(result.HasValue == false){ return; }
//We have the apple ItemInfo now.
var appleItemInfo = result.Value;
//Lets trim to 2 apples maximum
appleItemInfo = new ItemInfo(Mathf.Max(2, appleItemInfo.Amount), appleItemInfo);
//Lets get the ItemUser. This is usually placed of the same gameobject as the Inventory, but not always
var itemUser = m_Inventory.GetComponent<ItemUser>();
//now lets create the drop item action, You can cache this object and reuse it if you which for more performance.
var dropItemAction = new DropItemAction();
//You must initialize the item action before using it. This can be only once
dropItemAction.Initialize(false);
//OPTIONAL: check if you are allowed to invoke the action (it will do this anyways internally)
var canInvoke = dropItemAction.CanInvoke(appleItemInfo, itemUser);
//Invoke the action
dropItemAction.InvokeAction(appleItemInfo, itemUser);
//For some actions you'll even have extra data you can use
//For example drop action keeps track of the last dropped pickup object
var droppedPickup = dropItemAction.PickUpGameObject;
}
This worked flawlessly. Thank you!An ItemAction is a simple C# class.
So for example lets say you want to drop an item with the drop item action. First you need to find the item you want to call the item action on.
I've also added the code below in th documentation: https://opsive.com/support/documentation/ultimate-inventory-system/item-actions/
Code:public void ManuallyCallDropItemAction() { //To call an item action directly in code you will need 3 things //1) The ItemInfo //2) The ItemAction //3) The ItemUser //First get the item you want. Here we'll get the first stack of apples we find. var result = m_Inventory.GetItemInfo(InventorySystemManager.GetItemDefinition("Apple")); //if the result does not have a value we do no have Apples in the inventory if(result.HasValue == false){ return; } //We have the apple ItemInfo now. var appleItemInfo = result.Value; //Lets trim to 2 apples maximum appleItemInfo = new ItemInfo(Mathf.Max(2, appleItemInfo.Amount), appleItemInfo); //Lets get the ItemUser. This is usually placed of the same gameobject as the Inventory, but not always var itemUser = m_Inventory.GetComponent<ItemUser>(); //now lets create the drop item action, You can cache this object and reuse it if you which for more performance. var dropItemAction = new DropItemAction(); //You must initialize the item action before using it. This can be only once dropItemAction.Initialize(false); //OPTIONAL: check if you are allowed to invoke the action (it will do this anyways internally) var canInvoke = dropItemAction.CanInvoke(appleItemInfo, itemUser); //Invoke the action dropItemAction.InvokeAction(appleItemInfo, itemUser); //For some actions you'll even have extra data you can use //For example drop action keeps track of the last dropped pickup object var droppedPickup = dropItemAction.PickUpGameObject; }
Note: not all item actions will work calling them directly like this. some are depedent on additional data, like the MoveItemAction and a few others which expect a reference to a ItemViewSlotContaner.