Before I start I just want to make a public service announcement regarding this and all future posts. Since I am in my 3rd year of game development, I have decided that in order to help myself master the techniques, my blogs will be purely directed toward me attempting (being the operative word) to teach anyone who reads this, the concept in question.
Having that out of the way! Lets begin, so after much consideration I have decided to direct this blog towards the concept of component, entities and systems. These are vital parts of a game and to be honest before I was not directing my attention towards these things, which was a mistake on my part.
Now if you dont know what components, entities,and systems are then you no doubt have no idea what I am talking about. Using a few diagrams I hope that once you reach the end of this post you will at least have some small grasp on this concept, and if you are in the game development industry or in school for it, hopefully this can help you direct your games/projects in the right direction and save yourself some time!
Entities
Lets begin with entities; when you think of the word "entity" what comes to mind first? Well for me I see a living creature, with its own specific characteristics and actions, something unique. The same is true in gaming terms. An entity is literally a thing, an empty box waiting for you to give it components. Once you have given it components your entity becomes unique, its own thing separate from other empty boxes. Now dont worry if you do not know what a component is, because we will get there, for now just hold the concept in your head that empty entity + component = unique entity.
Great! Lets move to some quick diagrams and examples for entities to really smash the idea home.
The first and most common example is to think about a key. What does a key do? Unlock things, right? Great now think of a key with no prongs on it, just a flat piece of metal, now if every key was like that, then any key could open every lock, which wouldnt be good at all.
Now I want you to hold that idea of a flat key in your head through this entire post, because we will be adding things to it! For now though think of it without any prongs, something with no definition, just like all the other prong-less keys.
Components
Now that we know about entities, the next topic is components.
What do you think of when you hear the word component? I think of something that goes in, or fits in something else. A piece of a puzzle for example would be a component of the entire puzzle. Personally I think of a component as a page of the book. Each page is a different component of the book, if you take one page out, it changes the book completely right?
Easy enough right? A component is a part of something else, its only purpose is to be added to something else to make it more, or add functionality to it. Another quick example would be leather seats in a car. By default cars come with fabric seats (a component of the car) but you can take that component out, and add in a new component leather seats. Does the car change? Of course it does, and all you did was swap something for something else.... Neat isnt it?
Okay lets take that blank key you were thinking about. Remember the flat prong-less key that is just like every other key.
Hold that in your mind, now think of individual prongs, of different sizes, these are the components to our key. Now add certain components (different sized prongs) along the edge of your flat key. Now you have a key that is unique, unlike the others. Neat right? And all you did was add things to the key, you didnt change the key itself you just gave it properties, and characteristics.
Entities + Components
So now we have a key with prongs, cool! Now this is the part where some people might have trouble understanding. Lets apply this to something to do with game dev!
Think of our flat prong-less key as a character for example, generic and not interesting. No health bar no skills no nothing. Just a blank character with no features, no color.
Alright so we have our generic character. Now lets think of our prongs as attributes we can add to that character. Think of health, think of color, think of features. Think of a position in our game world. Think of our running speed that the character travels at. Think of a texture to give the character an outfit to wear.
If you add these individual components to our character (health, speed, texture, position) you will end up with an entity that has components making it unique, you will end up with a character that can be placed in the world, colored and have the ability to use the character in any way you want!
Sounds easy doesnt it? Well it is, its the simple concept of making an entity that can take components to flesh out the character, equipment or prop you want. YES that is right an entity isnt only a character it can even be a prop like a sword!
Take a moment and think of a sword entity, what components would you have in it? Once youre done please keep that in mind and comment on the blog which components you thought up! I would love to hear them.
Alright now back on topic, take the character entity we made together a few minutes ago. Remember that, we now have our character with components as we move to the last part of this tutorial.
Systems
Now lets get to systems, a brand new concept for us!
Remember how before we were using the concept of a flat generic key, then we moved to a key that had prongs on it? Now lets apply that to the concept of locks, each lock fits a certain type of key, but like everyone knows you can duplicate keys so that they all fit the same lock.
The exact same concept is true in game development with systems. Think of it this way, we have a system like a car starter. What does it do? Starts the car at the turn of a key. Now the key has prongs, which are the keys components (lets not put names to them just yet.) and they allow the car to be turned on. Now if you broke one of those prongs would the key work? No it wouldnt.
Now that we have that down, most cars have more than one key just in case you lose one correct? Great so we have two keys that fit the same lock, but no matter what the lock will always start the car if a key with the correct components are placed into it.
Lets put it in terms of game development now.
Lets use a basic system such as movement, this system is a lock, which requires a key (entity) to have certain prongs (components) to operate it.
The movement system will take in ANY entity with the position, and speed components, now when you turn the key in the movement system's lock, what happens is the system moves the entity (key) forward. That is the concept and the application of entities components and systems!
Pictures!
Just to make it easier for everyone to understand (because this is a difficult topic) I will include some pictures of exactly what I mean and hopefully it will help you understand!
In the above picture, we have three entities, with components in each. The Tree entity, has its own renderer(component), the tank entity has Renderer physics armor and AI components and the ninja has the renderer, physics health and stealth components.
Each of these entities have their components because using these components they can be added into a system.
Example:
There is a system called render, and it only requires an entity with one component (renderer) if an entity has this component then it can use the render system.
In the above case all three of the entities could use this system! You know what that means? You just rendered three things with one piece of code, THREE different things mind you.
That is the concept and purpose of entities, components and systems!
Here is one last picture just to put home the entity (key) and components (prongs) idea!
See the key? Awesome! That means you get it!
Thank you so much for viewing my blog, and I do hope that you understood everything, like I said, I am not a qualified teacher I am just trying to teach myself by teaching others. If this helped you please post a comment I would love to hear your feedback!
Thanks everyone for your time!
Bye for now!