3
\$\begingroup\$

everyone. I'm kinda new to game dev, I saw the idea of the ESC system and I'm inlove with it. I wanted to integrate something like that in my game, so I started rewriting my code. I have a the following structure. I have a World, that has a Manager attached to it. This manager takes care of all the Systems attached to this world that we will iterate over and use to update our game. Also the manager takes care of the Entities of the World. Every System and every Entity can have a Component attached to them. In the System the component represents the actual Components of every Entity that it's responsible for updating. On the other hand the Entity's Components are just condition (or behaviours) of the Entity. Like the player can walk, run, jump etc...

Everything seems to be fine I guess. The problem is when I want to perform the actions of updating the actual components. I can't wrap my head around of what to pass to the System. How do I know what entity I have to update? I'm using JS by the way. So lets say I have a eventListener attached to the window object. Whenever a player presses a KEY I want to update his position. I have a PositionComponent that is attached to my Player entity that has a ID generated by the following line of code

createEntity(components) {
    const id = (new Date().getTime()) & 0xffffffff; // Generate random 32 bit integer

    let mappedComponents = [];
    components.forEach((component) => {
        mappedComponents.push({
            [component.name]: component,
        });
    });

    this.entities.set(id, mappedComponents);

    return id;
}

Now this bad boy is a Entity with a ID and has a Component attached to it. But I also have like 10 more enemy entities with the same Component as the Player. How am I supose to determine who is moving? I just don't seem to get this part.

EDIT:

I probably need to clarify that every Entity has a unique by it's ID. So are the Components in it. Since every Component tha is attached to a Entity is a new instance of that Component and saves the data for the Entity. For example: This is a Position Component

export default class Position {
    constructor(x, y, rotation) {
        this.name = 'Position';
        this.state = {
            x,
            y,
            rotation,
        }
    }
}

This is how I attach Component to a new Entity

let playerEntityId = gameWorldManager.createEntity([
    new Position(20, 30, 0),
    new Renderable('#FFFFFF') 
]);
\$\endgroup\$
7
  • \$\begingroup\$ You'll need to define a bit more a few things: What does "takes care of" mean? Components and entities are created, owned (or stored), used (or updated) and destroyed. Who does what? When you say But I also have like 10 more enemy entities with the same Component as the Player., do you mean the same instance or do you mean the same type? Is a System Component the same as an Entity's Component? \$\endgroup\$
    – Vaillancourt
    Sep 30, 2021 at 16:55
  • \$\begingroup\$ Sorry for the poor information, "Takes care of means" that it will update the Component of the specific Entity. Whenever I create a Entity I create a new unique instance of tha Component so it can hold the data about this Entity. When I say 10 more enemies it means that every Enemy is a Entity with unique instances of each Component that is attached to them. The Component in the system is simply a array of strings describing the Components that this System is responsible of updating. \$\endgroup\$ Sep 30, 2021 at 17:11
  • \$\begingroup\$ I've also updated my answer with some code examples. I can attach my full code if needed. It's just not finished yet and I don't know if it will be confusing \$\endgroup\$ Sep 30, 2021 at 17:19
  • \$\begingroup\$ You may be interested in this series of articles that goes over some of the common architectures for ECS \$\endgroup\$
    – DMGregory
    Sep 30, 2021 at 17:20
  • \$\begingroup\$ I will check that out for sure, thanks. So far I got the idea from Here Here Here and Here \$\endgroup\$ Sep 30, 2021 at 17:23

1 Answer 1

2
\$\begingroup\$

The problem is when I want to perform the actions of updating the actual components.

What you've shown us seems to imply that the Component instances are owned by the Entity instances.

Assuming that System act upon one or more Component(s) of a single Entity, you need to make them aware that a new Entity has been created and has a set of Components they are interested in.

So once you create the Entity, pass it to every system, and let each system keep a reference to the entity (or to the set of components of that entity).

So when you need to update your game loop, you just need to call every System update method; they'll each loop through their set of component and act upon them.

Given this,

I can't wrap my head around of what to pass to the System.

You pass nothing when you update your System. You only need to bother about updating the list of references your Systems have when you create or remove components.

How am I supposed to determine who is moving?

Yep, the Entity representing your Player and the Enemies will all have the same components to display and place them around. The difference will be in how you control them.

You'll attach a "PlayerControlled" Component to the Entity that represents your player, this Component will do nothing per se (unless you need it to keep some kind of state), it's just that one of your Systems will act upon Entities that have both a Position component and a PlayerControlled component: the update method of the system will translate the "keyboard events" into "player moves" and update the Position component with it. Yes, the list of that System will contain only one set of components (or Entity).

Similarly, you'll attach a "AiControlled" Component to the Entities that represent the Enemies. The System that will update entities that have both AiControlled and Position will need to figure out what to do with each of those entities.


As a final note, it's great to look around at how other folks have done it, but keep in mind that there is no ECS police that will tell you that you're doing it right or wrong. An ECS is a means to an end, not an end in itself; in the paragraphs above, I mention "Yes, the list of that System will contain only one set of components (or Entity).". It could make more sense to you to just have keep this as a variable and not in a list.

If this is your first iteration, and "it works" you can go on with it and release your game with it, then improve upon it with the knowledge that you've gained with this project and release your next game with a better version.

\$\endgroup\$
2
  • \$\begingroup\$ That you very much for the detailed and good explanation. I think I understood the concept. Basically I need a component to distinguish the player from everything else, so as the AI. Thank you very much \$\endgroup\$ Sep 30, 2021 at 18:04
  • \$\begingroup\$ Yes, that's the gist of it. \$\endgroup\$
    – Vaillancourt
    Sep 30, 2021 at 18:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .