Why use ECS mode in game development

Source: Internet
Author: User

Http://www.richardlord.net/blog/why-use-an-entity-framework Why use an entity system framework for game development? 

Following my previous post on the entity systems for game development I received a number of good questions from developers. I answered many of them in the comments on this post, but one question stands out. It's all very well explaining what's an entity system framework are, and even building one, but why would you want to use one ? In particular, why use the later Component/system architecture I described and that I implement in Ashrather than the Earl IER object-oriented entity architecture as used, for example, Inpushbutton Engine.

So, in this post I'll look more at what the final architecture are, what it gives us, and architectures don ' t, and Why I personally prefer this architecture.

The core of the architecture

First, it's worth noting the core of this architecture are the components and the systems. Components is value-objects that contain the state of the game, and systems is the the logic that operates on this state, CH Anging it as the game progresses. The other elements of the architecture is purely incidental, designed to make life easier.

The

The entity object is present to collect related components together. The relation between are encapsulated by the concept of a entity and that's vital to the architecture, but it Isn ' t necessary to use a explicit Entity object for this. There is some frameworks that simply use a ID to represent the entity, adding the entity ID to every component to Indica TE which entity it belongs to. In this scenario there was no requirement for a entity object at all and entity based operations be performed through an Entity Manager, using the ID to indicate which entity they is operating on.

As a concept, the entity is vital to the architecture. But as a code construct it is entirely optional. I include it because, when using a object-oriented language, the entity object makes life easier. In particular, it enables us to create the methods this operate on the entity as methods of a entity object and to track and manage the entity through this object, removing the need-track IDs throughout the code.

While the concept of a entity is vital to the architecture, the concept of node objects is entirely incidental. The nodes is used as the objects in the collections of the components supplied to the systems. We could instead provide each system with a collection of the relevant entities and let the "systems pull" the components th EY want to operate on out of the entities, using the Get () method of the entity.

In Ash, the nodes serve and the purposes. First they enable us to use a more efficient data structure in which the node objects that the systems receive is nodes I n a linked list. This improves the execution speed of the framework.

Second, using the node objects enables us to apply strong, static typing throughout our code. The method to fetch a component from an entity necessarily returns an untyped object, which must then is cast to the Corre CT component type for use in the game code. The properties on the node was already statically typed to the "Components" data types, so no casting is necessary.

So, fundamentally, the entity architecture are about components and systems.

This is not object-oriented programming

We can build our entity architecture with a object-oriented language but at a fundamental level that is not Object-ori Ented programming. The architecture is isn't about objects, it's about data ("components") and sub-routines that operate on that data (systems).

For many object-oriented programmers the "the hardest part of the working with an entity system framework. Our tendency are to fall back to what we know and as a object-oriented programmer using an object-oriented language that M EANs encapsulating data and operations together into objects. If you don't have a framework like Ash you'll fail.

Data-oriented programming

Games tend to is about lots's fast changing state, with players, non-player characters, game objects like Bullets and Las ers, bats and balls, tables and chairs, and levels, scores, lives and more all have state that might include position, R Otation, speed, acceleration, weight, colour, intention, goals, desires, friendships, enemies and more.

The state of the game can is encapsulated in this large mass of constantly changing data, and on a technical level the GAM E is entirely about "what" this "is" and how this data changes.

In a game a single little piece of this data are many operations acting on it. Take, for example, a-player character that have a position property that represents the character ' s position in the game wo Rld. This single piece of data is used by

    • The render system, to draw the "player in the".
    • the camera system, to position the camera relative to the player.
    • the AI systems of all Non-player characters, to decide how they should react to the player.
    • the input system, which alters the player position based on user input.
    • the physics system, which alters the player ' s position based on the physics of the game world.
    • the collision system, which tests whether the player is colliding with other objects and resolves those collisions.

And probably many more systems besides. If we try to build our game using objects this encapsulate data with the operations this Act on that data and then we'll bu ILD dependencies between all these different systems as they all want to being encapsulated with the player ' s position data. This can ' t is done unless we code the game as a single, massive class, so inevitably we break some parts of the game int o Separate systems and provide data to those systems–the physics system, the graphics system–while including other ele Ments of the game logic within the entity objects themselves.

An entity architecture based in components and systems takes, the idea of discrete systems to its logical conclusion. All operations was programmed as independent systems, and all game state was stored separately in a set of data components, Which is provided to the systems according to their need.

The systems is decoupled form each of the other. Each system knows only is about itself and the data it operates on. It knows and cares nothing at all on the other systems and what they may have affected by or used the data before or aft Er this system gets to work with it.

Also, by embracing the system as the core logic of the architecture, we is encouraged to make many smaller and simpler Sy Stems rather than a few large complex ones, which again leads to simpler code and looser coupling.

This decoupling makes building your game much easier. It is what I enjoy working with this form of the entity system so much, and why I built Ash.

Storing the game state

Another benefit of the Component/system architecture is apparent when you want to save and restore the game state. Because the game state was contained entirely in the components, and Because these was simple value objects, saving the GAM E state was a relatively simple matter of serialising out the components, and restoring the game state involves just Deseri Alising the data back in again.

In the most cases, serialising a value-object are straightforward, and one could simply json-encode each component, with Additi Onal data to indicate it entity owner (an ID) and its component type (a string) to save the game state.

Adam Martin wrote about comparing-a entity system framework to data in a relational database (there ' s lots of interesting entity related stuff on Adam's blog), and emphasising that conversion between a relational database for Lon G-term Storage and components for game play doesn ' t require all object/relational mapping, because components is simple C Opies of the relational database ' s data structure rather than complex objects.

This leads further to the conclusion, a Component/system architecture are ideal for an MMO game, since state would be St ORed in a relational database on the game servers, and much of the processing of this state would occur on the servers, whe Re using a set of discrete, independent systems to process the data as the game unfolds are an excellent fit to both the DA TA storage requirements of the state and the parallelism available on the servers.

Concurrency

Indeed, a Component/system architecture is the well suited to applying concurrency to a game. In more games, some of the systems is entirely independent of each and including being independent of the order in WHI Ch They is applied. This makes it easy-to-run these systems in parallel.

Further, most systems consist the A loop in which all nodes is processed sequentially. In many cases, the loop can be parallelised since the nodes can is updated independently of each other.

This gives us and places in the code where concurrency can is applied without altering the core logic of the game, which I s inside the loop in the systems, or the core state of the game, which are in the components.

This makes adding concurrency to the game relatively simple.

We don ' t need object-orientation

Finally, because the Component/system architecture is not object-oriented, it lends itself to other programming languages That's implement different programming paradigms like functional programming and procedural programming. While I created Ash as a Actionscript framework, this architecture would is well suited to Javascript for client side Dev Elopment or any of the many functional languages used for highly concurrent server side development.

Update:in-game editors

Tom Davies have pointed out that a very valuable benefit to him are how easy it was to create an In-game level editor when de Veloping with a entity system framework like Ember Orash. You can see him example here.

I agree with Tom, the is a very useful benefit of these frameworks. The complete separation of the game state and the game logic in a entity system framework makes it easy-to-create an edit Or that lets your alter the state (configuration, level design, AI, etc.) while playing the game. The ADD to this easier saving and loading of state and we have a framework which is very well suited to in-game editing.

Why use ECS mode in game development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.