Discount is not working for me

I have looked into it some and it’s like the discount is just not being applied at all I have tryed going through the shoper class with the docs and no luck still the same outcome. also the price within the shop config is not do8ing anything do we even need this since each item has it’s own price in it.

Hmmm… looking at the course final code, we’ve left out the price in the shop config, because you’re quite right, we really don’t need it. You’re right in that we’re no longer in need of the price from the shop config.

The question is why you’re not getting the discount. Post your GetPrices() method from your shop.cs, and we’ll take a look.

Here you go.

        private float GetPrice(StockItemConfig config)
        {
            if (isBuyingMode)
            {
                return config.item.GetPrice() * (1 - config.buyingDiscountPersentage / 100);
            }
            return config.item.GetPrice() * (sellingPersentage / 100);
        }

That code is correct, so let’s see if it’s working properly…
Your Shop’s Stock Config should look something like this:
image
The Wooden dagger’s actual price is 180, and the wooden shield is 360
Now with these settings, when I play the game, my Shop looks like this:


Now, we’ll swap the discounts, so the shield gets the discount and the sword doesn’t
image
Now our shop should look like this:

If this is not happening on your end, paste in some screen shots using a similar methodology, and we’ll investigate further.

The discount price is right like it is displaying the right things but when I switch to sell an item it uses the same discount or price that was used in the buying of the item so I just get the same money back.

That is weird it’s like the discount is not even being applied at all even If I do it when buying an item.
but it works in the UI and displays the right amount.

also I went through the last few parts of the tutorial at least the shops part and now my code looks like this

 private Dictionary<InventoryItem, float> GetPrices()
        {
            Dictionary<InventoryItem, float> prices = new Dictionary<InventoryItem, float>();

			foreach (var config in GetAvailableConfigs())
			{
				if (isBuyingMode)
				{
                    if (!prices.ContainsKey(config.item))
                    {
                        prices[config.item] = config.item.GetPrice();
                    }
                    prices[config.item] *= (1 - config.buyingDiscountPercentage / 100);
                }
				else
				{
                    prices[config.item] = config.item.GetPrice() * (sellingPercentage / 100);
                }
            }
			return prices;
        }

no change on how the discounts work at all

Found the bug and fixed it ugg in confirm transaction

        public void ConfirmTransaction()
        {
            Inventory shopperInventory = currentShopper.GetComponent<Inventory>();
            MoneyPurse shopperMoneyPurse = currentShopper.GetComponent<MoneyPurse>();

            if (shopperInventory == null || shopperMoneyPurse == null) return;

            //Transfer to or from the invantory
            foreach (ShopItem shopItem in GetAllItems())
            {
                InventoryItem item = shopItem.GetInventoryItem();
                int quantity = shopItem.GetQuantityInTransaction();
                float price = shopItem.GetPrice(); //This is the fix I had Item instead of shopItem
                //float price = item.GetPrice();

                for (int i = 0; i < quantity; i++)
                {
					if (isBuyingMode)
					{
						BuyItem(shopperInventory, shopperMoneyPurse, item, price);
					}
					else
					{
                        SellItem(shopperInventory, shopperMoneyPurse, item, price);
                    }
                }
            }

Well done in rooting it out. There’s a lot going on in Shopper. I’ll admit, I didn’t think of the ConfirmTransaction(), as usually this is an error in the GetPrice() method.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms