Saturday, November 19, 2011

Entity System.

After reading T = Machine's post about Entity Systems, I started looking more into the matter, the reason for this is because I was getting tired of always doing the same hierarchy system (Object Oriented Programming), specially since at some points of the programming I would find myself with more than 20 (for me that's a lot) virtual functions that had nothing to do with the last object created.

with this being said I started looking into a different way of doing a simple game engine that would allow me to make new types of objects without having to mess with base classes that much, but that they can still relate from similar objects.

I heard that Unity used a component system, which encapsulates implementation inside a component and runs different behaviors from there, this seemed promising and I spend a good amount of time looking into it, but just to find out that at the end you would end up with too many components that did a lot, maybe a lot of what you didn't want them to do in an specific object, like for example, if we had an skeleton enemy object, which had a running component, but we wanted our skeleton to break apart if it went running too fast, then inside this component we would have to make sure that the skeleton breaks if it goes too fast, but if we do that, the zombie enemy object which has the same running (yes running zombies) component, now it will be unnecessarily checking if it is going too fast, now, I know this can be fixed by simply not doing that and adding a breakable component which checks for the speed of the skeleton, given, quick fix, but now you have a component that's 100% specific to the skeleton (unless you want more breakables?), this leads to design decisions, and when we draw the "too much" line.

This type of component system does fix a lot of the issues that come from object oriented programming, and still allowing you to be flexible about your implementation, I wasn't happy with it entirely, but it was fun to experiment with.

Following my obsession (yes, I have a coding problem), I stumble upon T = Machine's blog (http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/) where he explained more about this entities systems that used Data driven components, I asked myself... "What the hell is this?", well, in this articles he explains that Entities are nothing but a simple label, in fact, they are almost nothing, and the components he speaks about, they have absolutely no relevant implementation other than encapsulation of data, in an essence, they are just data, information, variables, nothing else.

This was extremely interesting and I soon found some examples about this, and it seems I have been under a rock for the longest time, because these systems are kind of old, at least from the 2000's, now... even though they are known they seems to be looked with.. well.. skepticism about their capabilities, this is my personal notion from what I read online.

I decided to give it a go, and after much struggle with the design, since I am really used to OOP, I was able to make an small impure Entity System (impure because I made entities as their own objects... instead of just labels, I just could not help myself), even so, I must say I am impressed with the results, it was so flexible that I could literally grab any components, put them together, change some data inside and I had a totally different object.

Even though I found a lot of examples on how this might work, I did some simple things to make it work, this is how my main class works (XNA, C#):

public class World
{
#region fields

EntityFactory m_EntityFactory;

List<basesystem> m_DrawingSystems;
List<basesystem> m_UpdatingSystems;
List<basesystem> m_InputSystems;


public World(SpriteBatch spriteBatch, ContentManager content)
{
  m_EntityFactory = EntityFactory.Initialize();
  m_DrawingSystems = new List<basesystem>();
  m_InputSystems = new List<basesystem>();
  m_UpdatingSystems = new List<basesystem>();

  TexturePool.Initialize(content);

  Draw2DSystem testDrawer = new Draw2DSystem(spriteBatch);

  testDrawer.AddEntity(EntityFactory.Player());
  m_DrawingSystems.Add(testDrawer);
}

#endregion



#region methods
public void Input()
{

}

public void Update()
{

}

public void Draw(SpriteBatch spriteBatch)
{
   for (int i = 0; i < m_DrawingSystems.Count; i++)            
   {
        m_DrawingSystems[i].Process();
   }
}
#endregion
}



as for now, I just have a simple sprite being drawn on the screen, from this chunk of code I hope you can see some of the things going on under.

Well, as I continue experimenting with this (as well as learning how to make proper posts in my blog) I will be around.

Thanks for reading!

No comments:

Post a Comment