Should components in Entity Component System pattern have logic?












1















Is often read that in entity component system pattern we should treat components just as a passive data structure with no logic at all, this way we follow to a data oriented design approach with efficient usage of cache memory and gain performance.



I was developing a simple game using this rule and when the game was getting bigger I start facing some serious code smells such as repeated code in systems, let's suppose its a shooter to illustrate an example. In this shooter I had bullets being fired so I created a BulletComponent and a BulletSystem that handles when it collides and make some damage and if don't collide with anything the bullet dissapear in five seconds. Then I wanted some explosives so I created the ExplosiveComponent and ExplosionSystem, once the explosive is placed it explodes in ten seconds. At this point we can notice the first repeated code between systems then we can make an abstraction and we create a DurationComponent and a DurationSystem so the bullet and the explosive contains it with the time it should last.Then I realized that when the explosive should explode I want to create some particle effect in the area so this abstraction is no longer valid as I want to perform differents actions when time runs out, but if I do as at the beginning I have the repeated code between systems (ugh!). This example is simple but I'm facing this with more complex things like AI, or reacting to collisions.



One solution I came to this is that I really wanted an ActionComponent with a ActionSystem that executes the action contained in the component but this way I'm breaking that components only contains data and they should not have logic! this is more an 'hybrid' approach. But if you start thinking deeply the ActionSystem is very generic and you can almost do anything with it so it's hard to determine whether a system is necessary or not. The same can be applied to collisions, instead of having lots of systems with logic for different entities colliding, you can just have a CollisionHandlerComponent with the logic on it for each entity.



I found the following advantages making generic systems like this:




  • Higher flexibility (you can customize each entity easily)

  • No repeated code

  • Less systems (more performance?)


But as I said the main disadvantage is that is really confusing if a system is needed as you can stick logic to components.



Is it wrong to follow this sort of hybrid ecs? If yes, what would be your approach to the example without repeating code in systems?










share|improve this question







New contributor




Alejandro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • This God Object smells way worse than your original design problem: "...you start thinking deeply the ActionSystem is very generic and you can almost do anything with it..."

    – Patrick Hughes
    8 hours ago











  • Also smells to me but not as having a lot of repeated code and lots of systems for having custom behaviour. I named it ActionComponent but it could be something like ScriptComponent that executes the contained script every tick. I think Unity uses some approach like this for I've seen but I never used it before. Also what concerned me about having a lot of systems is that every time a component is added or removed families of entities of all systems should be updated, this add overhead to these operations cause it has to iterate and update list of entities of systems that doesn't even change

    – Alejandro
    7 hours ago
















1















Is often read that in entity component system pattern we should treat components just as a passive data structure with no logic at all, this way we follow to a data oriented design approach with efficient usage of cache memory and gain performance.



I was developing a simple game using this rule and when the game was getting bigger I start facing some serious code smells such as repeated code in systems, let's suppose its a shooter to illustrate an example. In this shooter I had bullets being fired so I created a BulletComponent and a BulletSystem that handles when it collides and make some damage and if don't collide with anything the bullet dissapear in five seconds. Then I wanted some explosives so I created the ExplosiveComponent and ExplosionSystem, once the explosive is placed it explodes in ten seconds. At this point we can notice the first repeated code between systems then we can make an abstraction and we create a DurationComponent and a DurationSystem so the bullet and the explosive contains it with the time it should last.Then I realized that when the explosive should explode I want to create some particle effect in the area so this abstraction is no longer valid as I want to perform differents actions when time runs out, but if I do as at the beginning I have the repeated code between systems (ugh!). This example is simple but I'm facing this with more complex things like AI, or reacting to collisions.



One solution I came to this is that I really wanted an ActionComponent with a ActionSystem that executes the action contained in the component but this way I'm breaking that components only contains data and they should not have logic! this is more an 'hybrid' approach. But if you start thinking deeply the ActionSystem is very generic and you can almost do anything with it so it's hard to determine whether a system is necessary or not. The same can be applied to collisions, instead of having lots of systems with logic for different entities colliding, you can just have a CollisionHandlerComponent with the logic on it for each entity.



I found the following advantages making generic systems like this:




  • Higher flexibility (you can customize each entity easily)

  • No repeated code

  • Less systems (more performance?)


But as I said the main disadvantage is that is really confusing if a system is needed as you can stick logic to components.



Is it wrong to follow this sort of hybrid ecs? If yes, what would be your approach to the example without repeating code in systems?










share|improve this question







New contributor




Alejandro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • This God Object smells way worse than your original design problem: "...you start thinking deeply the ActionSystem is very generic and you can almost do anything with it..."

    – Patrick Hughes
    8 hours ago











  • Also smells to me but not as having a lot of repeated code and lots of systems for having custom behaviour. I named it ActionComponent but it could be something like ScriptComponent that executes the contained script every tick. I think Unity uses some approach like this for I've seen but I never used it before. Also what concerned me about having a lot of systems is that every time a component is added or removed families of entities of all systems should be updated, this add overhead to these operations cause it has to iterate and update list of entities of systems that doesn't even change

    – Alejandro
    7 hours ago














