Hi, guys.
I want create a store in my game, but I don’t know how right create structure in my scripts.
I have a lot of items and want create something like two arrays with type - Object(my items) and int(price) but I think is not the good solution.
I tried using the dictionary too, but I could not see in my inspector.
I think there is a more convenient solution to this problem. Can you help me find this solution?
Why not put the price on the items themselves? Or at least a base “value” property, and then any particular store can multiply this by some “markup” property to get the final price.
And your English seems fine to me… but out of curiosity, what’s your native language? (If it’s Japanese, I may try to practice with you!)
You mean to create another script with variables and attached it to every object in the store? But how can I get the price and the subject of another script?
public class Item {
public GameObject itemPrefab;
public float itemPrice;
}
public class ShopScript {
public List<Item > shopItems = new List<Item>();
}
ShopScript there should have been derived from MonoBehaviour (i.e. public class ShopScript : MonoBehaviour).
Then you attach ShopScript to some game object in the scene, and you can set up all the Items (with their prices) right there in the inspector.
It’s still not clear to me why the extra layer for prices is necessary… every item could know its own price. This approach seems to me more flexible than you probably need. But if it’s a good solution for you, then go for it!
public class ShopScript : MonoBehaviour {
public List<Items> Itemses = new List<Items>();
}
public class Items
{
public GameObject Prefabs;
public int Price;
}
Try adding [System.Serializable] right above “public class Items”. This tells the system that this particular class is “serializable” (convertable to/from text or binary data) and therefore OK to use in the inspector.
This is documented here, though contrary to the docs, I have never found it necessary to derive from System.Object.
You can then add whatever stats to items you make, and also make subclasses that have slightly different stats, like an Armor class, which derives from this Item class. Once done, I would make an Item Database, like this one:
You could then have the Vendor have a list of items, maybe even create loot tables, so that a vendor can only sell a specific set of items, perhaps randomly generate the list so that the vendor doesn’t always have the same items to sell
To have a shop, you first need to have items. To my knowledge, an Item class is the simplest and most efficient way of having this. And it’s actually not too complicated, once you get the “aha-moment”