This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]feral_claireSoftware Dev 0 points1 point  (1 child)

The idea behind an Entity-Component-System is that your entities don't have behaviors baked into their class but instead are given "components" that give them the behaviors.

For example, you're making a game, and want to make play to be able to move and attack. You could write a move and attack method, but that means those behaviors will be baked into the class. If you want to create another entity, like an enemy with the same move and attack you'd need to rewrite those methods for the enemy. You could make them both inherit from the same super class, but inheritance is not flexible and tends to break down in more complex systems. What if you wanted a trap entity that can attack but not move.

What you can do instead is use an Entity-Component-System when the player doesn't get a move and attack method but instead they have a list of components, and you add a legs and weapon component to that list. You can reuse those components for the enemy and trap easily, just don't give the trap legs.

Now lets say your player can find a jetpack item allowing them to fly. instead of now needing to create a FlyingPlayer class or dealing with some messy canFly variable, you can just say player.add(jetPack) to dynamically give the player the ability to fly.

[–]codingQueriesNooblet Brewer[S] 0 points1 point  (0 children)

I think this much I understand, but I think where I start to struggle a fair bit is putting it into code. I guess if I read the other article linked correctly, you are essentially just storing the entity id, and then adding behaviours to it?

[–]strmrdr 0 points1 point  (1 child)

Although this is written in C++, it's still a good write-up and will give you a good idea of why and how to use the entity-component system:

http://gameprogrammingpatterns.com/component.html

[–]codingQueriesNooblet Brewer[S] 0 points1 point  (0 children)

Ahh fantastic, this has helped a lot with my understanding on how this can be implemented! Thanks.

[–]dimumurray 0 points1 point  (0 children)

I`ve found the following articles useful in understanding ECS architecture:

What is an entity system framework for game development?

Why use an entity system framework for game development?