Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to develop the structure of the store?

Discussion in 'Scripting' started by hellpirat, Jul 30, 2015.

  1. hellpirat

    hellpirat

    Joined:
    Jun 12, 2015
    Posts:
    26
    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?

    p.s Sorry for my bad English.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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!)
     
  3. hellpirat

    hellpirat

    Joined:
    Jun 12, 2015
    Posts:
    26
    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?

    My native language is Russian.
     
  4. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Code (CSharp):
    1. public class Item {
    2.     public GameObject itemPrefab;
    3.     public float itemPrice;
    4. }
    5.  
    6.  
    7. public class ShopScript {
    8.       public List<Item > shopItems = new List<Item>();
    9. }


    How does your code look atm ?
     
    hellpirat likes this.
  5. hellpirat

    hellpirat

    Joined:
    Jun 12, 2015
    Posts:
    26
    I think this is a good solution for me. Thanks.
    But how do I interact with them in Unity?
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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!
     
  7. hellpirat

    hellpirat

    Joined:
    Jun 12, 2015
    Posts:
    26
    I have a code
    Code (CSharp):
    1.  
    2. public class ShopScript : MonoBehaviour {
    3.  
    4.     public List<Items> Itemses = new List<Items>();
    5.  
    6. }
    7. public class Items
    8. {
    9.     public GameObject Prefabs;
    10.     public int Price;
    11. }
    12.  
    But I can't see this in inspector

    1.png
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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.
     
    hellpirat likes this.
  9. ande04b

    ande04b

    Joined:
    Aug 29, 2012
    Posts:
    119
    I would make an Item class, like this one:
    http://pastebin.com/8L0y9iaN

    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:
    http://pastebin.com/HbVeX3hX

    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 :)
     
    hellpirat likes this.
  10. hellpirat

    hellpirat

    Joined:
    Jun 12, 2015
    Posts:
    26
    It's fine, but it's too hard for me. I need a simple shop.
     
  11. ande04b

    ande04b

    Joined:
    Aug 29, 2012
    Posts:
    119
    That's the thing, though.

    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" :)