Sending Messages of Intent

Learn how to add messages of intent and then apply the movement.

Currently, the PlayerInput and RandomMovement systems load the map and check for tile access. As we add more gameplay elements, we needn’t remember to update both of them and any other system that can produce movement. Instead, we can share a lot of the functionality by having systems that produce movement produce a message indicating that an entity intends to move.

A later system can then process all movement requests and apply them if they’re possible. This is quite powerful. Suppose we add a stun mechanic—stunned monsters can’t move. Our system that handles stuns could remove movement intention from any entity, and we wouldn’t have to change a line of the other systems.

Messages as entities

Games often feature elaborate messaging systems. Message queueing and efficient message handling could easily fill its own book. We can build a simple, efficient messaging system inside an ECS by treating each message as its own entity. We’ll need a new component type for the message. For this, we create two new component structures in components.rs:

Get hands-on with 1200+ tech skills courses.