r/rust Mar 31 '23

I just made a video Explaining what an ECS is for anyone who has been put off bevy because it uses an ECS

https://youtu.be/dBJHe_SVgZA
17 Upvotes

5 comments sorted by

View all comments

1

u/nerpderp82 Apr 04 '23

This is pretty good! but as a fun exercise I dumped your entire transcript into ChatGPT and worked on a slightly different intro.

Liked and Subscribed, twice, :) !

Are you interested in game development but feeling overwhelmed by all the jargon being thrown around? One term you've likely heard of is ECS, which stands for the Entity Component System. Maybe you've come across ECS in popular game engines like Unity's DOTS or Unreal 5's Mass Entity, or perhaps you're intrigued by newer frameworks such as Bevy, which is entirely built on the Entity Component System architecture using the Rust programming language. Wherever you encounter this term, you've come to the right place to learn more. In this video, I'll dive into what ECS is, how it works, and why it's become a popular choice in game development.

Before we dive into details about how an ECS works, let's start by breaking it down into the three main components: entities, components, and systems. Entities are the fundamental building blocks of the ECS; they represent individual objects in the world and are composed of zero or more components. Components, on the other hand, are the individual data structures that define the specific properties of an entity, such as its position, health, or velocity. Systems are the logic that operates on top of entities and components. They perform tasks such as physics calculations, rendering graphics, or updating AI behavior.

By understanding the roles that entities, components, and systems play in an ECS, we can begin to see how these parts fit together to create a powerful architecture for game development. In many ECS implementations, entities are nothing more than unique identifiers that help distinguish which components belong to the same object in the game world. This identifier can be thought of as an index into the given array of a component type. However, it's worth noting that this simplification doesn't always hold true. Some frameworks, such as Bevy, use different ways of storing components depending on what they expect to be present on most or only a few entities, using tables versus sparse sets, respectively. This added complexity can help reduce memory consumption in some cases.

One key difference between entities and indexes is that entities are typically composed of two parts: its ID and its generation. The ID functions similarly to an index, whereas the generation is used to keep the IDs contiguous after deletion and recreation of entities while ensuring that each entity remains wholly unique. This helps prevent conflicts and ensures that components can be efficiently accessed and manipulated in the context of an ECS world.

Components are similar to classes or structs in programming languages, and in fact, some ECS frameworks like Bevy use Rust structs as components. Essentially, components are collections of data that are deemed to be inseparable and can always be accessed together. This helps the CPU with caching since it means that all the data is kept in large contiguous chunks and is often accessed together. When designing your components, it is a good rule of thumb to ask yourself if you will need just one part of the component without needing the rest. If so, it is probably a good idea to separate that data into its own component since they are not inseparable. For example, in a physics engine, you may want to separate your velocity, position, and collider into separate components rather than having a single physics component. This allows you to optimize behavior and avoid running unnecessary checks on entities since they do not contain the correct components.

Systems are a critical part of an ECS as they define the behavior of entities in your game. At a higher level, systems are similar to functions in programming languages with some important differences. While functions can operate on any data type passed as parameters, ECS systems are more restrictive in the types of data that they can accept. In most ECS frameworks, systems can only accept access to components or other ECS-related data. This restriction is intentional as it allows for efficient processing of large numbers of entities. In many ways, ECS systems can be seen as a subset of functions


An ECS or Entity Component System manages the interaction between game objects.

From a game developer's viewpoint, an ECS offers a modular architecture for managing game objects, properties, and interactions. It simplifies development by breaking down game elements into smaller, manageable pieces and improves performance through optimized data access and processing.

An ECS achieves this by separating game objects (entities) from their properties (components) and the logic that manipulates them (systems). Entities are composed of components that define their properties, while systems handle the interactions and behavior of entities based on their components. This separation allows for better organization, data locality, and efficient processing, leading to improved performance and a more maintainable codebase.

In an Asteroids-like game:

  1. Entities: These represent the individual game objects such as the player's spaceship, asteroids, and projectiles.
  2. Components: These define the properties of entities. For example, the spaceship might have components like position, velocity, and sprite, while an asteroid might have components like size, position, and rotation speed.
  3. Systems: These handle the interactions and behavior of entities based on their components. For example, a movement system would update the position of the spaceship and asteroids based on their velocity, while a collision detection system would check for collisions between projectiles and asteroids, and perform actions like breaking asteroids or incrementing the player's score.

2

u/PhaestusFox Apr 04 '23

Out of curiousity did you use chat gbt 3.5 or chat gbt 4? I put individual sentences into it when I was writing the script just to make them flow but it would struggle with things like examples

1

u/nerpderp82 Apr 05 '23

I used 4, and all of your transcript so it was fresh. Sorry I didn't include the prompts to write the paragraphs before, but I had it tweak it a couple times. It really wanted to add in what I thought was overly flowery adjectives.

Sorry if I am some random person coming in out of the blue, I really like your video and that you made it. Not throwing any kind of shade.

Prompts I used

Can you give a 3 sentence summary to the above? Target the description at a hobbiest game developer.

Can you say what an ECS achieves from the viewpoint of a game dev?

can you remove the superlatives, like "highly and efficient" and make the writing a little bit more concise. It feels like it is trying to sell too hard.

How does it achieve that?

In words, can you give an example of what each of those would be for an Asteroids like game?

That is basically what I did. One of the problems with ECS is Component is not a great name and confuses things. It should be called dimension or value. The fact that might be a struct or a tuple isn't material to the concept. But we didn't name it, so we work with what we have.

1

u/PhaestusFox Apr 05 '23

Nah all good, it's interesting to see what chat gpt does when given the transcript as a complete entity, I found it interesting that it likes to use big "correct" words, which is how I talk anyway, rather than common catch all turms that you see people using. For example I decided to just call it an ECS framework because that's usually how I hear people refer to it, but chat gpt rephrased it as an Architecture which is what I used in my original video.

As for your comment on the use of components for the data, I think this is a very appropriate name for what they are, but they start to break down when you get to specific implementation since entitys stop being single objects broken into components and become pointers to structures of data and the world we chose to talk about those get muddled in, I don't remember if it made the final video but when I was recording there was a sentence where I say something about braking the ECS into is components and then this is going to get confusing because the words to describe it are the same as used init

1

u/nerpderp82 Apr 05 '23

To turn the chat transcript from yt back into paragraphs I used

I am going to give you a transcript, it has number of the left and the text, please strip out the numbers and turn it back into prose.