List<InventoryItem> itemLibrary = new List<InventoryItem>();
After serializing this list, I created two different items and put them into this list: For readability sake → I have one asset that’s just the regular ‘EquipableItem’ and the other ‘StatsEquipableItem’ but the items themselves are named as ‘Base EquipableItem’ & ‘Base StatsEquipableItem’.
These items are not defined at all, all configurable properties are empty besides the generic pick up spawner on each item and their ItemID’s.
Imagine with me for a moment: This gameObject will roll a dice via code when called. When the dice is rolled it will elect one of these items to proceed forward with. With this item my script will select a random name & description (appropriately), whether it’s stackable based on certain data and where it could be slotted if at all for the equipment or action bars and all other relevant things by RNG.
However despite that I got all of this working till this point…
I seem to have run into an issue. What I’d like to do is be able to instantiate a new instance of that ‘Base EquipableItem’ or ‘Base StatsEquipableItem’ and have them be entirely different from the previously created item that occured.
I want to have a ‘Base’ item for each individually different item that could be obtained in my game, and then have the ‘CreateRandomItem()’ method use that Base Item and then fill in all the blanks without associating itself with the Base item at the end… so it can be re-usable to continuously create new items from the base item.
Some code for context.
public void CreateNewItem()
{
int randomIndex = UnityEngine.Random.Range(0, itemLibrary.Count);
CreateRandomItem(itemLibrary[randomIndex]);
}
InventoryItem CreateRandomItem(InventoryItem createItem)
{
if (createItem is StatsEquipableItem)
{
StatsEquipableItem statItem = createItem as StatsEquipableItem;
return SetUpEquipableModifierItem(statItem);
}
else if (createItem is WeaponConfig)
{
WeaponConfig weapon = createItem as WeaponConfig;
return SetUpWeaponItem(weapon);
}
else if (createItem is EquipableItem)
{
EquipableItem equipItem = createItem as EquipableItem;
return SetUpEquipableItem(equipItem);
}
return createItem;
}
WeaponConfig SetUpWeaponItem(WeaponConfig weapon)
{
weapon.SetItemID(Guid.NewGuid().ToString());
SetUpWeaponLocations(weapon, out var location, out var category);
weapon.SetDisplayName("Weapon");
weapon.SetDescription("This weapon is a sword.");
SpawnGenericPickup(weapon.SpawnPickup(Vector3.zero, 1));
return weapon;
}
EquipableItem SetUpEquipableItem(EquipableItem equipableItem)
{
equipableItem.SetItemID(Guid.NewGuid().ToString());
equipableItem.SetDisplayName("No Stats Equip");
equipableItem.SetDescription("Here you have an example of no stats equipped.");
SpawnGenericPickup(equipableItem.SpawnPickup(Vector3.zero, 1));
return equipableItem;
}
StatsEquipableItem SetUpEquipableModifierItem(StatsEquipableItem statItem)
{
GameObject player = GameObject.FindGameObjectWithTag("Player");
statItem.SetItemID(Guid.NewGuid().ToString());
statItem.AddRandomStat(player);
statItem.SetDisplayName($"Modifier Item");
SpawnGenericPickup(statItem.SpawnPickup(Vector3.zero, 1));
return statItem;
}
When I learnt that the InventoryItem was an abstract class I removed the abstract and tried doing something like…
ScriptableObject newItem = Instantiate(statItem); // This was inside the SetUpEquipableModifierItem method
I tried doing something like the above here but it still created an instance that was linked with the base item, so any changes to the base also affected the instantiated item. This is also occuring with the set up I have in the code you’re reviewing now…