• entries
21
28
• views
34208

# ECS II: Messaging

1653 views

Last article:

https://www.gamedev.net/blog/1930/entry-2260476-ecs-i-entities-components-and-systems-basic-design/

Its been a while since I had time for programming, the new addon of WoW kept me busy for some time. But now I'm back and ready for action. This articles topic is messaging in my engines entity-component system.

Why messaging:

Remember from the last article that in my ECS I have a bunch of systems, that perform actions for components. Those systems pretty much live in their own SystemManager and used to be encapsulated from any direct access. Recently I got lazy and just exposed the direct pointer to any system added, since I was getting tired of handling some complex editor-interactions by adding 100 types of messages. But before that, and for game-interactions, messaging is still the most intelligent way. It requires no direct coupling between a system and its notifier, all you do is send a message, and any system the registered to it will get it. I also heavily relied on templates here again. A message is defined as a class:struct UpdateCameraMessage : public ecs::Message{ UpdateCameraMessage(Camera& camera); Camera* pCamera;};
I'm using yet again the CRTP to generate a unique message ID, and allow a simple 3-step procedure for using messages:// 1. register the system for the messagevoid CameraSyste::RegisterMessages(ecs::MessageManager& messages){ messages.Register(*this);}// 2. send the messagemessages.DeliverMessage(camera); // ctor of the message will be called from the passed arguments// 3. translate and handle the messagevoid CameraSystem::ReceiveMessage(const ecs::BaseMessage& message){ if(auto pUpdateCamera = message.Convert()) { pUpdateCamera->pCamera; // do stuff with camera here }}
Kind of neat, isn't it? I have seen way wore message deliverings in my short career.

Stuff for the future:

At the point I developed the system I was at the edge of my capabilities, but now I'd like to go one step beyond. I'm not a hundred percent satisfied with the way registration/message handling works, and I think I can use templates to further make it easier. I'm thinking about having a system where instead of manually registering and checking for type, I inherit from a base-class that does this stuff for me:class CameraSystem : public ecs::System, public ecs::MessageHandler{public: void ReceiveMessage(const UpdateCameraMessage& message) override; }
I'm not to sure about the inner workings here, but I've seen something similar done in EntityX, so I'm sure its possible.

Thats it for this time. You may have noticed the the message passed is "const", so a system cannot export information to it. I've got a second system called Query for that, but I'll show it in the next article. Thanks for reading, and until next time.

There are no comments to display.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account