Monday, November 24, 2014

Component Based Programmin'

Like every good Game Dev student interested in low level graphics I started writing a small game engine for my Data Structures and Algorithms for Games and Simulations class. I've learned a lot but what I wanted to write about was the magic of writing a component based system.

There's a reason Unity uses a component system for it's engine. It's elegant and modular; intuitive and powerful. It's a really great programming paradigm whether you're writing something simple or complex. Needless to say that actually writing a system like Unity is far from easy, but it's also really rewarding.

The way my engine is structured is that there are GameObjects and Components. GameObjects store Components, and Components store a reference to their parent GameObject. This creates the first architectural problem I ran into; making sure that there's no recursive header includes can be a bit tricky when the GameObject needs to include every derivation of Component.

From there I can have all the logic of the game modularized into core components and drastically cut down on the complexity of the architecture. Each GameObject just runs the Start, Update and Draw functions of its components. As an end user all I'm really doing is changing the core values of these core components and I can make some neat stuff appear on screen.

The big upside to all of this is that it makes it really easy to add scripting to a system like this. After some more core functionality gets sorted out an the game loop is modernized a bit I'm hoping to add Lua or Python scripting support.


No comments:

Post a Comment