1
\$\begingroup\$

I'm trying to build my first (2D) game with ECS but I always end up with some system calling other systems. Here's an example.

There is a RegionSystem that manages RegionComponents. On each frame, this system loops through all the RegionComponents and checks if the player is inside any of them.

When the player enters the water, the player's sprite must be changed from PLAYER_RUNNING to PLAYER_SIWMMING.

When the player steps onto a teleporter, an animation will play around the player, he'll be rotating and a new level will be loaded.

When the player steps into a hole, he will lose some health and will be repositioned in the level.

So, first of all, it needs the coordinates of the player's body to check if it is in a region (check point in rectangle). Hence it will ask the RigidbodySystem to get the coordinates.

Then if the player is inside a region, it will call PlayerSystem_OnInsideRegion(regionInfo).

In turn this function, PlayerSystem_OnInsideRegion must react differently depending on the type of the region:

  • for the water case, it needs to call the SpriteSystem to change a sprite, the PlayerSystem to set a boolean (m_inWater = True;) and the RigidBodySystem to get the coordinates of the player's body;

  • for the teleporter case, it needs to call the LevelSystem and tell him to switch to another level on the next frame;

  • for the hole case, well, simply descrease the member m_health of the player component, so no need to call another system here.

I have the feeling this is a horrible design and does not fit the ECS architecture. I read that people use a message system where systems can subscribe and unsubscribe to particular type of message. But I also read that it can be tricky to react to them in a proper order.

\$\endgroup\$
  • \$\begingroup\$ "and does not fit the ECS architecture" that's the thing: there is no such thing as the ECS architecture, there are many ways to implement it. What is your way? What do the E, the C and the S do? How do they interact and what are their responsibilities? \$\endgroup\$ – Vaillancourt Mar 23 '19 at 19:13
1
\$\begingroup\$

As you describe, this is usually solved by a message system.

The region system runs early in your frame update, say just after movement, and any time an object changes region, it issues a "region changed" message for that object.

Other systems can subscribe to that type of message. When their turn in the update comes, they can read the relevant messages for the objects they care about and react accordingly — say by swapping out an animation or sound set. They don't need to know anything about how regions are determined, they just use the region ID as a key to look up into their asset collection & find the matching entry.

 But I also read that it can be tricky to react to them in a proper order.

I don't think that's likely to bite you here. With an ECS you'll typically have your systems running in a fixed order, so you can ensure the animation & sound systems run after the region update system, which runs after the movement integration systems. That ensures there's a well-defined order in which the data gets processed and used.

You can use this to cut down on excess workload too. Rather than searching every region for every object that needs region information, you can use messages from your movement system to indicate just the objects that moved. The region system can then process them: "Oh, the player moved from 3,3 in region A to 4,3? That's still in regionA, so no change to report!" without looping over all regions or searching a partition structure to make that determination.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks, I'll go with the mesage system solution then even if I wanted to avoid it because I was scared of the errors I could make. But I clearly see that it has benefits using one in ECS. And the MoveSystem posting message about region seems really nice. I was still in a mindset of looping through all my regions because I guess I don't have any experience with ECS yet. The part that scares me with messages is the collision handling and resolution but I need to do more research on this. \$\endgroup\$ – Itsbananas Mar 23 '19 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.