Advertisement

When x happens - trigger y? C#

Started by August 12, 2018 10:54 PM
4 comments, last by ChaosEngine 6 years, 1 month ago

How do I do title in C#?

Conceptually I am thinking when an event occurs in a game, it will trigger a bunch of things watching for said occurance.

Example: in a card game, I have a unit that has "whenever you play a unit this gains +1/+1". SO when I play a unit, a method in this card will get called giving it +1/+1;


When researching events however, i learned that you have to subscribe to the publisher of the event (correct me if I am wrong) but this defeats the purpose?? I don't know when or where the event will come from is the issue.

is there an alternate way of doing this?
Or is my understanding how events work incorrect?

There are lots of ways to implement this, all of which have pros and cons and will depend on the architecture of your game. 

But a simple method would be to allow each object to publish events to an event queue. Other objects could then subscribe to certain events in the event queue. 

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
Advertisement

Thank you for fast reply!

So in other words I make a middleman that connects every publisher to itself and every subscriber to itself?

And the middleman just "republish" everything it gets?

 

I think I get it...

 

Thanks

You create a "man in the middle" that has each sender send its events to and each subscriber register itself, there isn't any knowledge of that event manager that it has to take subscriptions itself or even tell any producer to register at it, thats not it's responsability rather than from all other components in your game.

7 hours ago, ChaosEngine said:

a simple method would be to allow each object to publish events to an event queue

It is more or less simple and effective depending on your games architecture. I always feeled that this is enougth and even better in asynchronous/multithreaded code to have an external event dispatcher. My version is capable of addressing different channels with different types of events and runs a dispatcher as bakround task/thread

2 hours ago, Shaarigan said:

It is more or less simple and effective depending on your games architecture. I always feeled that this is enougth and even better in asynchronous/multithreaded code to have an external event dispatcher. My version is capable of addressing different channels with different types of events and runs a dispatcher as bakround task/thread

Yeah, there's lots you can to in terms of functionality, complexity, performance, etc... all of which are dependent on the architecture, requirements, performance constraints, etc.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

This topic is closed to new replies.

Advertisement