1












1








1








Is often read that in entity component system pattern we should treat components just as a passive data structure with no logic at all, this way we follow to a data oriented design approach with efficient usage of cache memory and gain performance.



I was developing a simple game using this rule and when the game was getting bigger I start facing some serious code smells such as repeated code in systems, let's suppose its a shooter to illustrate an example. In this shooter I had bullets being fired so I created a BulletComponent and a BulletSystem that handles when it collides and make some damage and if don't collide with anything the bullet dissapear in five seconds. Then I wanted some explosives so I created the ExplosiveComponent and ExplosionSystem, once the explosive is placed it explodes in ten seconds. At this point we can notice the first repeated code between systems then we can make an abstraction and we create a DurationComponent and a DurationSystem so the bullet and the explosive contains it with the time it should last.Then I realized that when the explosive should explode I want to create some particle effect in the area so this abstraction is no longer valid as I want to perform differents actions when time runs out, but if I do as at the beginning I have the repeated code between systems (ugh!). This example is simple but I'm facing this with more complex things like AI, or reacting to collisions.



One solution I came to this is that I really wanted an ActionComponent with a ActionSystem that executes the action contained in the component but this way I'm breaking that components only contains data and they should not have logic! this is more an 'hybrid' approach. But if you start thinking deeply the ActionSystem is very generic and you can almost do anything with it so it's hard to determine whether a system is necessary or not. The same can be applied to collisions, instead of having lots of systems with logic for different entities colliding, you can just have a CollisionHandlerComponent with the logic on it for each entity.



I found the following advantages making generic systems like this:




  • Higher flexibility (you can customize each entity easily)

  • No repeated code

  • Less systems (more performance?)


But as I said the main disadvantage is that is really confusing if a system is needed as you can stick logic to components.



Is it wrong to follow this sort of hybrid ecs? If yes, what would be your approach to the example without repeating code in systems?










share|improve this question







New contributor




Alejandro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












Is often read that in entity component system pattern we should treat components just as a passive data structure with no logic at all, this way we follow to a data oriented design approach with efficient usage of cache memory and gain performance.



I was developing a simple game using this rule and when the game was getting bigger I start facing some serious code smells such as repeated code in systems, let's suppose its a shooter to illustrate an example. In this shooter I had bullets being fired so I created a BulletComponent and a BulletSystem that handles when it collides and make some damage and if don't collide with anything the bullet dissapear in five seconds. Then I wanted some explosives so I created the ExplosiveComponent and ExplosionSystem, once the explosive is placed it explodes in ten seconds. At this point we can notice the first repeated code between systems then we can make an abstraction and we create a DurationComponent and a DurationSystem so the bullet and the explosive contains it with the time it should last.Then I realized that when the explosive should explode I want to create some particle effect in the area so this abstraction is no longer valid as I want to perform differents actions when time runs out, but if I do as at the beginning I have the repeated code between systems (ugh!). This example is simple but I'm facing this with more complex things like AI, or reacting to collisions.



One solution I came to this is that I really wanted an ActionComponent with a ActionSystem that executes the action contained in the component but this way I'm breaking that components only contains data and they should not have logic! this is more an 'hybrid' approach. But if you start thinking deeply the ActionSystem is very generic and you can almost do anything with it so it's hard to determine whether a system is necessary or not. The same can be applied to collisions, instead of having lots of systems with logic for different entities colliding, you can just have a CollisionHandlerComponent with the logic on it for each entity.



I found the following advantages making generic systems like this:




  • Higher flexibility (you can customize each entity easily)

  • No repeated code

  • Less systems (more performance?)


But as I said the main disadvantage is that is really confusing if a system is needed as you can stick logic to components.



Is it wrong to follow this sort of hybrid ecs? If yes, what would be your approach to the example without repeating code in systems?







entity-system component-based logic data-oriented entity-component-system






share|improve this question







New contributor




Alejandro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Alejandro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Alejandro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 11 hours ago









AlejandroAlejandro

62




62




New contributor




Alejandro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Alejandro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Alejandro is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • This God Object smells way worse than your original design problem: "...you start thinking deeply the ActionSystem is very generic and you can almost do anything with it..."

    – Patrick Hughes
    8 hours ago











  • Also smells to me but not as having a lot of repeated code and lots of systems for having custom behaviour. I named it ActionComponent but it could be something like ScriptComponent that executes the contained script every tick. I think Unity uses some approach like this for I've seen but I never used it before. Also what concerned me about having a lot of systems is that every time a component is added or removed families of entities of all systems should be updated, this add overhead to these operations cause it has to iterate and update list of entities of systems that doesn't even change

    – Alejandro
    7 hours ago



















  • This God Object smells way worse than your original design problem: "...you start thinking deeply the ActionSystem is very generic and you can almost do anything with it..."

    – Patrick Hughes
    8 hours ago











  • Also smells to me but not as having a lot of repeated code and lots of systems for having custom behaviour. I named it ActionComponent but it could be something like ScriptComponent that executes the contained script every tick. I think Unity uses some approach like this for I've seen but I never used it before. Also what concerned me about having a lot of systems is that every time a component is added or removed families of entities of all systems should be updated, this add overhead to these operations cause it has to iterate and update list of entities of systems that doesn't even change

    – Alejandro
    7 hours ago

















