MarketSystem design approach?!

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 :wink:

Thank you very much in advance !
PBeast

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:

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.

ObservableList<Commodity> list = new ObservableList<Commodity>();

void Start()
{
     list.Updated += HandleUpdated;
     list.Changed += HandleChanged;

     list.Add( myCommodity );
}

void OnDestroy()
{
     list.Changed -= HandleChanged;
     list.Updated -= HandleUpdated;
}


void HandleChanged(int index)
{
     //index of item changed
}

void HandleUpdated()
{
     //list updated!
}
2 Likes

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() :smile:
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 !