Question: Custom Exchange and Crafting

Theremy

New member
Hi, I have questions.

I want to make a Slot Collection UI for a crafting (Like screenshot below).
So it's supposed to work like this:
  1. I have two Inventory UI from two different inventory (Left window: Player, Right window: Crafting desk)
  2. Player need to drag the required material from the inventory to the correct slot on the right window
  3. When all correct material is dragged, the [Instantiate] button on the right is active, and when player click that, we instantiate the desired item
  4. If player click cancel, all the item that was dragged need to be moved back to the player inventory (Left window), the returned order doesn't matter

My problem:
  1. I have a problem with no. 4. In my custom script, I have a reference to the crafting desk inventory, but, how do I get info of what items are currently in that Crafting Desk inventory?
  2. Do I really need custom script for this? I feel like it's not required, but I can't figured that out, lol
 

Attachments

  • Capture.PNG
    Capture.PNG
    32.6 KB · Views: 1
Do you need to know the list of items in the desk inventory or do you need to know in which slot they are within the itemSlotCollection?

Here is some code example I gave someone this morning, it might be relevant to you:
C#:
var equipmentItemCollection = m_Inventory.GetItemCollection(m_EquipmentItemCollectionID) as ItemSlotCollection;

//Use the slot Index
var itemAdded = equipmentItemCollection.AddItem(itemInfo, slotIndex);
var itemInSlot = equipmentItemCollection.GetItemInfoAtSlot(slotIndex);
var itemRemoved = equipmentItemCollection.RemoveItem(slotIndex, amountToRemove);

//Or use the slot name
itemAdded = equipmentItemCollection.AddItem(itemInfo, slotName);
itemInSlot = equipmentItemCollection.GetItemInfoAtSlot(slotName);
itemRemoved = equipmentItemCollection.RemoveItem(slotName, amountToRemove);

var item = equipmentItemCollection.GetAllItemStacks()[0];
//Might not be equal to
var otherItem = equipmentItemCollection.GetItemInfoAtSlot(0);

You can get all the items in an inventory (includes all the collections) with
Code:
var allItemInfos = m_Inventory.AllItemInfos;
AllItemInfos is a list that is cached whenever the item collection within the inventory change
 
... or do you need to know in which slot they are within the itemSlotCollection? ...

This would be something I'd like to hear for items currently equipped and thus within the equipment itemslot collection :)

Of course you can always filter through the ItemSlots and compare until whatever Item you have equals the item within a particular slot, but if there is something built-in anywhere already, that might be a better option.
 
hum... I'm not sure I understand your last comment.

The code I pasted above shows you how you can get an item that is within a slot (in an ItemSlotCollection).
If you are searching from the ItemViewSlotCollection (in the UI). There should be some similar functions to find the ItemViewSlot using the ItemSlotSet slot names or indexes.
 
The question was not in regards to getting an item if I know the slot but the other way around. If I have the item as variable, is there an easy way to retrieve which itemslotindex it is sitting in?
 
Thanks for the response!
And yes, it runs as intended.

For those who needs a similar feature, I hope this might help!
Also @Sangemdoko, if there's a better way to do this, please suggest.

using System.Collections.Generic;
using UnityEngine;
using Opsive.UltimateInventorySystem.Core.InventoryCollections;
using Opsive.UltimateInventorySystem.Core.DataStructures;

public class ItemTransfer : MonoBehaviour
{
[SerializeField] Inventory playerInventory;
[SerializeField] Inventory crafterInventory;

public KeyCode moveItem;

private void TransferItem()
{
var allItemInfos = crafterInventory.AllItemInfos;
for (int i = 0; i < allItemInfos.Count; i++)
{
if(allItemInfos != null)
{
playerInventory.AddItem(allItemInfos);
crafterInventory.RemoveItem(allItemInfos);
}
}

allItemInfos = new List<ItemInfo>();
}

void Update()
{
//for testing purpose
if(Input.GetKey(moveItem))
{
TransferItem();
}
}
}



Btw, OOT a bit,
I keep getting error from the image below every time I hit Play (which currently doesn't break anything)
But, do you have any suggestion what to check to fix that?
 

Attachments

  • ItemSlotCollection error.PNG
    ItemSlotCollection error.PNG
    119 KB · Views: 2
@Stormi
The question was not in regards to getting an item if I know the slot but the other way around. If I have the item as variable, is there an easy way to retrieve which itemslotindex it is sitting in?
Sorry I misunderstood, you can do this using the functions:
C#:
//Gets the slot where the item actually is.
var realSlotIndex = equipmentItemCollection.GetItemSlotIndex(myItem);
//Get the index where the item could potentially fit if it was added .
var potentialSlotIndex = equipmentItemCollection.GetTargetSlotIndex(myItem);

@Theremy
Somethings a bit off in your code. You are using a forloop to iterate all the items, and then within the forloop you are removing+Adding all items on each iteration.
I think you can get rid of the for loop.
Also, it's always recommended to remove items and then add them. Otherwise you might duplicate some items for no reason.

The error says that your character inventory does not have a ItemSlotCollection with the same ID that you specified in the UI.
1611305361997.png
1611305410883.png
The collection type must be ItemSlotCollection. As you can see here the ID matches because the purpose is the same and the name is blank (ignored) in the ID specified in the UI. The Item Slot Sets must match exactly too.

Note that if you don't need a ItemSlotCollection, but instead just in an Inventory, then you can use an Inventory Grid instead of an ItemSlotCollectionView. It's up to you, whatever best fits your game.
 
Yeah, thanks! now it's gone.
I just realize that I have a hidden equipment UI that I forgot to delete, lol.

As for the for loop, it is for getting all item info in that crafting desk inventory, so that I can add those items to the player inventory and clean up the desk inventory after that. Is there a way to clean an inventory?
 
Great!

For the for loop I meant that you are adding & removing ALL the items multiple times. You only need to add & remove ALL items once, so you do not need the for loop.
 
Top