Adding Custom Inventory Items while Application is not playing

Hello there!

I have a bunch of objects placed around the world. I am making an editor function for myself that adds these items into an inventory. The idea behind it is if we are starting a new game, it will populate the world from the load out inventory ("Game Start"). If it is not a new game, it will generate the trees based on the saved inventory of trees. The problem I have is that the ItemObject on each game object is set up properly but the custom attributes are not saved into the inventory. Instead it reverts the attributes to default settings.

C#:
        public void CheckInSelection()
        {
            ItemObject[] placedItems = GetItemObjects(Selection.gameObjects);
            if (placedItems != null)
            {
                if (placedItems.Length > 0)
                {
                    m_Inventory.Initialize(true);
                    ItemCollection startCollection = m_Inventory.GetItemCollection("Game Start");
                    for (int i = 0; i < placedItems.Length; i++)
                    {
                        var item = Opsive.UltimateInventorySystem.Editor.VisualElements.ItemField.CreateItem(placedItems[i].Item.ItemDefinition);
                        var positionAttribute = item.GetAttribute<Attribute<Vector3>>("Position");
                        if (positionAttribute != null)
                        {
                            positionAttribute.SetOverrideValue(placedItems[i].transform.position);
                        }
                        var rotationAttribute = item.GetAttribute<Attribute<Quaternion>>("Rotation");
                        if (rotationAttribute != null)
                        {
                            rotationAttribute.SetOverrideValue(placedItems[i].transform.rotation);
                        }
                        placedItems[i].SetItem(item);
                        EditorUtility.SetDirty(placedItems[i]);
                        if (startCollection.DefaultLoadout == null)
                        {
                            startCollection.DefaultLoadout = new ItemAmounts();
                        }
                        ItemAmount newItem = new ItemAmount(item, 1);
                        startCollection.DefaultLoadout.Add(newItem);
                    }
                    m_Inventory.Serialize();
                    EditorUtility.SetDirty(m_Inventory);
                }
            }
        }
 
I should also mention that it's been some time since I have updated UIS - currently on 1.2.8. If you think that updating will help I can give that a shot.
 
That's odd, your code seems perfectly fine... so I'm really not sure why it wouldn't work.
Out of curiousity if you add the item to the collection before setting it as the ItemObject, does it make a difference? (It shouldn't)
I'd recommend updating since there has been a ton of improvements since 1.2.8. I don't remember this being an issue but we never know it might have been fixed at some point.
But make sure to make a backup in case something goes wrong during the update.
You could also try to import the latest in a new project and try out your script before deciding to make the jump in your main project
 
Sorry for the delay! I was able to get the project updated with the latest UIS, so that's nice to be up to date.

Unfortunately the update didn't fix the issue. I also attempted to change the Set Item to happen after and it didn't have any impact. Perhaps the code that happens in this area doesn't take into account any custom attributes?
C#:
ItemAmount newItem = new ItemAmount(item, 1);
startCollection.DefaultLoadout.Add(newItem);
 
I was also wondering if you can think of any reason the EnableGameplayInput is not getting called properly after upgrading from 1.2.8 to 1.2.16?

When opening my menus, previously the rewired input script on the character would get disabled/enabled. After the upgrade it no longer works. I have been debugging for a while but I thought you might already know what could be the cause?
 
I was also wondering if you can think of any reason the EnableGameplayInput is not getting called properly after upgrading from 1.2.8 to 1.2.16?

When opening my menus, previously the rewired input script on the character would get disabled/enabled. After the upgrade it no longer works. I have been debugging for a while but I thought you might already know what could be the cause?
Ok I found the issue for this one. The problem in this case was in the Awake function of PlayerInput:
C#:
            if (GetComponent<Shared.Character.ICharacter>() != null) {
                RegisterEvents(gameObject);
            }
My character is a custom character, not an ICharacter, but was still relying on those events being fired, specifically the EnableGameplayInput one. I tweaked my custom player input script to ignore the if statement and register the events regardless.

My initial concern I am still unable to figure out though with adding the inventory items during edit time.
 
If you are able to give me a very simple test script that fails to save the custom atttribute (or even better a small project) with soem setps to reproduce the issue, that'll help me identify what is going on.
 
I have attached something that can be replicated using just the basic Demo Scene.

Click on the PlayerCharacter, and add the "AddToInventory" script to the character. This should populate with an array of hints on how to replicate it. Let me know if you have any questions!
 

Attachments

  • AddToInventory.unitypackage
    1.8 KB · Views: 3
Top