Requiring inventory for interact ability

HU$H

New member
Is there a way to require an item in the characters inventory in order to interact with an interactable item? I tried making a new interactable script that requires the item in when running CanInteract(), but it seems the interact ability requires the class named Interactable to work which makes my script useless. Should I just copy the interact ability script and have it use my custom UnlockableInteractable script or is there an integrated way to do this?
 
Well I created a new interact ability that uses my custom UnlockableInteractable script but for some reason it cant find my item in the inventory. I do see it in my inventory as seen bellow:

Screen Shot 2021-06-27 at 7.41.56 PM.png

But when I grab my item identifier and try to get the item using InventoryBase.GetItem(id, slot) it says:
"IndexOutOfRangeException: Index was outside the bounds of the array."

Screen Shot 2021-06-27 at 7.43.54 PM.png

Any idea why this is happening? Is it looking somewhere else outside of whats shown in the inspector of the inventory script?
 
Is m_RequiredItems an array of Items? If so, you can get the ItemIdentifier from an Item directly: IItemIdentifier itemID = m_RequiredItems[i].ItemIdentifier The error is probably because you're passing an invalid ItemIdentifier into GetItem, so try that.
 
Oh my bad it’s an array of ItemType’s, and then I use CreateItemIdentifier() on each required item type to get the item Identifier. Not sure that’s the best way, I couldn’t find any other way to get the ID from the ItemType class.

From there I’m grabbing the item from the inventory with GetItem, and it looks like I’m redundantly checking if it exists in the inventory with HasItem (was fishing for the right method I’ll clean that up later).

the reason I’m doing it this way is because the only asset I can drag into my inspector field for m_requiredItems is an ItemType from my item collection. Unless I’m doing something wrong? The only real info I could find on non-equipable items didn’t get me far, so I just added an item type and then created my own script for adding it to the inventory. When my chosen gameobject is collided with, i update InventoryBase using AddItem.
 
Last edited:
CreateItemIdentifier should be fine in this case (ItemType is actually just an IItemIdentifier anyway - CreateItemIdentifier returns itself, so you could just cast the ItemType to an IItemIdentifier for the same result).

I just checked and you actually can't pass a slot ID of -1 into GetItem. That'll be the source of your error. So you'll need to use whatever slot ID is relevant for the item you're checking (or if it's an item that has more than 1 possible slot, you'll need to check all of them separately).
 
Ok i guess im having trouble underrstanding how to add a non-equipable item to the inventory. Isnt the item slot -1 when its non-equipable? If so how would I then check if the user has an item that is non-equipable. The item slot is the left or right hand correct?
 
The inventory component in the character controller is pretty basic so it only handles equipped items. If you are looking for something for non equippable items the Ultimate Inventory System works well for that.
 
Oh okay that helps a lot! I’ll just build an inventory system in that case. Didn’t wanna double up my work if I didn’t have to tho. Thanks so much!
 
Top