Ohh I see, I understand now.
You helped me plenty! I believe I figured out.
when an item is created I store that info in the struct, along with the config ID from the InventoryItem (I also create a unique ID for the created Item). I simply pass the struct to the shop/inventory. when I need to create a shop item, I check if there is anything in the Dictionary of structs and if there is I create it with the info from that, if not I create a new item and do the random stats, then add it to the struct Dictionary.
Here is my updated code.
public IEnumerable GetAllItems() {
//HAVE A RANDOM CHANCE TO SEE HOW MANY OF THE CONFIG WILL BE CREATED.
int numberOfItems = 0;
//HAVE A CHANCE TO SET THE ITEM QUALITY
int chanceForQualityItem = 0;
//THE HIGHER THE TIER RANK, THE MORE LIKELY IT IS TO CREATE BETTER WEAPONS
//HAVE A BOOL TO SEE IF ANY EPIC/LEGENDARYS HAVE BEEN CREATED
bool createdStrongItem = false;
//IF NOT, DO A FOREACH LOOP OVER THE CONFIG
//DO A CHANCE TO SEE IF THAT CONFIG WILL BE AN EPIC OR LEGENDARY
//IF YES BREAK OUT OF THE LOOP.
//HAVE AT LEAST ONE EPIC OR LEGENDARY ITEM.
ItemQualityType qualityType = ItemQualityType.poor;
if(ItemStructInShop.Count <= 0)
{
foreach (StockItemConfig config in stockConfig)
{
chanceForQualityItem = UnityEngine.Random.Range(0, 101);
numberOfItems = UnityEngine.Random.Range(1, 5);
for (int i = 0; i < numberOfItems; i++)
{
//Randomly creating quality of the item.
if (chanceForQualityItem < 5) { qualityType = ItemQualityType.legendary; createdStrongItem = true; }
if (chanceForQualityItem > 5 && chanceForQualityItem < 25) { qualityType = ItemQualityType.epic; createdStrongItem = true; }
if (chanceForQualityItem > 25 && chanceForQualityItem < 50) { qualityType = ItemQualityType.basic; }
if (chanceForQualityItem > 50 && chanceForQualityItem < 100) { qualityType = ItemQualityType.poor; }
//CHANGE THE PRICE BASED ON TIER RANK AND QUALITY OF ITEM
float originalPrice = config.item.ItemPrice;
ShopItem newItem = new ShopItem(config.item, qualityType, originalPrice, currentShopper.GetComponent<CharacterStats>().GetTierRank(), System.Guid.NewGuid().ToString());
ItemStruct itemStruct = new ItemStruct();
itemStruct = AddItemToStruct(newItem, itemStruct);
ItemStructInShop.Add(itemStruct.GetItemID(), itemStruct);
yield return newItem;
}
//If no high quality item is created
if (!createdStrongItem)
{
//Run a random chance between epic and legendary.
//It will have to return one.
yield return CreaterHigherQuality(createdStrongItem, qualityType, chanceForQualityItem);
}
}
}
else
{
foreach (ItemStruct item in ItemStructInShop.Values)
{
ShopItem newItem = new ShopItem(item);
yield return newItem;
}
}
}
private ShopItem CreaterHigherQuality(bool createdStrongerItem, ItemQualityType qualityType, float chanceForQualityItem)
{
int checkAmount = 4;
int startingChance = 5;
for (int i = 0; i < checkAmount; i++)
{
foreach (StockItemConfig config in stockConfig)
{
if (createdStrongerItem) { break; }
if (chanceForQualityItem < startingChance)
{
createdStrongerItem = true;
qualityType = ItemQualityType.legendary;
float originalPrice = config.item.ItemPrice;
ShopItem newItem = new ShopItem(config.item, qualityType, originalPrice, currentShopper.GetComponent<CharacterStats>().GetTierRank(), System.Guid.NewGuid().ToString());
ItemStruct itemStruct = new ItemStruct();
itemStruct = AddItemToStruct(newItem, itemStruct);
ItemStructInShop.Add(itemStruct.GetItemID(), itemStruct);
return newItem;
}
if (chanceForQualityItem > 5 && chanceForQualityItem < (25 + startingChance))
{
createdStrongerItem = true;
float originalPrice = config.item.ItemPrice;
qualityType = ItemQualityType.epic;
ShopItem newItem = new ShopItem(config.item, qualityType, originalPrice, currentShopper.GetComponent<CharacterStats>().GetTierRank(), System.Guid.NewGuid().ToString());
ItemStruct itemStruct = new ItemStruct();
itemStruct = AddItemToStruct(newItem, itemStruct);
ItemStructInShop.Add(itemStruct.GetItemID(), itemStruct);
return newItem;
}
}
if (i == 2 && !createdStrongerItem) { i = 0; startingChance *= startingChance; }
}
return null;
}
private static ItemStruct AddItemToStruct(ShopItem newItem, ItemStruct itemStruct)
{
itemStruct.SetInventoryItemID(newItem.GetInventoryItemID());
itemStruct.SetItemID(newItem.ItemID);
itemStruct.SetItemStatOne(newItem.ItemStatOne);
itemStruct.SetItemStatTwo(newItem.ItemStatTwo);
itemStruct.SetItemPrice(newItem.ItemPrice);
itemStruct.SetItemQuality(newItem.ItemQualityType);
itemStruct.SetItemCategory(newItem.ItemCategory);
itemStruct.SetItemName(newItem.ItemName);
return itemStruct;
}
When I need to create it as a ShopItem I do the same in shop as the inventory
public IEnumerable GetInventoryItems()
{
foreach (ItemStruct item in ItemsInInventory.Values)
{
ShopItem newItem = new ShopItem(item);
yield return newItem;
}
}
The Shopitem class has 2 ways of calling to it
public ShopItem(InventoryItem inventoryItem,ItemQualityType itemQuality,float originalPrice, float tierRank, string itemID)
{
this.item = inventoryItem;
this.ItemBonus = item.itemBonus;
this.ItemCategory = item.ItemCategory;
this.ItemName = item.ItemName;
this.ItemQualityType = itemQuality;
SetItemStats(tierRank, item.ItemStatOne, item.ItemStatTwo, ItemQualityType);
ItemPrice = originalPrice;
ItemID = itemID;
}
public ShopItem(ItemStruct itemStruct)
{
this.InventoryItemID = itemStruct.GetInventoryItemID();
this.ItemID = itemStruct.GetItemID();
this.ItemName = itemStruct.GetItemName();
this.ItemStatOne = itemStruct.GetItemStatOne();
this.ItemStatTwo = itemStruct.GetItemStatTwo();
this.ItemPrice = itemStruct.GetItemPrice();
this.ItemQualityType = itemStruct.GetItemQuality();
this.ItemCategory = itemStruct.GetItemCategory();
}
Idk if I explained it well enough but so far it is working. I am able to sell, buy and save.
How does it look?