2
\$\begingroup\$

I'm using a model where entities are collections of components and components are just data. All the logic goes into systems which operate on components. Making basic systems(for Rendering and handling collision) was easy. But how do I do more compilcated systems? For example, in a CollisionSystem I can check if entity A collides with entity B. I have this code in CollisionSystem for checking if B damages A:

if(collides(a, b)) {
HealthComponent* hc = a->get<HealthComponent();
hc.reduceHealth(b->get<DamageComponent>()->getDamage());

But I feel that this code shouldn't belong to Collision system. Where should code like this be and which additional systems should I create to make this code generic?

\$\endgroup\$
3
\$\begingroup\$

Messaging is a common way of handling this. So your health system would subscribe to messages about collision, combat, etc. Then the health system is the only place where health is modified.

Messaging also allows multiple systems to subscribe to the same events. For example, when a collision happens the health system can be notified, sound system and the user notification system. Lots of options and it keeps code localized to the systems where it belongs.

See this question for some details about such systems.

\$\endgroup\$
  • \$\begingroup\$ Is there any resources which show how to implement and use those systems? I think OP's solution was too difficult to understand with all those long named classes. Most tutorials I've seen focus only on the aspect of creating simple systems, but I've never seen one where messaging between systems is covered. \$\endgroup\$ – Elias Daler Oct 26 '13 at 18:34
  • 1
    \$\begingroup\$ This site has a number of questions about it: gamedev.stackexchange.com/search?q=messaging+system+is%3Aq. Otherwise, you can ask a specific question about the trouble you're having implementing a messaging system if those questions don't solve your problems. \$\endgroup\$ – MichaelHouse Oct 26 '13 at 18:47
  • \$\begingroup\$ I'll read this, thanks. My problem definitely will be solved using this. \$\endgroup\$ – Elias Daler Oct 26 '13 at 19:04
  • \$\begingroup\$ By the way, which system do you use yourself? Any recommended approaches? Because most of the threads cover lots of themes to choose from. \$\endgroup\$ – Elias Daler Oct 26 '13 at 19:39
  • \$\begingroup\$ I rolled my own. It'll probably be specific to your requirements. Start with something simple and keep adding the things you need. Any further discussion about it should be taken to chat. Good luck! \$\endgroup\$ – MichaelHouse Oct 26 '13 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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