= Entity System Introduction
:revnumber: 2.0
:revdate: 2020/07/25


Every experienced programmer knows that planning a huge project is difficult.
You need to know exactly what you want your program to do and which
things should be possible to add later.
If you do not do this you get unclean code, lose the clear view over the project
and have to rewrite large parts again.
Because game development is a dynamic process, sometimes it can be difficult to
decide in which direction the game will develop)
(Maybe you already compared alpha screenshots of a game with the final result)


image::http://media.moddb.com/images/articles/1/116/115301/auto/shjWa.png[shjWa.png,width="",height="",align="center"]

In this example we suddenly want a new unit, an armored robot.
This is not possible with our structure and therefore we would need to rewrite the code.
At this point, the idea for an Entity System starts.


== Entity System

There are various concepts for Entity Systems and they all have some differences.
In this article we concentrate on the way the jMonkey Entity System works.


=== Entity

An Entity is simply an unique Id.
Every Unit, Item, Bullet, etc. in your game will be represented by one of these entity Ids.


=== Component

A Component is a class which only contains data.
This means it has a constructor and *only* getter methods.
Examples for Components can be:

*  PositionComponent
*  MovementComponent
*  CollisionComponent

Components are added to the Entities.

[IMPORTANT]
====
 At any particular time, there can be only one component of the same class for one entity.
====


As you can see Entities are flexibly made up of different Components.
You need your Armored Robot? No Problem, you only need to combine the right components.
Besides, you are able to remove/change/add components during the game which is a huge benefit of Entity Systems.


image::http://media.moddb.com/images/articles/1/116/115301/auto/0WoEb.png[0WoEb.png,width="",height="",align="center"]



=== Systems

Simply put, any classes other than the components and the entities can be considered as systems.
You need to pay attention when you read about other Entity Systems
because they can have different ideas of what a system is.

In the systems, you can request all Entities which have special components.
For example you can say that you want all entities who have a position and a movement component,
and then calculate the new position for these Entities by overwriting the old position component with a new one.

This leads automatically to many classes that only perform a single task, like the movement.
As a result the project becomes clear and in case of bugs, you directly know which class is responsible for these components.

For a more detailed and technical view, have a look at the xref:es/entitysystem/advanced.adoc[Entity System Advanced] article


=== Pros and Cons

Pro:

*  good overview
*  flexibility
*  modularity

Con:

*  performance
*  unfamiliar
*  a bit risky

As mentioned above, the System has many benefits.  Now lets focus on the cons:
The performance is slightly slower because you have more classes and more calculations are needed in the background.
This point is not so significant anymore because computers are becoming faster and faster.
A more critical point is that, until now, only a few projects have employed entity systems. One example of a project which uses a similar Entity System is the adventure Mythruna.
link:http://mythruna.com/[http://mythruna.com/]

Some say that it is not guaranteed that the Entity System will always work in practice.  This may be true depending on the projects structure.  Therefore project structure and layout is an important consideration when using an entity system.


=== Difficulty

It can be hard to adapt to the style of Entity System Programming if you are used to OOP.
Therefore you need to pay attention that you in no way mix both styles together.
Like other software architectures, you lose all benefits if you are not consistent.


=== Should I use an Entity System?

*  Make sure that you want to take the risk of using a very new concept
*  It is better if you have good programming experience
*  An entity system can make it easier to add new content as your game project grows.
*  Entity Systems are recommended for MMOs, but can be used for practically any game project.


=== Sources:

*  link:http://www.indiedb.com/games/attack-of-the-gelatinous-blob/news/the-entity-system[http://www.indiedb.com/games/attack-of-the-gelatinous-blob/news/the-entity-system]
*  link:http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/[http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/]
