Shop Listing - Item Conditions (level,...)π§¬
Like in skyrim, you don’t go to a shop and have the top rare weapons available until you reach a certain level. I wanted all the stores to have items that are good for all the levels, instead of having a store with only rare items and the old town with dirty old things.
So with this, you still need to set the items of the store BUT you can set the condition for it, including min level of listing for items.
WHY USE CONDITIONS INSTEAD OF A LEVEL PROPERTY? I wanted to allow the maximum possibility of evolution, so now you can set specific things like missions and stuff that are required for the item!
Import
First import the the new condition IsPlayerLevel.cs to “Conditional->Conditions”.
Changes
In Item.cs, add the following:
[Header("Shop")]
[SerializeField] private int m_price = 50; // i changed this from it's initial place, it just makes more sence
[SerializeField] private ICondition m_shopListingCondition = new IsPlayerLevel();
public ICondition shopListingCondition => m_shopListingCondition ;
In Shop.cs add:
public Item[] GetItems()
{
if (items == null)
return System.Array.Empty<Item>();
int playerLevel = GameManager.Player.level;
return System.Array.FindAll(items, item => item.shopListingCondition.Evaluate());
}
Change:
//public Item[] items = null; //to:
[SerializeField] protected Item[] items = null;
Finally change in UIShop.cs the method FillSlots() to the following:
private void FillSlots() {
Item[] items = m_shop.GetItems();
int itemCount = items.Length;
m_slots = new UIShopEntry[itemCount];
for (int i = 0; i < itemCount; ++i)
{
Item item = items[i];
GameObject itemSlot = Instantiate(m_shopEntryPrefab, m_itemSlotsRoot.transform);
UIShopEntry inventoryBagSlot = itemSlot.GetComponent<UIShopEntry>();
if (inventoryBagSlot)
{
inventoryBagSlot.Initialize(item);
m_slots[i] = inventoryBagSlot;
}
}
}
How to use?
Just go to your items and you should have a property to set the condition. By default i’ve set the level condition, and it’s set to 0, so you can just leave it be and it will not take any effect.


π¬ Comments (3)
Want to continue the conversation?