This God Object smells way worse than your original design problem: "...you start thinking deeply the ActionSystem is very generic and you can almost do anything with it..."

– Patrick Hughes
8 hours ago





This God Object smells way worse than your original design problem: "...you start thinking deeply the ActionSystem is very generic and you can almost do anything with it..."

– Patrick Hughes
8 hours ago













Also smells to me but not as having a lot of repeated code and lots of systems for having custom behaviour. I named it ActionComponent but it could be something like ScriptComponent that executes the contained script every tick. I think Unity uses some approach like this for I've seen but I never used it before. Also what concerned me about having a lot of systems is that every time a component is added or removed families of entities of all systems should be updated, this add overhead to these operations cause it has to iterate and update list of entities of systems that doesn't even change

– Alejandro
7 hours ago





Also smells to me but not as having a lot of repeated code and lots of systems for having custom behaviour. I named it ActionComponent but it could be something like ScriptComponent that executes the contained script every tick. I think Unity uses some approach like this for I've seen but I never used it before. Also what concerned me about having a lot of systems is that every time a component is added or removed families of entities of all systems should be updated, this add overhead to these operations cause it has to iterate and update list of entities of systems that doesn't even change

– Alejandro
7 hours ago










1 Answer
1






active

oldest

votes


















2














I was considering voting to close this question as opinion-based, but I think there are a couple of misunderstandings about Entity-Component Systems and Data-Oriented Design here that are worth addressing.





  1. ECS is just a tool



    Like any programming pattern or principle, it exists to help you write good software. It is not an unbreakable law of physics or a straight jacket meant to inhibit you from doing your work.



    If you find that for your particular needs, adding logic to your component makes your game perform better or more reliably, or helps you write clearer or more maintainable code, or helps you improve your development and iteration speed, then that might very well be a worthwhile exception to make to the rule.



    There's no absolute standard to which every component system must adhere, so make yours work for you. Games get no points for compliance to X or Y's definition of what its code "should" do, only for shipping and offering a great play experience.



    (This is the reason I normally consider "am I allowed to/do I have to do X in an ECS" questions to be opinion-based)




  2. Not everything has to be a component or system



    We wouldn't search for every instance of + in our codebase and replace it with an AdditionComponent processed by an AdditionSystem — that would be just silly.



    A duration isn't much more complicated than that. You're checking a time against a threshold, something you can do in one line. So you don't gain any particular utility, clarity, or simplicity/reduction in code by packing this up into a DurationComponent



    If you really want, you could make a simple Expiration struct that's just a wrapper around a timestamp, computing an expiration date duration seconds in the future upon construction, and exposing an IsExpired predicate to check the stored timestamp against the current time. This packages up this small amount of time-related code for reuse, without making it a whole new component & system.



    Then your bullet component, explosive component, etc can store an Expiration as part of their data, and their respective system updates can check the expiration and do the appropriate thing for bullets or explosives respectively.



    Far from unnecessarily duplicating code, you can gain advantages from doing it this way — for instance, your bullet system can recycle expired bullets as it runs through its batch, saving any cost of a separate removal by doing it as part of the iteration it's already doing anyway.




  3. Fewer systems does not necessarily mean better performance



    The major factor in how data-oriented ECS systems aim to boost performance is cache.



    When you have a system that does one consistent set of operations repeatedly on a contiguous array of similar data, the code you need stays hot in the instruction cache, and the data access patterns are predictable so pre-fetching keeps your data cache full, so your processor can hum along at full speed.



    As soon as you throw in indirection, like firing some arbitrary "On Expiration" or generic "Action" callback/delegate/function pointer that might point to something different for every item in the batch, that goes out the window, and you risk cache misses costing hundreds of processor cycles for each hop.



    So no, reducing the amount of unique code won't necessarily make it perform better, and it can in fact make it slower if you're counting on data-oriented batching to push your cache to its limits.



    It's also not necessarily good for development iteration. Although ostensibly you have less code to write and keep track of, that code ends up being a lot more generic and ambiguous about what it's doing than a group of systems each focused on doing their one job. The latter gives you much more predictable code that's easy to follow and debug than chasing data triggers all over memory.




So, the upshot is: you can adapt your version of a component system to work however you want. Just be sure you understand the reasons you're making the choices you're making, and the reasons the recommendations that caution against them exist, so you can weigh those considerations and make the call that's right for your game's needs.






