How should 'actions' be implemented in an EntityComponentSystem

Hello dear community,

currently, I am thinking about how actions should be implemented in an entity component based game. Let me describe this with the following example:

Let’s say the player controls a monster. This monster can fire some actions for example it can ‘attack’, it can ‘scream’ and maybe some other things… . So, ‘attack’ and ‘scream’ would be two different types of actions. Also, there can only be one action being fired at a time per entity, so it is not possible to scream and attack at the same moment.

My current approach looks as follows: When the player presses a certain button (e.g. “F”) it is checked if the entity already has an Action component and if it does not, a new Action component will be added which contains the type of action (e.g. “attack”). This can easily be done by an enum which lists all actions plus their duration. After the time (duration) is up, the Action component is removed again, so that a new action can be fired.

Later, I want to play specific animations and/or do some audio output for each action.

However, is the way I described a good approach or should I change something? This probably can be done this way but I just wanted to ask you guys before I go over and code this. :slight_smile:

Best regards
Domenic

1 Like

Doesn’t sound wrong to me. How do you check if an entity does have already an action?

As I described: By checking if it already has an action component.

Alright, I will do it this way, thanks!

About question: How would you handle jumping? Is jumping an action too or should it be handled differently (separate component for example)?

I guess and you said it in the description, my question was how do you check :wink: But I guess with ed.getComponent(…) the only point where I’m always unsure if it is a good idea (I also do this in some cases).

You could add an IdleAction component. And attach this if no action is attached. When you want to play an action you have to check whether the entity is in the idleaction set or not and replace this component by the action component.

I would do it this way if I’d have to follow this design :wink:
Create a group of actions for the arms and a group of actions for the legs and treat them separately.