Hello all! First post, loving the series. I saw a couple episodes back somebody was asking about how to sell items and buy them back, and I have implemented that with a couple simple changes. I wanted to wait until the end to make sure it was going to work with Sam’s code. It’s actually just a change to the GetAvailableConfigs function, and a small tweak to the StockItemConfig class to accommodate.
Also, apologies in advance if my code style looks different, I am not a fan of having the curly braces all spread out, I find it harder to track, just the way I learned I guess
First, we need to add a simple constructor to StockItemConfig:
public StockItemConfig(InventoryItem item){
this.item = item;
this.buyingDiscountPercentage = 0;
this.initialStock = 0;
}
Second, adjust the GetAvailableConfigs function as follows:
private IEnumerable<StockItemConfig> GetAvailableConfigs() {
int shopperLevel = GetShopperLevel();
List<InventoryItem> itemsCovered = new List<InventoryItem>();
foreach (var config in stockConfig) {
if (config.levelToUnlock > shopperLevel) continue;
if(!itemsCovered.Contains(config.item)) itemsCovered.Add(config.item);
yield return config;
}
if(IsBuyingMode()){
foreach(InventoryItem item in stockSold.Keys){
if(itemsCovered.Contains(item)) continue;
yield return new StockItemConfig(item);
}
} else {
Inventory inventory = currentShopper.GetComponent<Inventory>();
if(inventory == null) yield break;
for(int i = 0; i < inventory.GetSize(); i++){
if(inventory.GetNumberInSlot(i) == 0) continue;
if(itemsCovered.Contains(inventory.GetItemInSlot(i))) continue;
yield return new StockItemConfig(inventory.GetItemInSlot(i));
}
}
}