share|improve this answer
























  • 2. It is arguable that an addition is just as an abstract that entities expiring. For my particular case I do have a timestamp that compares to actual time and when the entity expires is scheduled for removal, but its just in one place. It could be wrapped as you said but the feel of repeated code still there between systems and components, I mean the concept of in X time schedule removal, or even in X time do Y. I know this example was simple, but it also happens with more complex things like AI.

    – Alejandro
    6 hours ago











  • In AI you have a lot of specific behaviour for individuals entities, and sometimes maybe just an AI for an unique entity. -If you have multiples components & systems for differents AI types, you have lot of repeated stuff in the systems. -If you have just one component specifing the AI type, you end with one huge unmaintainable system handling all AI cases. -If you do the polymorfic approach leting the AIComponent handle the specific behaviour of the AI the code got cleaner. So this make me think that pure data approach its not viable in all cases.

    – Alejandro
    5 hours ago











  • 3. I know the amount of unique code won't necessarily make it perform better, I did not express myself enough. This is really dependant of the implementation of the ECS, but in my case when a component is added or removed of an entity it iterates all systems to check if the mask of the components on it matches the system, so it add it or remove from its list. This is pretty fast for a few systems, but its not the same if you have 10 systems than if you have 500 systems and maybe even some systems just hold logic for one particular entity. This don't happen with generic systems.

    – Alejandro
    5 hours ago











  • That sounds like an implementation bug. You can do that iteration (lazily if you choose) once for each component mask and save the results, so your cost to add a component is proportionate to the number of systems that need it, not the number of systems you have. But you really don't need that many systems because 2: not everything has to be a system. Specific to your example, AI don't all need to run through one monolithic system, nor have unique bespoke systems each. A behaviour tree for the logic leveraging a small kit of shared movement/pathfinding/action components can often work well.

    – DMGregory
    4 hours ago













Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "53"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Alejandro is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgamedev.stackexchange.com%2fquestions%2f167058%2fshould-components-in-entity-component-system-pattern-have-logic%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I was considering voting to close this question as opinion-based, but I think there are a couple of misunderstandings about Entity-Component Systems and Data-Oriented Design here that are worth addressing.





  1. ECS is just a tool



    Like any programming pattern or principle, it exists to help you write good software. It is not an unbreakable law of physics or a straight jacket meant to inhibit you from doing your work.



    If you find that for your particular needs, adding logic to your component makes your game perform better or more reliably, or helps you write clearer or more maintainable code, or helps you improve your development and iteration speed, then that might very well be a worthwhile exception to make to the rule.



    There's no absolute standard to which every component system must adhere, so make yours work for you. Games get no points for compliance to X or Y's definition of what its code "should" do, only for shipping and offering a great play experience.



    (This is the reason I normally consider "am I allowed to/do I have to do X in an ECS" questions to be opinion-based)




  2. Not everything has to be a component or system



    We wouldn't search for every instance of + in our codebase and replace it with an AdditionComponent processed by an AdditionSystem — that would be just silly.



    A duration isn't much more complicated than that. You're checking a time against a threshold, something you can do in one line. So you don't gain any particular utility, clarity, or simplicity/reduction in code by packing this up into a DurationComponent



    If you really want, you could make a simple Expiration struct that's just a wrapper around a timestamp, computing an expiration date duration seconds in the future upon construction, and exposing an IsExpired predicate to check the stored timestamp against the current time. This packages up this small amount of time-related code for reuse, without making it a whole new component & system.



    Then your bullet component, explosive component, etc can store an Expiration as part of their data, and their respective system updates can check the expiration and do the appropriate thing for bullets or explosives respectively.



    Far from unnecessarily duplicating code, you can gain advantages from doing it this way — for instance, your bullet system can recycle expired bullets as it runs through its batch, saving any cost of a separate removal by doing it as part of the iteration it's already doing anyway.




  3. Fewer systems does not necessarily mean better performance



    The major factor in how data-oriented ECS systems aim to boost performance is cache.



    When you have a system that does one consistent set of operations repeatedly on a contiguous array of similar data, the code you need stays hot in the instruction cache, and the data access patterns are predictable so pre-fetching keeps your data cache full, so your processor can hum along at full speed.



    As soon as you throw in indirection, like firing some arbitrary "On Expiration" or generic "Action" callback/delegate/function pointer that might point to something different for every item in the batch, that goes out the window, and you risk cache misses costing hundreds of processor cycles for each hop.



    So no, reducing the amount of unique code won't necessarily make it perform better, and it can in fact make it slower if you're counting on data-oriented batching to push your cache to its limits.



    It's also not necessarily good for development iteration. Although ostensibly you have less code to write and keep track of, that code ends up being a lot more generic and ambiguous about what it's doing than a group of systems each focused on doing their one job. The latter gives you much more predictable code that's easy to follow and debug than chasing data triggers all over memory.




So, the upshot is: you can adapt your version of a component system to work however you want. Just be sure you understand the reasons you're making the choices you're making, and the reasons the recommendations that caution against them exist, so you can weigh those considerations and make the call that's right for your game's needs.






