2

Is there a way to pass Events between Systems in a multi-threaded Entity-Component-System that realizes the benefits of saving on system resources?

I've read a german article about the Entity-Component-System design-pattern. In this article, the author wrote, that it would be good to let the systems communicate via Events with each other. So, if the CollisionSystem fires an CollisionEvent when an Entity has collided with another one the SoundSystem can play a sound.

I've implemented that with the Observer-pattern: An EventManager receives events from Systems and notifies all Systems. Then the Systems can look themselves if the want to react on that Event.

By using this Event-based system I can save a lot of computer-power. A System just does anything if it has to do. The author from the article also wrote, that because of that encapsulation from Systems you can put every System in it's own thread.

I tried to implement that behavior, but noticed, that if System A fires an Event and System B reacts on this Event because it's update-Method is called from the EventManager, the action gets called in the Thread of System A.

The only solution which comes to my mind is, to have a loop inside each System which looks in a fixed frequency through a Event-Queue and if it finds an interesting Event it fires the action by itself. In that way I would loose my Observer-Pattern and would not save the computer-power.

2
  • 1
    Just make sure Actor Model is not a better fit :). Depends on project of course.
    – Den
    Oct 17, 2014 at 16:21
  • @Den I think, I will use a event queue for my project and let the systems threads wait on the queue, like Iden Arye supposed. I have to inform me more about the actor model, before I can say, that I will use it. Thanks for your thoughts! Oct 17, 2014 at 16:27

1 Answer 1

2

The observer pattern does not fit here. Observers are being called synchronically, and must finish before the caller may proceed. What you want is the Actor Model - which, like you said, uses event queues. You don't have to do busy waiting though - there are more efficient ways to do it, like making threads wait on the event queues.

1
  • Thank you really much. I was hoping to receive such an answere. Sep 23, 2014 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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