[SOLVED] Qustion about entity system (Zay-ES)

So, I decided to make my Zombie Survival like game. This time, I decided implementing an entity system. However, I never used this architeture before and I’d like to know if what I am gonna do is the most efficient:

  • I’ll create an ItemComponent that extends EntityComponent. Then, when creating a new Item component, he will ask me for one or various ItemComponents. These components can be things such as “Attack”, “Consumable”, “Tool”, etc… This way I can limitate the components to only item type components, and define what should each item do.

  • I’m gonna add a system ok skills similar do Runescape: woodcutting, cooking, mining, etc… I’m thinking of simply creating field in the Player component that indicate the level of that skill. When I need to change the skill’s level, of someone, I just need to “catch” the specific Player component.

Nope. That’s an OOP way of doing things and not an ES way of doing things. If you find yourself having many components implement some common interface then it’s time to recheck the design. Generally it’s the sign of a problem.

This is precisely what you DON’T want to do in an ES. The whole point is that components and systems are independent exactly so that you can add new features later just be recombining things in a different way.

If you are thinking “I need this data” first then you are thinking backwards. You need to think “I need this system, what data does it need?”

The above seems to contradict itself. Probably you have different components for skill levels… it kind of depends on how you implement the skills. It’s possible that all skills are implemented the same way in which case you might have one skill level component that attaches skill level entities to a player.

Maybe browse through the Zay ES wiki on github and look at the case study may help to get the idea. ECS != OO. do not inherit components do not inherit system or you will end in frustration.

Thanks @ia97lies and @pspeed.

I already created the game following the zay’s wiki. I understood a little how to use this system, and to separate logic from visual.

About the items, let’s suppose I have 3 components: Damage, Defense and Consumable. Then, when I “use” that item, the program should find out what are the components that were added and apply specific commands.

However, if I start to add around 20 components, would it still be efficient for the program to find if there is each component on the item and then run code to each one?

Also, when I meant to create a field in the component and then catch the specific component, I was talking about adding fields to all Player components. But when I need to change the level of only one guy, I look for that specific Player component.

What systems would be using the 20 components?

Most systems only use 2 or maybe 3 components at a time. Some only use 1.

You will have to explain what you mean by this because it makes no sense to me in the context of an ES.

Ok, I’ll explain better:

Let’s suppose my items can only have 3 components:

Weapon - Allows the item to be used in combat (kill other entities)
Consumable - Allows the item to be eaten (change stats on Player)
Tool - Allows the item to interact with the world (chop trees, mine stones, etc…)

Lets suppose I create an axe, which has a Weapon and Tool component. This way I can chop down trees and zombies xD

Now, when I click left mouse, the ItemAppState gets the current selected item, and then it tests if the item has a Weapon, Consumable and Tool component. At the end it find out it only has two components. After that, I see what components should be used. If im just killing a zombie, the Tool component wont be handled by ToolAppState. So, WeaponAppState will handle the component and do the things to “kill the zombie”

Yeah, that’s fine.

Personally, I might have lumped Weapon and Tool into the same Usable component and left it up to scripts attached to the item type to know what to do but your way is not wrong. If you know that all weapon things will always do the same thing and all tool things will always do the same thing then it’s an appropriate approach.

…but I don’t see how that could balloon into 20 components, either.

Yeah, that was a little exaggerated. Thanks for the help xD

Hmm I personally would work with kind of power component which tree and zombies has and a hit component and kind a collision system which shrink down the power when hit collides. then a tree and zombie system, tree system collect wood and the zombie system just let zombie die or so.
But your approach does not look wrong. I guess there are always many ways to do it.

1 Like