share|improve this answer
























  • 2. It is arguable that an addition is just as an abstract that entities expiring. For my particular case I do have a timestamp that compares to actual time and when the entity expires is scheduled for removal, but its just in one place. It could be wrapped as you said but the feel of repeated code still there between systems and components, I mean the concept of in X time schedule removal, or even in X time do Y. I know this example was simple, but it also happens with more complex things like AI.

    – Alejandro
    6 hours ago











  • In AI you have a lot of specific behaviour for individuals entities, and sometimes maybe just an AI for an unique entity. -If you have multiples components & systems for differents AI types, you have lot of repeated stuff in the systems. -If you have just one component specifing the AI type, you end with one huge unmaintainable system handling all AI cases. -If you do the polymorfic approach leting the AIComponent handle the specific behaviour of the AI the code got cleaner. So this make me think that pure data approach its not viable in all cases.

    – Alejandro
    5 hours ago











  • 3. I know the amount of unique code won't necessarily make it perform better, I did not express myself enough. This is really dependant of the implementation of the ECS, but in my case when a component is added or removed of an entity it iterates all systems to check if the mask of the components on it matches the system, so it add it or remove from its list. This is pretty fast for a few systems, but its not the same if you have 10 systems than if you have 500 systems and maybe even some systems just hold logic for one particular entity. This don't happen with generic systems.

    – Alejandro
    5 hours ago











  • That sounds like an implementation bug. You can do that iteration (lazily if you choose) once for each component mask and save the results, so your cost to add a component is proportionate to the number of systems that need it, not the number of systems you have. But you really don't need that many systems because 2: not everything has to be a system. Specific to your example, AI don't all need to run through one monolithic system, nor have unique bespoke systems each. A behaviour tree for the logic leveraging a small kit of shared movement/pathfinding/action components can often work well.

    – DMGregory
    4 hours ago


















2














I was considering voting to close this question as opinion-based, but I think there are a couple of misunderstandings about Entity-Component Systems and Data-Oriented Design here that are worth addressing.





  1. ECS is just a tool



    Like any programming pattern or principle, it exists to help you write good software. It is not an unbreakable law of physics or a straight jacket meant to inhibit you from doing your work.



    If you find that for your particular needs, adding logic to your component makes your game perform better or more reliably, or helps you write clearer or more maintainable code, or helps you improve your development and iteration speed, then that might very well be a worthwhile exception to make to the rule.



    There's no absolute standard to which every component system must adhere, so make yours work for you. Games get no points for compliance to X or Y's definition of what its code "should" do, only for shipping and offering a great play experience.



    (This is the reason I normally consider "am I allowed to/do I have to do X in an ECS" questions to be opinion-based)




  2. Not everything has to be a component or system



    We wouldn't search for every instance of + in our codebase and replace it with an AdditionComponent processed by an AdditionSystem — that would be just silly.



    A duration isn't much more complicated than that. You're checking a time against a threshold, something you can do in one line. So you don't gain any particular utility, clarity, or simplicity/reduction in code by packing this up into a DurationComponent



    If you really want, you could make a simple Expiration struct that's just a wrapper around a timestamp, computing an expiration date duration seconds in the future upon construction, and exposing an IsExpired predicate to check the stored timestamp against the current time. This packages up this small amount of time-related code for reuse, without making it a whole new component & system.



    Then your bullet component, explosive component, etc can store an Expiration as part of their data, and their respective system updates can check the expiration and do the appropriate thing for bullets or explosives respectively.



    Far from unnecessarily duplicating code, you can gain advantages from doing it this way — for instance, your bullet system can recycle expired bullets as it runs through its batch, saving any cost of a separate removal by doing it as part of the iteration it's already doing anyway.




  3. Fewer systems does not necessarily mean better performance



    The major factor in how data-oriented ECS systems aim to boost performance is cache.



    When you have a system that does one consistent set of operations repeatedly on a contiguous array of similar data, the code you need stays hot in the instruction cache, and the data access patterns are predictable so pre-fetching keeps your data cache full, so your processor can hum along at full speed.



    As soon as you throw in indirection, like firing some arbitrary "On Expiration" or generic "Action" callback/delegate/function pointer that might point to something different for every item in the batch, that goes out the window, and you risk cache misses costing hundreds of processor cycles for each hop.



    So no, reducing the amount of unique code won't necessarily make it perform better, and it can in fact make it slower if you're counting on data-oriented batching to push your cache to its limits.



    It's also not necessarily good for development iteration. Although ostensibly you have less code to write and keep track of, that code ends up being a lot more generic and ambiguous about what it's doing than a group of systems each focused on doing their one job. The latter gives you much more predictable code that's easy to follow and debug than chasing data triggers all over memory.




So, the upshot is: you can adapt your version of a component system to work however you want. Just be sure you understand the reasons you're making the choices you're making, and the reasons the recommendations that caution against them exist, so you can weigh those considerations and make the call that's right for your game's needs.






