Adding Equipped Items from start of game.

Niroan

Member
Hello

Im trying to get this working with equipped items.
I managed to get the list.

But when i try it add the items to equipped list its like they are not removed from normal inventory?
They stay there and now user have 2 times normal items.


C#:
  public void AddItemsToEquipped() {

        string equippedItems = "";
        equippedItems = GTD_GameData.PlayFabEquippedItems;
        var ArrEI = equippedItems.Split(","[0]);

        for (int i = 0; i < ArrEI.Length; i++)
        {
            Debug.Log("i: " + i + " testCount: " + ArrEI.Length);
            Debug.Log("Equipped: " + ArrEI[i].ToString());

            string ItemName = ArrEI[i].ToString();
            var myItemDefinition = InventorySystemManager.GetItemDefinition(ItemName);
            var myItem = InventorySystemManager.CreateItem(myItemDefinition);
            int Amount;
            Amount = 1;

            var myItemInfo = new ItemInfo((Amount, myItem));
            inventory.GetItemCollection("Backpack").RemoveItem(myItemInfo);
            inventory.GetItemCollection("Equipped").AddItem(myItemInfo);
        }

    }

this is the code im using to update items as soon as Mainmenu opens up.
GTD_GameData.PlayFabEquippedItems is just a string of items that the users has from Cloud containing this: "Knight Gloves,Knight Helmet,Knight Boots,Knight Armor,Necklace of Destruction"

So to replicate you can use:

GTD_GameData.PlayFabEquippedItems = "Knight Gloves,Knight Helmet,Knight Boots,Knight Armor,Necklace of Destruction"
Its only removing the first item. "Knight Gloves" is being removed. But not rest after that.
 
Hi @Niroan

Even though at first glance your code might look ok, removing the correct item is actually a bit more involved than that.

When you have Unique items you must give the exact item (with the same id and reference) as the one you want to remove.
The reason for that is if you have two swords in your inventory, one with a +5 upgrade and the other with no upgrade. You want to have control on which sword gets removed.

I understand that might cause a bit of confusion in your use case because all your items are immutable.
I might look into changing the functions to remove items, such that the default behaviour is to remove the item if their definition match... but I'm not quite sure that's the correct approach so I'll continue thinking about it some more

So the way you actually want to remove the item is like so:

C#:
//Before version 1.0.7
    public void AddItemsToEquipped() {

        string equippedItems = "Bomb,Potion,Wood,Surprise Box";
        var ArrEI = equippedItems.Split(","[0]);

        for (int i = 0; i < ArrEI.Length; i++)
        {
            Debug.Log("i: " + i + " testCount: " + ArrEI.Length);
            
            string ItemName = ArrEI[i];
            int Amount = 1;
            
            var myItemDefinition = InventorySystemManager.GetItemDefinition(ItemName);
            
            //Find the item to remove by searching the item Definition.
            var itemToRemoveInfo = m_Inventory.GetItemCollection("Backpack").GetItemInfo(myItemDefinition);
            if (itemToRemoveInfo.HasValue) {
                // Remove 1 of the item info
                m_Inventory.GetItemCollection("Backpack").RemoveItem((Amount,itemToRemoveInfo.Value));
            }

            var myItem = InventorySystemManager.CreateItem(myItemDefinition);

            var itemInfoToAdd = new ItemInfo((Amount, myItem));
            m_Inventory.GetItemCollection("Equipped").AddItem(itemInfoToAdd);
        }

    }
    
    //If you have the latest version 1.0.7 you can do this which is much simpler.
    public void AddItemsToEquippedNewestVersion() {

        string equippedItems = "Bomb,Potion,Wood,Surprise Box";
        var equippedNameArray = equippedItems.Split(","[0]);

        for (int i = 0; i < equippedNameArray.Length; i++)
        {
            string itemName = equippedNameArray[i];
            int amount = 1;

            m_Inventory.GetItemCollection("Backpack").RemoveItem(itemName, amount);
            m_Inventory.GetItemCollection("Equipped").AddItem(itemName, amount);
        }

    }

If you are using 1.0.7 it's super easy because you can add and remove items by name.
Before that you first need to get the item in collection using the item definition such that you can remove the correct one.

I hope that helps


This might not make sense
 
Code:
/ Retrieves an attack attribute value by getting its attribute.
var attackAttribute = item.GetAttribute<Attribute<int>>("Attack");
if (attackAttribute != null) {
    // The real attack value.
    var myAttack = attackAttribute.GetValue();
    // The override attack value.
    var myAttackOverride = attackAttribute.OverrideValue;
    // The inherited attack value.
    var myAttackInherited = attackAttribute.GetInheritedValue();
}

// Retrieves an icon attribute value.
if (item.TryGetAttributeValue("Icon", out Sprite myIcon)) {
    // myIcon exists.
}
 
I managed to get it working :)

Last thing i need to know is where the action for equipped items is called?
When i click the action button "Equip" and "Unequip" i want a script to run, cause i need to update the cloud with new items :)

I got everything else working now, thanks for the great assistance
 
So in short, when i equip item i want to notified. and when i Unequip i want to be notificed.
I can setup EventHandler now so i just need to know how and where its setup.

I use the ActionEquip
 
In UIS there's no specifc ItemAction for equiping because there are many different ways to equip items (For example in the UCC integration we have a custom equip item action).
Instead to "equip" items in the UIS demo we simply move them to the Equipped item collection. We can do so with the MoveToCollectionItemAction.
Your inventory grid should be referencing a CategoryItemActionSet, that has the reference to a Category Item Actions object.
One of them should look like this (I'm on a dev version so it may look a bit different):
1602487304204.png

You can have a look at the MoveToCollectionItemAction.cs script to see how that works.
If you wish you can create your own item action to replace the equip item action.

Another solution to know if your item was equipped is to listen to the "EventNames.c_Inventory_OnAdd_ItemInfo_ItemStack" event and check that the itemStack has the equipped itemCollection.
 
Top