Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by spectre1989 · Mar 21, 2014 at 03:03 PM · componentsystem

Entity-Component-System

I come from an OOP background, and I'm trying to make a proper effort to write code that really embraces the ECS design pattern. Something that I've been reading is that Components are meant to be just data, and Systems run the code that acts on Components. Should I really be separating the code from the data like this in Unity? If so, how do people recommend I go about creating a System?

Cheers Joe

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wuzseen · Mar 21, 2014 at 03:10 PM 1
Share

ECS is the paradigm in Unity (sort of), but ECS is still OOP. It's just an architecture inside. Ins$$anonymous$$d of using inheritance for everything, it favors using composition when possible.

Separating code from data isn't a bad idea per se, what do you want to do? You should usually want to decouple stuff like that as it is so you have the smallest list of dependencies as possible. Your game shouldn't depend on data, but it should use it.

avatar image spectre1989 · Mar 21, 2014 at 03:27 PM 0
Share

I was reading the t-machine article on ECS http://t-machine.org/index.php/2007/11/11/entity-systems-are-the-future-of-mmog-development-part-2/ in which he says "Common misconception #1 – Entity Systems are part of OOP", it seems like everyone has their own interpretation of the design pattern with slight differences.

avatar image Wuzseen · Mar 21, 2014 at 03:42 PM 0
Share

That's an interesting article! Thanks for sharing it. $$anonymous$$y response was less about it being a part of OOP (as an architecture) though. ECS is virtually impossible without OOP code. The OOP paradigm he talks about is just an 'old' way of doing things. It used to be right to subclass everything. Some people call that the OOP paradigm. I've never been a fan of the term as it's very confusing when you're talking about what is literally OOP code (ECS would definitely be), and what is an OOP architecture. TonyLi gets to the heart of question quite well though.

avatar image rakkarage · Mar 21, 2014 at 03:50 PM 0
Share

http://strangeioc.github.io/strangeioc/exec.html

avatar image spectre1989 · Mar 21, 2014 at 03:55 PM 0
Share

@Wuzseen yeah I think I get what you mean, a lot of people have been championing composition over inheritance for years, and as you say when I inherit from $$anonymous$$onoBehaviour I'm taking advantage of OOP program$$anonymous$$g.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by TonyLi · Mar 21, 2014 at 03:15 PM

I don't think that the writer intended to imply that components are just data. Rather, each component addresses a very specific, narrow concern, such as "Hit Points" or "Navigation".

In an OOP approach, you might have a parent class that defines Hit Points, Navigation, and a slew of other concerns.

In a component-based approach, each concern is a separate class. This allows you to compose an entity of exactly what it needs. For example, an Orc would be composed of Hit Points and Navigation, but a Wall would only be composed of Hit Points since it's stationary and never needs to navigate anywhere.

Another advantage of component-based design is that each component is small and focused, so it's easy to write and maintain, its purpose is very clear, and it's decoupled from other code.

But to answer your question: No, don't separate code from data. Separate concerns. Your Hit Points component would have data (current and max hit points) and code to manipulate that data, but nothing else.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image spectre1989 · Mar 21, 2014 at 03:51 PM 0
Share

I actually did intend to imply that, in my reading about ECS on t-machine he says:

Nor do Components contain methods. Ins$$anonymous$$d, you have an external system for each aspect, and that external system contains all the methods that can be invoked on any Entity that possess the Component that marks it as being compatible with this system.

As I said in reply to a comment on my question, I think there's just many different interpretations to ECS with subtle variations. I have no problem putting code in a Component, it's what I've been doing up until now, just wanted to make sure that I should be doing that.

avatar image TonyLi · Mar 21, 2014 at 11:51 PM 0
Share

Sorry, I meant $$anonymous$$$$anonymous$$artin on t-machine. But you're right -- even he writes that there isn't a consensus on the terms. If I recall correctly, Jason Gregory, in Game Engine Architecture, discusses a very data-driven approach to component-based design, where entities are basically just tables of data. But, given how $$anonymous$$onoBehaviours are designed, it seems to me that Unity was designed with the idea that a component should have both code and data related to a specific concern (e.g., Nav$$anonymous$$eshAgent for navigation).