share|improve this answer
























  • 2. It is arguable that an addition is just as an abstract that entities expiring. For my particular case I do have a timestamp that compares to actual time and when the entity expires is scheduled for removal, but its just in one place. It could be wrapped as you said but the feel of repeated code still there between systems and components, I mean the concept of in X time schedule removal, or even in X time do Y. I know this example was simple, but it also happens with more complex things like AI.

    – Alejandro
    6 hours ago











  • In AI you have a lot of specific behaviour for individuals entities, and sometimes maybe just an AI for an unique entity. -If you have multiples components & systems for differents AI types, you have lot of repeated stuff in the systems. -If you have just one component specifing the AI type, you end with one huge unmaintainable system handling all AI cases. -If you do the polymorfic approach leting the AIComponent handle the specific behaviour of the AI the code got cleaner. So this make me think that pure data approach its not viable in all cases.

    – Alejandro
    5 hours ago











  • 3. I know the amount of unique code won't necessarily make it perform better, I did not express myself enough. This is really dependant of the implementation of the ECS, but in my case when a component is added or removed of an entity it iterates all systems to check if the mask of the components on it matches the system, so it add it or remove from its list. This is pretty fast for a few systems, but its not the same if you have 10 systems than if you have 500 systems and maybe even some systems just hold logic for one particular entity. This don't happen with generic systems.

    – Alejandro
    5 hours ago











  • That sounds like an implementation bug. You can do that iteration (lazily if you choose) once for each component mask and save the results, so your cost to add a component is proportionate to the number of systems that need it, not the number of systems you have. But you really don't need that many systems because 2: not everything has to be a system. Specific to your example, AI don't all need to run through one monolithic system, nor have unique bespoke systems each. A behaviour tree for the logic leveraging a small kit of shared movement/pathfinding/action components can often work well.

    – DMGregory
    4 hours ago
















2












2








2







I was considering voting to close this question as opinion-based, but I think there are a couple of misunderstandings about Entity-Component Systems and Data-Oriented Design here that are worth addressing.





  1. ECS is just a tool



    Like any programming pattern or principle, it exists to help you write good software. It is not an unbreakable law of physics or a straight jacket meant to inhibit you from doing your work.



    If you find that for your particular needs, adding logic to your component makes your game perform better or more reliably, or helps you write clearer or more maintainable code, or helps you improve your development and iteration speed, then that might very well be a worthwhile exception to make to the rule.



    There's no absolute standard to which every component system must adhere, so make yours work for you. Games get no points for compliance to X or Y's definition of what its code "should" do, only for shipping and offering a great play experience.



    (This is the reason I normally consider "am I allowed to/do I have to do X in an ECS" questions to be opinion-based)




  2. Not everything has to be a component or system



    We wouldn't search for every instance of + in our codebase and replace it with an AdditionComponent processed by an AdditionSystem — that would be just silly.



    A duration isn't much more complicated than that. You're checking a time against a threshold, something you can do in one line. So you don't gain any particular utility, clarity, or simplicity/reduction in code by packing this up into a DurationComponent



    If you really want, you could make a simple Expiration struct that's just a wrapper around a timestamp, computing an expiration date duration seconds in the future upon construction, and exposing an IsExpired predicate to check the stored timestamp against the current time. This packages up this small amount of time-related code for reuse, without making it a whole new component & system.



    Then your bullet component, explosive component, etc can store an Expiration as part of their data, and their respective system updates can check the expiration and do the appropriate thing for bullets or explosives respectively.



    Far from unnecessarily duplicating code, you can gain advantages from doing it this way — for instance, your bullet system can recycle expired bullets as it runs through its batch, saving any cost of a separate removal by doing it as part of the iteration it's already doing anyway.




  3. Fewer systems does not necessarily mean better performance



    The major factor in how data-oriented ECS systems aim to boost performance is cache.



    When you have a system that does one consistent set of operations repeatedly on a contiguous array of similar data, the code you need stays hot in the instruction cache, and the data access patterns are predictable so pre-fetching keeps your data cache full, so your processor can hum along at full speed.



    As soon as you throw in indirection, like firing some arbitrary "On Expiration" or generic "Action" callback/delegate/function pointer that might point to something different for every item in the batch, that goes out the window, and you risk cache misses costing hundreds of processor cycles for each hop.



    So no, reducing the amount of unique code won't necessarily make it perform better, and it can in fact make it slower if you're counting on data-oriented batching to push your cache to its limits.



    It's also not necessarily good for development iteration. Although ostensibly you have less code to write and keep track of, that code ends up being a lot more generic and ambiguous about what it's doing than a group of systems each focused on doing their one job. The latter gives you much more predictable code that's easy to follow and debug than chasing data triggers all over memory.




So, the upshot is: you can adapt your version of a component system to work however you want. Just be sure you understand the reasons you're making the choices you're making, and the reasons the recommendations that caution against them exist, so you can weigh those considerations and make the call that's right for your game's needs.






share|improve this answer













