Entity Systems

Object oriented game engines got you down? Do you find yourself writing redundant code? Is your project becoming increasingly unmaintainable with every bit of additional functionality? Let an entity system engine help!

Jump here to see my entity system engine.

ES Javascript Excerpt

ES Javascript Excerpt

While an “entity system” (ES) has broader applications than game engines, I am only going to focus on the game engine side of things. An entity system fundamentally differs from an object oriented model of programming. Instead of a collection of instantiated objects that have their own variables and methods, an entity system contains entities that are assigned components, and acted upon by a system. Entities are simply an ID. In my ES implementation, they are simply integers. Components are tags, you can imagine them as labels such as “renderable”, “hasPhysics”, or “takesKeyboardInput”. Systems have the code to act upon relevant entities and components (sometimes more than one component).

One of the big advantages of ES engines is their flexibility. Adding completely unforeseen capabilities to a given entity can be accomplished in as little as one line of code. Additionally, new systems can be added without disrupting old functionality. By its very nature, there is no class inheritance to worry about breaking (or breaking out of). As I understand it, entity systems also scale fairly well to large MMO/MMORPG games, especially since server-side databases are needed to hold this information even when the player isn’t playing.

The ES programming pattern drastically cleans up the “game loop” as it were. In case you are out of the loop (aha), the game loop repeats tasks that must be carried out every game “tick”. This usually includes rendering, handling user input, updating physics, handling collisions, calculating an A.I.’s next move, etc. For an ES, this game loop becomes a simple loop through each system in the game. Each system does something different, whether that be rendering, calculating movement, or physics, etc.

Let’s take the “renderable” system for example. When it comes time in the game loop for this system to get to work, it grabs a list of entities with relevant tags/components (the “renderable” component in this case). It then updates a canvas object with the shape/graphic associated with that component. When it is done, the loop continues on to the next system, which repeats the process. When I was implementing my ES engine, I found that an ideal data structure for storing this information would be a doubly-indexed table. One indexes first on entity ID, and then by component assigned to the entity. The information relevant to each entity is then stored via a pointer in this table. Indexing first by entity ID is great for grabbing a handle to a certain entity (perhaps for collision detection), but it is also beneficial to index by component first, and then by entity ID. This allows systems to easily grab the entities they need to do work on.

It is interesting to note that there is a general incompatibility between the object oriented thought process and an entity system. This is the same problem that relational databases suffer from. It takes a different mindset to comprehend them (since most of us are steeped in object oriented traditions), but the result can be especially powerful in many situations.

My own entity system engine is available for your scrutiny here. It is written in Javascript, using the HTML 5 canvas element. So if you cannot view it, get a newer browser! (works with Chrome and the later versions of Firefox) This is meant to be merely a proof-of-concept. It’s nothing fancy. Feel free to use the code (please learn something from it), but many optimizations need to be made that I did not have time to do.

This entry was posted in Games, Javascript. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>