Jump to content
  • Advertisement
mercutio604

C# When x happens - trigger y? C#

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement

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. 

Share this post


Link to post
Share on other sites
Posted (edited)

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

Edited by mercutio604
forgot thanks

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!