I was considering voting to close this question as opinion-based, but I think there are a couple of misunderstandings about Entity-Component Systems and Data-Oriented Design here that are worth addressing.





  1. ECS is just a tool



    Like any programming pattern or principle, it exists to help you write good software. It is not an unbreakable law of physics or a straight jacket meant to inhibit you from doing your work.



    If you find that for your particular needs, adding logic to your component makes your game perform better or more reliably, or helps you write clearer or more maintainable code, or helps you improve your development and iteration speed, then that might very well be a worthwhile exception to make to the rule.



    There's no absolute standard to which every component system must adhere, so make yours work for you. Games get no points for compliance to X or Y's definition of what its code "should" do, only for shipping and offering a great play experience.



    (This is the reason I normally consider "am I allowed to/do I have to do X in an ECS" questions to be opinion-based)




  2. Not everything has to be a component or system



    We wouldn't search for every instance of + in our codebase and replace it with an AdditionComponent processed by an AdditionSystem — that would be just silly.



    A duration isn't much more complicated than that. You're checking a time against a threshold, something you can do in one line. So you don't gain any particular utility, clarity, or simplicity/reduction in code by packing this up into a DurationComponent



    If you really want, you could make a simple Expiration struct that's just a wrapper around a timestamp, computing an expiration date duration seconds in the future upon construction, and exposing an IsExpired predicate to check the stored timestamp against the current time. This packages up this small amount of time-related code for reuse, without making it a whole new component & system.



    Then your bullet component, explosive component, etc can store an Expiration as part of their data, and their respective system updates can check the expiration and do the appropriate thing for bullets or explosives respectively.



    Far from unnecessarily duplicating code, you can gain advantages from doing it this way — for instance, your bullet system can recycle expired bullets as it runs through its batch, saving any cost of a separate removal by doing it as part of the iteration it's already doing anyway.




  3. Fewer systems does not necessarily mean better performance



    The major factor in how data-oriented ECS systems aim to boost performance is cache.



    When you have a system that does one consistent set of operations repeatedly on a contiguous array of similar data, the code you need stays hot in the instruction cache, and the data access patterns are predictable so pre-fetching keeps your data cache full, so your processor can hum along at full speed.



    As soon as you throw in indirection, like firing some arbitrary "On Expiration" or generic "Action" callback/delegate/function pointer that might point to something different for every item in the batch, that goes out the window, and you risk cache misses costing hundreds of processor cycles for each hop.



    So no, reducing the amount of unique code won't necessarily make it perform better, and it can in fact make it slower if you're counting on data-oriented batching to push your cache to its limits.



    It's also not necessarily good for development iteration. Although ostensibly you have less code to write and keep track of, that code ends up being a lot more generic and ambiguous about what it's doing than a group of systems each focused on doing their one job. The latter gives you much more predictable code that's easy to follow and debug than chasing data triggers all over memory.




So, the upshot is: you can adapt your version of a component system to work however you want. Just be sure you understand the reasons you're making the choices you're making, and the reasons the recommendations that caution against them exist, so you can weigh those considerations and make the call that's right for your game's needs.







share|improve this answer












share|improve this answer



share|improve this answer










answered 6 hours ago









DMGregoryDMGregory

59.1k13106169




59.1k13106169













  • 2. It is arguable that an addition is just as an abstract that entities expiring. For my particular case I do have a timestamp that compares to actual time and when the entity expires is scheduled for removal, but its just in one place. It could be wrapped as you said but the feel of repeated code still there between systems and components, I mean the concept of in X time schedule removal, or even in X time do Y. I know this example was simple, but it also happens with more complex things like AI.

    – Alejandro
    6 hours ago











  • In AI you have a lot of specific behaviour for individuals entities, and sometimes maybe just an AI for an unique entity. -If you have multiples components & systems for differents AI types, you have lot of repeated stuff in the systems. -If you have just one component specifing the AI type, you end with one huge unmaintainable system handling all AI cases. -If you do the polymorfic approach leting the AIComponent handle the specific behaviour of the AI the code got cleaner. So this make me think that pure data approach its not viable in all cases.

    – Alejandro
    5 hours ago











  • 3. I know the amount of unique code won't necessarily make it perform better, I did not express myself enough. This is really dependant of the implementation of the ECS, but in my case when a component is added or removed of an entity it iterates all systems to check if the mask of the components on it matches the system, so it add it or remove from its list. This is pretty fast for a few systems, but its not the same if you have 10 systems than if you have 500 systems and maybe even some systems just hold logic for one particular entity. This don't happen with generic systems.

    – Alejandro
    5 hours ago











  • That sounds like an implementation bug. You can do that iteration (lazily if you choose) once for each component mask and save the results, so your cost to add a component is proportionate to the number of systems that need it, not the number of systems you have. But you really don't need that many systems because 2: not everything has to be a system. Specific to your example, AI don't all need to run through one monolithic system, nor have unique bespoke systems each. A behaviour tree for the logic leveraging a small kit of shared movement/pathfinding/action components can often work well.

    – DMGregory
    4 hours ago





















  • 2. It is arguable that an addition is just as an abstract that entities expiring. For my particular case I do have a timestamp that compares to actual time and when the entity expires is scheduled for removal, but its just in one place. It could be wrapped as you said but the feel of repeated code still there between systems and components, I mean the concept of in X time schedule removal, or even in X time do Y. I know this example was simple, but it also happens with more complex things like AI.

    – Alejandro
    6 hours ago











  • In AI you have a lot of specific behaviour for individuals entities, and sometimes maybe just an AI for an unique entity. -If you have multiples components & systems for differents AI types, you have lot of repeated stuff in the systems. -If you have just one component specifing the AI type, you end with one huge unmaintainable system handling all AI cases. -If you do the polymorfic approach leting the AIComponent handle the specific behaviour of the AI the code got cleaner. So this make me think that pure data approach its not viable in all cases.

    – Alejandro
    5 hours ago











  • 3. I know the amount of unique code won't necessarily make it perform better, I did not express myself enough. This is really dependant of the implementation of the ECS, but in my case when a component is added or removed of an entity it iterates all systems to check if the mask of the components on it matches the system, so it add it or remove from its list. This is pretty fast for a few systems, but its not the same if you have 10 systems than if you have 500 systems and maybe even some systems just hold logic for one particular entity. This don't happen with generic systems.

    – Alejandro
    5 hours ago











  • That sounds like an implementation bug. You can do that iteration (lazily if you choose) once for each component mask and save the results, so your cost to add a component is proportionate to the number of systems that need it, not the number of systems you have. But you really don't need that many systems because 2: not everything has to be a system. Specific to your example, AI don't all need to run through one monolithic system, nor have unique bespoke systems each. A behaviour tree for the logic leveraging a small kit of shared movement/pathfinding/action components can often work well.

    – DMGregory
    4 hours ago



