avatar image spectre1989 · Mar 24, 2014 at 10:09 AM 0
Share

Ahhh I see sorry. Awesome, I haven't got to that part of GEA, I really ought to finish reading it. I suppose if you REALLY want to do this with $$anonymous$$onoBehaviours, you could just put the code in a second $$anonymous$$onoBehaviour. In particular I was thinking about trying to put any code that requires two different component types, in a $$anonymous$$onoBehaviour on its own. $$anonymous$$g. I have a bullet component with damage, and an NPC component with health. Then a third script on the bullet which listens for the Bullet script to fire off a "I hit something" event, at which point it looks for the NPC component on the thing that was hit, and applies the damage if it was found.

avatar image
7

Answer by sebas77 · Oct 05, 2015 at 05:51 PM

Since I recently blogged about the argument, I'd like to add my answer. The question is old, but still relevant for other people. I can see the confusion. As mentioned in the previous answer, Unity doesn't really implemented an Entity Component System design, at least not the one designed by Adam Martin.

Unity framework design should be called Entity Component, since it misses the concept of "System". Systems in Unity cannot be designed by the programmer, but they are part of the Engine itself, so they are not extensible.

All the logic in Unity should be written inside focused MonoBehaviour. Every MonoBehaviour should have just one functionality, or responsibility, and they shouldn't operate outside the gameobject itself. They should be written with modularity in mind, in such a way they can be reused independently on several GameObjects. Monobehaviours also hold Data. The design obviously follows the basic concepts of OOP.

Modern design tends instead to separate data from logic. See for example the MVC or MVP patterns. Data, Views and Logic should be separated to achieve a better code modularity.

The real problem about Unity Entity Component design is that it's impossible to put two entities in communication in a reasonable way that doesn't lead to use various Anti-Pattern.

Unity inverts the control of the creation of the entities, thus the coder doesn't have any control on the creation of them. Inverting the creation control is a good thing, but Unity doesn't do it in a proper way. Not being able to inject dependencies in the Entities is a limitation that forces coders to use many anti-patterns in order to overcome all the intrinsics limitations. Basically the cleanest option is to use Singletons in order to share information between entities.

That's why it's quite hard to manage and maintain big projects with Unity. I wrote a lot about these problematics in my blog. So if you want you can continue reading my thoughts there:

  • http://www.sebaslab.com/ioc-container-for-unity3d-part-1/

  • http://www.sebaslab.com/ioc-container-for-unity3d-part-2/

  • http://www.sebaslab.com/the-truth-behind-inversion-of-control-part-i-dependency-injection/

  • http://www.sebaslab.com/the-truth-behind-inversion-of-control-part-ii-inversion-of-control/

  • http://www.sebaslab.com/the-truth-behind-inversion-of-control-part-iii-entity-component-systems/

  • http://www.sebaslab.com/the-truth-behind-inversion-of-control-part-iv-dependency-inversion-principle/

  • http://www.sebaslab.com/the-truth-behind-inversion-of-control-part-v-drifting-away-from-ioc-containers/

Svelto ECS has now been released : http://www.sebaslab.com/ecs-1-0/

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sebas77 · May 24, 2016 at 05:30 PM 0
Share

Svelto ECS has now been released: http://www.sebaslab.com/ecs-1-0/

avatar image dirkraft · Jul 09, 2016 at 07:23 PM 0
Share

Just wanted to say thank you for recording your thoughts. "Not being able to inject dependencies in the Entities is a limitation that forces coders to use many anti-patterns in order to overcome all the intrinsics limitations." Seeing you say that validates a lot of my frustrations with writing what I consider clean, organized OOP code. I want to check out ECS when I find the time.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Question about new Component system 2 Answers

2D Animation does not start 1 Answer

Access all current Collisions 0 Answers

[ECS] Create Entities from Job ( IJobForEachWithEntity) using an EntityCommandBuffer 1 Answer

Legacy particle system doesn't work on Unity 4.0 6 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges