Objects, Components, and Entities, Oh My!
A new post at last, and it’s that rarest of rare beasts, a programming post!
I’ve been reading and thinking about game system architecture lately. What I mean, specifically, is how objects and actors that take part in a game are represented in code, and what the relationships (in code) between them are like.
The traditional approach follows the Object Oriented programming model of a class hierarchy, where everything is represented by an object containing both data and functions, so that an Orc has both a Size value and an EatPeasant() function bundled up inside it. Furthermore, rather than writing each object from scratch, inheritance is used so that a tree of objects emerges, where specific actors like Orcs inherit much of their data and functionality from object higher up in the tree.
For example, an Orc may inherit its Size from a Monster class, as well as Position, which Monster in turn inherits from MovableObject, which inherits it from GameObject.
This is the naïve OOP approach to managing the actors in a game, and it is almost certainly the wrong approach, at least for a game of any appreciable size or complexity. Don’t get me wrong, it’s an intuitive way to set things up, especially for programmers with experience in other fields, and it can work very well for a small game where the hierarchy can be kept under control and doesn’t grow to massive, unmanageable proportions.
But there are better ways to do things, ways that are much cleaner once you have hundreds of different types of objects in your game, ways that are ultimately just as intuitive once you get used to them.
This is what I’ve been reading about, and pondering, in recent days: component-based architectures and entity systems for representing and managing the actors in your game. While I explain the concepts involved below, if you're really interested in the subject I recommend Mick West's article on the subject, which is very well written and goes into considerable detail.
At its most basic, a component-based architecture eschews a complex object hierarchy in favor of every actor in the game being a collection of components that define what that actor is, and what that actor does. Each actor then, apart from perhaps having some extremely common data such as an identifier, holds a list of components like PhysicsComponent, InputControllable, IsMonster, or whatever else is necessary to define all the operations that the actor can undergo.
These components then hold the relevant data and functions that define the actor, while centralized game systems either iterate over the actors, or even hold their own references to the individual actors’ components, and are able to do whatever updating or rendering the component requires every frame.
The benefits to this kind of setup are many. For one thing, instead of having perhaps hundreds of separate inherited objects, each necessitating its own class with all the upkeep that this entails, actors are simply the sum of their components, and any time a new component is written by a programmer, it can not only very easily be used to create new actors, but can also be trivially added to actors that already exist, giving them new functionality.
This means that with the proper tools, designers can easily redefine behavior on any actor simply by changing what components make up that actor, or tweaking the data that component manages; the only limitation is what components are available. Writing an individual component is also simpler and easier to test than modifying a fully realized, complex, working object that is being passed around throughout the game.
As an example, our Orc from before would be defined by the components that give it a size, and that allow it to eat peasants. If we then decide that Orcs should fly, we can give it a flying component, one which we might already be using for birds, or that we can subsequently use for whatever other actors we think should fly.
It also becomes trivially easy to make other Orc-like monsters, by simply swapping out a couple of components, or simply initializing the new monsters components with different data. For instance, a Goblin monster may be functionally identical to the Orc, but its SpriteComponent loads Goblin.jpg instead of Orc.jpg.
A component system within the confines of an object-oriented language like C++ or C# is all well and good, but what I found particularly interesting while reading on this subject was a method used by Adam Martin, to do away with the pesky and ill-fitting OOP paradigm entirely while designing this sort of component-based architecture.
Essentially, Adam proposes that the actors in a game (which he call entities, a common term that I have not used up until now specifically because he calls his method an Entity System) and their components should be approached using a database model.
Instead of even having an actor class which holds a list of component objects, he advocates divorcing the data and functionality of entities entirely from the traditional OOP model. An entity then becomes nothing more than a unique identifier, a number or a string, and components become packets of data needed by that entity.
Separate systems operate on the data in a component, for instance, a PhysicsSystem might take the data in a PhysicsComponent and use it modify the data in the entity’s PositionComponent, but components themselves merely hold data, not functionality.
The cool thing about this, though it took me a while to make sense of it, is that instead of passing around complex class-based objects in your code, you can simply hold onto an ID for the entity you want, and then essentially perform database-style queries to obtain any data you currently need from the components bound to that ID.
Not only do you get all the benefits of the popular and powerful component architecture, but you also keep your data separate from functionality which comes with a number of benefits; for example, it allows you to perform parallel operations easily on different entities (which are just sets of data) without breaking anything.
I feel like my thoughts about this database-like view of a component system are not yet fully formed, but I definitely feel like there is a powerful elegance to doing things this way. I will need to let the thoughts simmer some more before I have more to say about it, I think.
I recommend checking out Adam’s series of articles on the subject, linked above. It really is quite eye-opening.
May 28th, 2010 - 16:05
Go digging around in the raw files for dwarf fortress. It seems like more of an entity system is at use there, with each actor type (dwarf, elf, human, magmaman, whatever) having an entry. They then have several flags which dictate common traits. It might be interesting to learn a thing or two from the setup.
Individual dwarfs at the same time have a name (their identifier), and their own traits which are randomly selected from a set of traits available to their entity, but not necessarily common to all dwarfs. So, you never end up with a dwarf who can fly, but you do end up with a dwarf that likes cows for their haunting moos and hates his neighbor Urist McJackass because he keeps a pet rat.
The real fun comes from giving the user a way to leverage this system to their advantage, or even actively manipulate it. Again to the dwarf fortress example, you can draft dwarfs into the military who have the appropriate traits (toughness, lack of empathy, strength) to create a military juggernaut that clears the fortress of all threats swiftly. At the same time, it is just as much fun to give the clumsy dwarf with poor eyesight and spatial sense an adamantine battle axe and have him spar with all the dwarfs you deem “unfit” for your fortress. As an added bonus, it gives your surgeons something to do!
December 21st, 2010 - 01:24
I used to love reading your blog, but lately it’s been a little boring. I’ll still read it though =)
December 22nd, 2010 - 14:38
I must say, as substantially as I enjoyed reading what you had to say, I couldnt help but lose interest after a while. Its as if you had a great grasp to the topic matter, but you forgot to include your readers. Perhaps you should think about this from additional than one angle. Or maybe you shouldnt generalise so much. Its better if you think about what others may have to say instead of just going for a gut reaction to the topic. Think about adjusting your very own thought process and giving others who may read this the benefit of the doubt.
December 22nd, 2010 - 14:48
Pretty insightful publish. Never thought that it was this simple after all. I had spent a very good deal of my time looking for someone to explain this topic clearly and you’re the only 1 that ever did that. Kudos to you! Keep it up
December 22nd, 2010 - 22:14
Thank you for the wise critique. Me & my neighbour were preparing to do some research about that. We obtained a great book on that matter from our local library and most books where not as influensive as your info. I am extremely glad to see these information and facts which I was searching for a lengthy time.
December 23rd, 2010 - 07:33
Pretty good post. I just stumbled upon your site and wanted to saythat I’ve really liked browsing your posts. Anyway I’ll be subscribing to your blog and I hope you write again soon!
December 23rd, 2010 - 14:12
Pretty insightful publish. Never thought that it was this simple after all. I had spent a excellent deal of my time looking for someone to explain this subject clearly and you’re the only 1 that ever did that. Kudos to you! Keep it up
December 23rd, 2010 - 14:19
Thank you for an additional wonderful write-up. Where else could anyone get that kind of information in these a perfect way of writing? I’ve a presentation next week, and I’m to the look for this kind of data.
December 23rd, 2010 - 14:22
This was a really really good post. In theory I’d wish to publish like this also – getting time and actual effort to make a good piece of writing… but what can I say… I procrastinate alot and by no means appear to obtain anything done.
December 24th, 2010 - 13:26
This was a really very superior post. In theory I’d like to create like this also – getting time and actual effort to make a good piece of writing… but what can I say… I procrastinate alot and by no means appear to obtain some thing done.
December 24th, 2010 - 13:33
Thank you for that smart critique. Me & my neighbour were preparing to do some research about that. We got a very good book on that matter from our local library and most books where not as influensive as your information. I am extremely glad to see such facts which I was searching for a lengthy time.
December 24th, 2010 - 13:34
Thank you for an additional good post. Where else could anyone get that kind of info in these a perfect way of writing? I have a presentation next week, and I’m to the look for such data.
December 25th, 2010 - 12:27
Ive been meaning to read this and just never got a chance. Its an issue that Im pretty interested in, I just started reading and Im glad I did. Youre a terrific blogger, 1 of the greatest that Ive seen. This blog unquestionably has some information on subject that I just wasnt aware of. Thanks for bringing this stuff to light.
December 25th, 2010 - 12:45
Youre so right. Im there with you. Your weblog is certainly worth a read if anyone comes throughout it. Im lucky I did because now Ive obtained a whole new view of this. I didnt realise that this issue was so important and so universal. You absolutely put it in perspective for me.
December 25th, 2010 - 23:23
I’ve been reading a few posts and genuinely and enjoy your writing. I’m just starting up my own blog and only hope that I can write as well and give the reader so much insight.
December 26th, 2010 - 15:11
First of all, allow my family recognize a person’s command during this matter. Even though this is certainly brand new , nevertheless soon after registering your site, this intellect has exploded extensively. Allow all of us to take hold of one’s rss to help keep in touch with at all probable messages Sincere understand but will pass it on to help admirers and my particular are living members
December 26th, 2010 - 15:11
How is it that just anybody can publish a blog and get as popular as this? Its not like youve said something incredibly impressive –more like youve painted a pretty picture more than an issue that you know nothing about! I dont want to sound mean, here. But do you definitely think that you can get away with adding some fairly pictures and not really say anything?
December 27th, 2010 - 00:40
Great to be visiting your weblog again, it has been months for me. Nicely this article that i’ve been waited for so long. I need this article to total my assignment inside the school, and it has exact same subject with your post. Thanks, good share.
December 27th, 2010 - 06:14
Of course, what a great site and informative posts, I will add backlink – bookmark this site? Regards,
Reader.
December 27th, 2010 - 13:35
I mistyped this website and luckily I found it again. presently am at my university I added this to favorites so that I can re-read it later regards
December 27th, 2010 - 13:45
Nice to become visiting your weblog again, it continues to be months for me. Nicely this post that i’ve been waited for so lengthy. I want this write-up to total my assignment inside the university, and it has exact same topic with your write-up. Thanks, excellent share.
December 27th, 2010 - 13:50
Youre so right. Im there with you. Your weblog is absolutely worth a read if anyone comes across it. Im lucky I did because now Ive obtained a whole new view of this. I didnt realise that this issue was so important and so universal. You certainly put it in perspective for me.
December 27th, 2010 - 19:08
I would prefer to thank you for that efforts you might have produced in writing this write-up. I am hoping the exact same finest operate from you within the future too. In fact your creative writing skills has inspired me to start my own BlogEngine blog now.
December 28th, 2010 - 06:08
A thoughtful insight and ideas I will use on my website. You’ve obviously spent a lot of time on this. Congratulations!
December 28th, 2010 - 13:52
I just couldnt leave your website before letting you know that I really enjoyed the useful information you offer to your visitors… Will be back often to check up on new stuff you post!
December 29th, 2010 - 15:09
I admire the useful info you provide inside your content articles. I will bookmark your weblog and also have my children check up right here usually. I am very positive they’ll learn lots of new stuff here than anybody else!
December 29th, 2010 - 15:14
What I wouldnt give to have a debate with you about this. You just say so many things that come from nowhere that Im fairly sure Id have a fair shot. Your blog is terrific visually, I mean people wont be bored. But others who can see past the videos and the layout wont be so impressed with your generic understanding of this topic.
December 29th, 2010 - 15:16
Thank you for another fantastic write-up. Where else could anybody get that type of information in these a ideal way of writing? I have a presentation subsequent week, and I am on the look for this kind of details.
December 29th, 2010 - 15:57
Hey – nice blog, just looking about some blogs, appears a pretty nice platform you are making use of. I’m currently utilizing Wordpress for a few of my websites but looking to change 1 of them around to a platform comparable to yours like a trial run. Anything in particular you’d recommend about it?
December 29th, 2010 - 18:18
I’d prefer to thank you for that efforts you’ve produced in writing this post. I’m hoping the same very best perform from you inside the potential as well. In reality your inventive writing abilities has inspired me to start my personal BlogEngine blog now.
December 30th, 2010 - 00:59
I discovered your blog site on google and check a few of your early posts. Continue to keep up the very good operate. I just additional up your RSS feed to my MSN News Reader. Seeking forward to reading more from you later on!?
December 30th, 2010 - 07:04
You made some good points there. I did a search on the topic and found most people will agree with
your blog.
December 30th, 2010 - 10:39
Ernest Hemingway~ Theres absolutely nothing noble in becoming superior to your fellow men. True the aristocracy is becoming superior to your former self.
December 30th, 2010 - 14:02
Probably a little bit of a unusual response, however I like the actual theme you use and My spouse and i definitely wanted to inform you! Having a few fine articles this weblog is really great
December 30th, 2010 - 14:45
Just a fast hello and also to thank you for discussing your ideas on this web page. I wound up inside your weblog right after researching physical fitness connected issues on Yahoo… guess I lost track of what I had been performing! Anyway I’ll be back once again within the long term to check out your blogposts down the road. Thanks!
December 30th, 2010 - 15:09
I’m happy I found this blog, I couldnt uncover any information on this subject matter prior to. I also run a site and if you want to ever serious in a little bit of guest writing for me if possible really feel free to let me know, i’m always look for people to check out my site. Please stop by and leave a comment sometime!
December 30th, 2010 - 15:12
Resources such as the one you mentioned right here will be extremely useful to myself! I’ll publish a hyperlink to this page on my particular weblog. I’m certain my site visitors will discover that quite helpful.
December 30th, 2010 - 16:26
If just more individuals can see it the following way. Would likely help make things considerably better for certain!
December 30th, 2010 - 18:11
Resources such as the one you mentioned here will be incredibly helpful to myself! I will publish a hyperlink to this web page on my individual weblog. I’m certain my site website visitors will discover that quite valuable.
December 30th, 2010 - 18:56
Perhaps a little bit of a weird respond, but I love the template you use and I definitely wanted to inform you! Having quite a few fine articles this blog site is definitely excellent
December 30th, 2010 - 19:08
This was probably just right! I possibly could not have done this much better by any means haha. Dislike to admit that though
December 30th, 2010 - 19:52
sE, fatcow fcweef
December 31st, 2010 - 00:41
A lot of of what you articulate is supprisingly accurate and it makes me wonder the reason why I hadn’t looked at this with this light previously. This particular piece really did turn the light on for me as far as this particular issue goes. But at this time there is actually 1 issue I am not necessarily too comfy with so while I attempt to reconcile that with the actual central idea of your point, let me see exactly what the rest of the subscribers have to point out.Nicely done.
December 31st, 2010 - 16:16
I’m happy I found this weblog, I couldnt find out any data on this subject matter prior to. I also run a site and if you want to ever serious in a little bit of guest writing for me if possible feel free to let me know, i’m always look for people to test out my site. Please stop by and leave a comment sometime!
January 1st, 2011 - 20:25
Please tell me that youre heading to keep this up! Its so excellent and so important. I cant wait to read more from you. I just really feel like you know so much and know how to make people listen to what you’ve to say. This blog is just too cool to be missed. Fantastic things, genuinely. Please, PLEASE keep it up!
January 2nd, 2011 - 12:48
Intellitechs Computer Repair 4200 south Valley View, Las Vegas, NV 89103 (702) 582-6767
January 2nd, 2011 - 17:11
The new Zune browser is surprisingly good, but not as good as the iPod’s. It works well, but isn’t as fast as Safari, and has a clunkier interface. If you occasionally plan on using the web browser that’s not an issue, but if you’re planning to browse the web alot from your PMP then the iPod’s larger screen and better browser may be important.
January 2nd, 2011 - 17:22
Congratulations on having one of the most sophisticated blogs Ive arrive across in some time! Its just incredible how significantly you can take away from a little something simply because of how visually beautiful it’s. Youve put together a terrific blog space –great graphics, videos, layout. This is certainly a must-see weblog!
January 3rd, 2011 - 04:45
The new Zune browser is surprisingly good, but not as good as the iPod’s. It works well, but isn’t as fast as Safari, and has a clunkier interface. If you occasionally plan on using the web browser that’s not an issue, but if you’re planning to browse the web alot from your PMP then the iPod’s larger screen and better browser may be important.
January 3rd, 2011 - 16:38
Ive been meaning to read this and just never acquired a chance. Its an issue that Im extremely interested in, I just started reading and Im glad I did. Youre a excellent blogger, 1 of the very best that Ive seen. This blog absolutely has some facts on topic that I just wasnt aware of. Thanks for bringing this stuff to light.