Attempting to Spawn UIS at Runtime with UCC and UMA

Eyeshock

Member
Hello!

Context: I've got a pretty good hang on workable version of UCC and UIS running independently of the UMA Spawning. So I can test all the UIS DB and UCC basics on non-runtime spawned chars without any issues.

Per our discussion in the forums, I'm trying to spawn the UIS with a runtime Spawner using UCC. It's all a bit clunky.

I got fairly close, and I'm still trying the attached script.

Issues I encountered:
1) Had to comment out the Inventory spawning part; I have it already attached to the UMA Spawner because spawning via script leaves the Loadouts inaccessible (Null error when you click on them)
2) The Set Manager has 1 Slot per Set, doesn't add any slots from Loadout
3) Null errors from Bridge due to Awake script checking for No Null on Categories (this is mostly annoying though); I don't think it affects anything.

Most of the other issues seem related to UCC and I'll address them in another forum.

Mostly looking for some advice or glaring issues. I'll tackle this again tomorrow but I'm worn out today.

Here's a video; spawning works, I get a saber, but lots of issues:

Here's the function I'm using to convert the UCC to UIS (using the UCC In the initial setup from UMA Integration). Note I was forced to comment out the part that adds <Inventory>. It seems to add the categories fine, but they would throw null errors.

C#:
private void SetupCharacter()
        {
            GameObject newChar = this.gameObject;

            // Ensure the database is valid.
            var inventorySystemManager = Object.FindObjectOfType<Opsive.UltimateInventorySystem.Core.InventorySystemManager>();
            var database = inventorySystemManager != null ? inventorySystemManager.Database :
                    Opsive.UltimateInventorySystem.Editor.Managers.MainManagerWindow.InventorySystemDatabase;
            if (database == null)
            {
                Debug.LogError("Error: Unable to find the database. Ensure the database has been created and is assigned to the Inventory System Manager or " +
                               "database field of the Inventory Manager.");
                return;
            }

            // The database needs to be in the correct format.
            database.Initialize(false);
            Opsive.UltimateInventorySystem.Core.ItemCategory consumableItemCategory = null, equippableItemCategory = null, multiItemCategory = null;
            if (database.ItemCategories != null)
            {
                for (int i = 0; i < database.ItemCategories.Length; ++i)
                {
                    if (database.ItemCategories[i].name == c_ConsumableItemCategoryName)
                    {
                        consumableItemCategory = database.ItemCategories[i];
                    }
                    else if (database.ItemCategories[i].name == c_EquippableItemCategoryName)
                    {
                        equippableItemCategory = database.ItemCategories[i];
                    }
                    else if (database.ItemCategories[i].name == c_MultiItemItemCategoryName)
                    {
                        multiItemCategory = database.ItemCategories[i];
                    }
                }
            }
            if (consumableItemCategory == null || equippableItemCategory == null)
            {
                Debug.LogError("Error: Unable to find the Consumable Item Category or the Equippable Item Category. Ensure the database is setup from the " +
                                "Ultimate Character Controller empty database.");
                return;
            }

            // *** ENSURE THESE ELEMENTS ARE NEVER ADDED TO BEGIN WITH
            var characterInventory = newChar.GetComponent<Opsive.UltimateCharacterController.Inventory.Inventory>();
            if (characterInventory != null)
            {
                Object.DestroyImmediate(characterInventory, true);
            }
            var itemSetManager = newChar.GetComponent<Opsive.UltimateCharacterController.Inventory.ItemSetManager>();
            if (itemSetManager != null)
            {
                Object.DestroyImmediate(itemSetManager, true);
            }

            
            // Add the default ItemCollections to the character.
            /*
            var itemCollections = new List<Opsive.UltimateInventorySystem.Core.InventoryCollections.ItemCollection>();
            var itemCollection = new Opsive.UltimateInventorySystem.Core.InventoryCollections.ItemCollection();
            itemCollection.SetName("Default");
            InspectorUtility.SetFieldValue(itemCollection, "m_Purpose", ItemCollectionPurpose.Main);
            itemCollections.Add(itemCollection);
            itemCollection = new Opsive.UltimateInventorySystem.Core.InventoryCollections.ItemCollection();
            itemCollection.SetName("Equippable");
            InspectorUtility.SetFieldValue(itemCollection, "m_Purpose", ItemCollectionPurpose.Secondary);
            itemCollections.Add(itemCollection);
            
            var itemSlotCollection =
                new Opsive.UltimateInventorySystem.Core.InventoryCollections.ItemSlotCollection(GetItemSlotSet(database, equippableItemCategory));
            itemSlotCollection.SetName("Equipped");
            InspectorUtility.SetFieldValue(itemSlotCollection, "m_Purpose", ItemCollectionPurpose.Equipped);
            itemCollections.Add(itemSlotCollection);
            

            itemCollection = new Opsive.UltimateInventorySystem.Core.InventoryCollections.ItemCollection();
            itemCollection.SetName("Loadout");
            InspectorUtility.SetFieldValue(itemCollection, "m_Purpose", ItemCollectionPurpose.Loadout);
            itemCollections.Add(itemCollection);
            */
            // Adding Inventory
            //var inventory = newChar.AddComponent<Opsive.UltimateInventorySystem.Core.InventoryCollections.Inventory>();
            //InspectorUtility.SetFieldValue(inventory, "m_ItemCollections", itemCollections);
            //inventory.Serialize();


            // Add the Ultimate Inventory System integration components.
            var bridge = newChar.AddComponent<UltimateInventorySystemBridge>();
            InspectorUtility.SetFieldValue(bridge, "m_EquippableCategory", equippableItemCategory);
            InspectorUtility.SetFieldValue(bridge, "m_ConsumableCategory", consumableItemCategory);
            if (multiItemCategory != null)
            {
                InspectorUtility.SetFieldValue(bridge, "m_MultiItemCategory", multiItemCategory);
            }
            EditorUtility.SetDirty(bridge);
            
            newChar.AddComponent<InventorySystemItemSetManager>();


            // Add the additional Inventory System components for characters
            newChar.AddComponent<InventoryIdentifier>();
            newChar.AddComponent<ItemUser>();
            newChar.AddComponent<CurrencyOwner>();
            newChar.AddComponent<InventoryInteractor>();

            /*btw the character set up does not add saving components so you'll need to add the bridge and currency owner saver too if that's what you want***/
 

        }


        private static ItemSlotSet GetItemSlotSet(InventorySystemDatabase database, Opsive.UltimateInventorySystem.Core.ItemCategory equippableCategory)
        {
            var directory = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(database));
            var name = database.name + "EquippedSlots";

            //var itemSlotSet = ScriptableObject.CreateInstance<ItemSlotSet>();
            var itemSlotSet = (ItemSlotSet)AssetDatabase.LoadAssetAtPath(directory + "/" + name + ".asset", typeof(ItemSlotSet));
            EditorUtility.SetDirty(itemSlotSet);

            return itemSlotSet;
        }
 
I'm afraid that without trying it out myself I can't really help you diagnose the issues. Currently my focus is on stability of UIS so it may take a while before I can seriously look into an UMA integration (or runtime adding of characters with inventories). We have plans of refactoring the UCC/UIS integration some time next year once I am happy with UIS. We'll probably look into the UMA integration around that time too or just after.

I'm sorry I can't help you more than that
 
Top