I'm learning to create my own inventory and equipment slots where I can equip different weapons based on what is in the "gun" slot. Currently I can get the item added to the opsive inventory system. It will show the item and quantity at runtime in the inspector after I "equip" the item to the slot. However I can't actually get the item to be equipped onto the player. Below are the two scripts that I created and an error that the equipItem method is occuring.
I'm going to continue trouble shooting this and reading the Opsive documentation in the meantime. However, any help is appreciated. ( I may be taking a very wrong approach but I don't want to edit the Opsive scripts.)
I get that the error is telling me the item trying to be equipped isn't actually in the inventory, but as said about, I can visibly see it in the inspector of the player after it is equipped to the slot, just can't get the player to wield it.
I'm going to continue trouble shooting this and reading the Opsive documentation in the meantime. However, any help is appreciated. ( I may be taking a very wrong approach but I don't want to edit the Opsive scripts.)
I get that the error is telling me the item trying to be equipped isn't actually in the inventory, but as said about, I can visibly see it in the inspector of the player after it is equipped to the slot, just can't get the player to wield it.
C#:
using System.Collections.Generic;
using UnityEngine;
using Opsive.Shared.Game;
using Opsive.Shared.StateSystem;
using Opsive.UltimateCharacterController.Inventory;
using Opsive.UltimateCharacterController.Objects.CharacterAssist;
public enum ItemType
{
Default,
Gun,
BaseMod,
BarrelMod,
ModMod,
ScopeMod,
SocketOne,
SocketTwo,
SocketThree,
SocketFour
}
[CreateAssetMenu(fileName = "NewItem", menuName = "Inventory/Item")]
public class BaseItem : ScriptableObject
{
public int itemID;
public string itemName;
public Sprite itemIcon;
public int quantity;
public ItemIdentifierAmount[] m_ItemDefinitionAmounts;
public List<ItemModifier> modifiers = new List<ItemModifier>();
}
[System.Serializable]
public class Item
{
public BaseItem baseItem;
public int quantity;
public ItemIdentifierAmount[] m_ItemDefinitionAmounts;
public List<ItemModifier> modifiers;
public Item(BaseItem baseItem)
{
this.baseItem = baseItem;
this.quantity = baseItem.quantity;
this.m_ItemDefinitionAmounts = baseItem.m_ItemDefinitionAmounts;
this.modifiers = new List<ItemModifier>();
foreach (var modifier in baseItem.modifiers)
{
this.modifiers.Add(new ItemModifier
{
itemType = modifier.itemType,
damage = modifier.damage,
durability = modifier.durability,
magicPower = modifier.magicPower
});
}
}
}
[System.Serializable]
public class ItemModifier
{
public ItemType itemType;
public float damage;
public float durability;
public float magicPower;
}
C#:
using System.Collections.Generic;
using UnityEngine;
using Opsive.UltimateCharacterController.Inventory;
using Opsive.UltimateCharacterController.Items;
public class Equipment : MonoBehaviour
{
public Backpack backpack;
public Item gun;
public Item baseMod;
public Item barrelMod;
public Item modMod;
public Item scopeMod;
public Item socketOne;
public Item socketTwo;
public Item socketThree;
public Item socketFour;
public void SetItemInSlot(Item newItem, ItemType type)
{
Item oldItem = null;
// Determine the slot to update based on the EquipmentType.
switch (type)
{
case ItemType.Gun:
oldItem = gun;
gun = newItem;
TriggerOpsiveItemPickup(newItem);
break;
case ItemType.BaseMod:
oldItem = baseMod;
baseMod = newItem;
break;
case ItemType.BarrelMod:
oldItem = barrelMod;
barrelMod = newItem;
break;
case ItemType.ModMod:
oldItem = modMod;
modMod = newItem;
break;
case ItemType.ScopeMod:
oldItem = scopeMod;
scopeMod = newItem;
break;
case ItemType.SocketOne:
oldItem = socketOne;
socketOne = newItem;
break;
case ItemType.SocketTwo:
oldItem = socketTwo;
socketTwo = newItem;
break;
case ItemType.SocketThree:
oldItem = socketThree;
socketThree = newItem;
break;
case ItemType.SocketFour:
oldItem = socketFour;
socketFour = newItem;
break;
// Add additional cases as necessary.
}
}
[Tooltip("Reference to the character's Opsive Inventory component.")]
public InventoryBase opsiveInventory; // Assign this in the Inspector
// Method to equip an item in the Opsive Inventory
private void TriggerOpsiveItemPickup(Item itemToEquip)
{
if (itemToEquip != null && itemToEquip.baseItem != null && opsiveInventory != null)
{
foreach (var itemDefinitionAmount in itemToEquip.baseItem.m_ItemDefinitionAmounts)
{
if (itemDefinitionAmount.ItemIdentifier != null)
{
var wasPickedUp = opsiveInventory.PickupItem(itemDefinitionAmount.ItemIdentifier, itemDefinitionAmount.Amount, 1, true, false);
opsiveInventory.EquipItem(itemDefinitionAmount.ItemIdentifier, DetermineSlotForItem(itemToEquip.baseItem), true);
}
}
}
}
// which slot
private int DetermineSlotForItem(BaseItem baseItem)
{
return 0; // Right hand
}
public void RemoveItemInSlot(ItemType type)
{
// Determine the slot to update based on the EquipmentType.
switch (type)
{
case ItemType.Gun:
gun = null;
break;
case ItemType.BaseMod:
baseMod = null;
break;
case ItemType.BarrelMod:
barrelMod = null;
break;
case ItemType.ModMod:
modMod = null;
break;
case ItemType.ScopeMod:
scopeMod = null;
break;
case ItemType.SocketOne:
socketOne = null;
break;
case ItemType.SocketTwo:
socketTwo = null;
break;
case ItemType.SocketThree:
socketThree = null;
break;
case ItemType.SocketFour:
socketFour = null;
break;
}
}
public float CalculateTotalDamageModifier()
{
float totalDamageModifier = 0f;
// List of all items for easier iteration
List<Item> equippedItems = new List<Item> { gun, baseMod, barrelMod, modMod, scopeMod, socketOne, socketTwo, socketThree, socketFour };
foreach (var item in equippedItems)
{
if (item != null && item.modifiers != null)
{
foreach (var modifier in item.modifiers)
{
totalDamageModifier += modifier.damage; // Add up the damage values
}
}
}
return totalDamageModifier;
}
}
Code:
Error: Unable to equip item with ItemIdentifier Pistol: the itemIdentifier hasn't been added to the inventory.
UnityEngine.Debug:LogError (object)
Opsive.UltimateCharacterController.Inventory.Inventory:EquipItemInternal (Opsive.Shared.Inventory.IItemIdentifier,int) (at Assets/Opsive/UltimateCharacterController/Scripts/Inventory/Inventory.cs:228)
Opsive.UltimateCharacterController.Inventory.InventoryBase:EquipItem (Opsive.Shared.Inventory.IItemIdentifier,int,bool) (at Assets/Opsive/UltimateCharacterController/Scripts/Inventory/InventoryBase.cs:397)
Equipment:TriggerOpsiveItemPickup (Item) (at Assets/Custom/Backpack/Equipment.cs:81)
Equipment:SetItemInSlot (Item,ItemType) (at Assets/Custom/Backpack/Equipment.cs:29)
EquipmentSlotUI:OnDrop (UnityEngine.EventSystems.PointerEventData) (at Assets/Custom/Backpack/EquipmentSlotUI.cs:91)
UnityEngine.EventSystems.EventSystem:Update () (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:530)