Search Unity

MarketSystem design approach?!

Discussion in 'Scripting' started by CxydaIO, Apr 17, 2015.

  1. CxydaIO

    CxydaIO

    Joined:
    May 12, 2014
    Posts:
    61
    Hi guys!
    i've a question (Thanks captain obvious).... i want to realize a market system in my game. A system which manages buy and sell prizes of (later maybe) ~200 different commodities but since i never did this before i want your opinion on how to approach this?!

    i'm currently have two approaches ...
    first to store the buy/sell prizes in the Commodity class itself and then cycle every tick through all commodities and send a generell OnMarketUpdate event to all listeners (UI etc to update OnMarketUpdate event)

    second: have the prizes not stored in the class, but rather making a dictionary<commotity, prizes> so that every ui etc can fetch the needed commodity prize from the market dictionary.

    which one do you think is the best approach? or do you have other ideas?
    You dont have to give me complete solutions if you don't want to, just throw some google buzzwords at me ;)

    Thank you very much in advance !
    PBeast
     
  2. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    Hmm, interesting question. I haven't done anything similar but this is what I would do.

    If commodities are gameObjects, e.g. you can carry them around, I would create a CommodityBehaviour which inherits MonoBehaviour that you can attach to any gameObject you would like to sell / trade / whatever.

    Or if they are something intangible, like just an integer in your inventory system, a normal class like Commodity inheriting Object sounds fine. (or ScriptableObject if you're feeling lucky)

    In any case, I would recommend you to use ObservableList:
    http://answers.unity3d.com/questions/677956/getting-notified-when-modifying-a-collection-ala-c.html

    Basically you can subscribe your classes to events like Updated, Changed. That way you can add objects and all subscribers will be notified, thankfully avoiding Update() loop calls.

    Code (CSharp):
    1. ObservableList<Commodity> list = new ObservableList<Commodity>();
    2.  
    3. void Start()
    4. {
    5.      list.Updated += HandleUpdated;
    6.      list.Changed += HandleChanged;
    7.  
    8.      list.Add( myCommodity );
    9. }
    10.  
    11. void OnDestroy()
    12. {
    13.      list.Changed -= HandleChanged;
    14.      list.Updated -= HandleUpdated;
    15. }
    16.  
    17.  
    18. void HandleChanged(int index)
    19. {
    20.      //index of item changed
    21. }
    22.  
    23. void HandleUpdated()
    24. {
    25.      //list updated!
    26. }
     
    rakkarage and CxydaIO like this.
  3. CxydaIO

    CxydaIO

    Joined:
    May 12, 2014
    Posts:
    61
    Hi Fajlworks !
    Thanks for your response ! I think i don't want to handle Commodies as Monobehaviour Components, i don't like the idea to attach ~200 Components to my market GO and iterating each frame through their Update() :D
    I think i will update the market calculations every 15-30 seconds maybe and then use the events ...

    Thanks for telling me about the ObservableList ! This seems to really handy !