Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm designing an entity-component engine in C# right now, and all components need to have access to the global event manager, which sends off inter-entity events (every entity also has a local event manager).

I'd like to be able to simply call functions like this:

GlobalEventManager.Publish("Foo", new EventData());
GlobalEventManager.Subscribe("Bar", OnBarEvent);

without having to do this:

class HealthComponent
{
    private EventManager globalEventManager;

    public HealthComponent(EventManager gEM)
    {
        globalEventManager = gEM;
    }
}

// later on...
EventManager globalEventManager = new EventManager();
Entity playerEntity = new Entity();
playerEntity.AddComponent(new HealthComponent(globalEventManager));

How can I accomplish this?

share|improve this question
1  
Welcome to the site! I am glad you found a solution to your problem. I invite you to put the answer into an actual answer and mark it as the answer. The site more than allows for this :) –  James Mar 19 '12 at 16:59
    
I've also removed the "SOLVED" tag from your question, as the proper way to indicate such on this site is to post an answer and accept it, as James said (probably you will need to wait until you question is a few more hours old before the system will let you do so). –  Josh Petrie Mar 19 '12 at 17:12
    
Yeah, I tried doing that and it said I had to wait 8 more hours. –  Boreal Mar 19 '12 at 17:14
2  
It's been more than 8 hours :) –  Byte56 Oct 8 '12 at 3:17
add comment

3 Answers

Although your solution works, you are losing testability by using singleton. In addition you are highly coupled with your global objects. If you will ever think of unit tests for your new components, you will have specially prepare your GlobalEventManager and ensure that your tests are not run in parallel.

One option is to use Factory pattern. Something like:

 playerEntity.AddComponent(componentFactory.createHealth());

Or read about dependency injection frameworks.

share|improve this answer
add comment

Why not just use C#'s native event system? That way any components that want to subscribe to a component's events can do so from outside the class.

share|improve this answer
    
My own system works perfectly well and it's already very similar to C#'s events. The difference is that I have full control over it. I just want a way to call methods from an API object. –  Boreal Mar 19 '12 at 16:32
    
@Boreal Pass the API object to every object that calls its methods. –  Zolomon Mar 19 '12 at 16:46
    
@Boreal Singletons are killer for unit testing, I highly recommend finding a different approach. –  mrnye Mar 20 '12 at 10:06
add comment
up vote 1 down vote accepted

I solved it by creating a singleton called GlobalEventManager. It derives from the local EventManager class and I use it like this:

GlobalEventManager.Instance.Publish("Foo", new EventData());

Thanks Byte :p

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.