Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Logic in components vs logic in systems?  (Read 1827 times)

0 Members and 1 Guest are viewing this topic.

Veritas

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
    • Email
Logic in components vs logic in systems?
« on: May 01, 2014, 09:56:39 pm »
Hello everyone, I did some research on the different practices and patterns used in game development and I settled on a composition system where the different components communicate using messages that the container-GameObject distributes.

For instance say that I press the attack key. The playerInput component sends an AttackMessage to all the other components using the notify function of the GameObject. The Weapon component picks the message, sets some local variables and sends a RigidBodyStateMessage to the RigidBody component which in turn sends another message containing the rotation and position of the GameObject. The Weapon component catches the message and instantiates a bullet using the information passed in the message.

Communication between different objects is achieved via messages sent in message buses on which the GameObjects have registered. For example, the PhysicsComponent continuously sends messages to the CollisionBus which are then delivered to the registered GameObjects for collisionDetection in the appropriate component.

This approach allows for complete decoupling between the components but I am not sure if it may create any long-term problems. Some people also told me that components shouldn't have any logic in them and that they should only keep data which will be processed in the appropriate systems. I was wondering what advantages the latter method would have compared to the one that I am currently using. Sorry for the wall of text, I am writing from my phone. Any help is much appreciated!
« Last Edit: May 01, 2014, 10:56:15 pm by Veritas »
"Lasciate ogni speranza, voi ch'entrate"

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Logic in components vs logic in systems?
« Reply #1 on: May 02, 2014, 12:44:00 am »
I guess there are many approaches to a component based system and message buses. I like Tank's approach quite good: Component System Part 1/Part 2 & Message Bus.
If I compare these articles to what you've described the thing that stands out the most, is what you're sending with the messages. I'd rather just use the message bus for trigger information and not for sending the actual data for all the different components.

But I guess I'm not the most qualified person to say something about this. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Veritas

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
    • Email
Re: Logic in components vs logic in systems?
« Reply #2 on: May 02, 2014, 12:10:43 pm »
So instead of keeping the functions in the components I have different systems that operate on the component data? I am not sure how that works. Let's take collision, if I understand this correctly the GameObjects have a RigidBody component that a CollisionSystem uses to detect collisions. I don't seem to understand who is responsible for reacting to the collision message though. Do I make ActionSystems for all the different monsters and handle things there?
« Last Edit: May 02, 2014, 02:04:18 pm by Veritas »
"Lasciate ogni speranza, voi ch'entrate"

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Logic in components vs logic in systems?
« Reply #3 on: May 02, 2014, 07:58:45 pm »
I'd like to ask this question too, in fact.  Most of the articles about component entity systems I've seen on the internet only seem to cover the concepts and none of the implementation, leaving me utterly lost as to how such a thing can actually be implemented without being so dynamic and abstract you end up completely incompatible with basic optimizations like sprite batching and quadtrees.  Tank's articles are a good example of that; I have no idea what any of his classes would actually contain or do at a code level, or what the top level of code would do with any of them.

So, instead of another rehash of the vague concepts, could someone knowledgeable about these systems provide a complete toy example?

Veritas

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
    • Email
Re: Logic in components vs logic in systems?
« Reply #4 on: May 02, 2014, 10:18:34 pm »
This seems to be the main problem for me too. There are many resources on the concept of systems and components but the only actual implementations I have found keep logic inside the components and don't use systems.
"Lasciate ogni speranza, voi ch'entrate"

 

anything