2. It is arguable that an addition is just as an abstract that entities expiring. For my particular case I do have a timestamp that compares to actual time and when the entity expires is scheduled for removal, but its just in one place. It could be wrapped as you said but the feel of repeated code still there between systems and components, I mean the concept of in X time schedule removal, or even in X time do Y. I know this example was simple, but it also happens with more complex things like AI.

– Alejandro
6 hours ago





2. It is arguable that an addition is just as an abstract that entities expiring. For my particular case I do have a timestamp that compares to actual time and when the entity expires is scheduled for removal, but its just in one place. It could be wrapped as you said but the feel of repeated code still there between systems and components, I mean the concept of in X time schedule removal, or even in X time do Y. I know this example was simple, but it also happens with more complex things like AI.

– Alejandro
6 hours ago













In AI you have a lot of specific behaviour for individuals entities, and sometimes maybe just an AI for an unique entity. -If you have multiples components & systems for differents AI types, you have lot of repeated stuff in the systems. -If you have just one component specifing the AI type, you end with one huge unmaintainable system handling all AI cases. -If you do the polymorfic approach leting the AIComponent handle the specific behaviour of the AI the code got cleaner. So this make me think that pure data approach its not viable in all cases.

– Alejandro
5 hours ago





In AI you have a lot of specific behaviour for individuals entities, and sometimes maybe just an AI for an unique entity. -If you have multiples components & systems for differents AI types, you have lot of repeated stuff in the systems. -If you have just one component specifing the AI type, you end with one huge unmaintainable system handling all AI cases. -If you do the polymorfic approach leting the AIComponent handle the specific behaviour of the AI the code got cleaner. So this make me think that pure data approach its not viable in all cases.

– Alejandro
5 hours ago













3. I know the amount of unique code won't necessarily make it perform better, I did not express myself enough. This is really dependant of the implementation of the ECS, but in my case when a component is added or removed of an entity it iterates all systems to check if the mask of the components on it matches the system, so it add it or remove from its list. This is pretty fast for a few systems, but its not the same if you have 10 systems than if you have 500 systems and maybe even some systems just hold logic for one particular entity. This don't happen with generic systems.

– Alejandro
5 hours ago





3. I know the amount of unique code won't necessarily make it perform better, I did not express myself enough. This is really dependant of the implementation of the ECS, but in my case when a component is added or removed of an entity it iterates all systems to check if the mask of the components on it matches the system, so it add it or remove from its list. This is pretty fast for a few systems, but its not the same if you have 10 systems than if you have 500 systems and maybe even some systems just hold logic for one particular entity. This don't happen with generic systems.

– Alejandro
5 hours ago













That sounds like an implementation bug. You can do that iteration (lazily if you choose) once for each component mask and save the results, so your cost to add a component is proportionate to the number of systems that need it, not the number of systems you have. But you really don't need that many systems because 2: not everything has to be a system. Specific to your example, AI don't all need to run through one monolithic system, nor have unique bespoke systems each. A behaviour tree for the logic leveraging a small kit of shared movement/pathfinding/action components can often work well.

– DMGregory
4 hours ago







That sounds like an implementation bug. You can do that iteration (lazily if you choose) once for each component mask and save the results, so your cost to add a component is proportionate to the number of systems that need it, not the number of systems you have. But you really don't need that many systems because 2: not everything has to be a system. Specific to your example, AI don't all need to run through one monolithic system, nor have unique bespoke systems each. A behaviour tree for the logic leveraging a small kit of shared movement/pathfinding/action components can often work well.

– DMGregory
4 hours ago












Alejandro is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Alejandro is a new contributor. Be nice, and check out our Code of Conduct.













Alejandro is a new contributor. Be nice, and check out our Code of Conduct.












Alejandro is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Game Development Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgamedev.stackexchange.com%2fquestions%2f167058%2fshould-components-in-entity-component-system-pattern-have-logic%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

is 'sed' thread safe

How to make a Squid Proxy server?