# Dynamic Events in an Entity Component System

My game makes extensive use of libGDX's Entity Component System Ashley, and my game world (including all levels and areas) is created with Tiled.

In Tiled, I have an object layer where all of my entities are added (as rectangles, although the shape does not matter). Each object properties links to a YAML file containing serialized components which are deserialized when the Entity is added. This system works well, and I want to use a similar method for handling all types of map events, although I don't know of a way to do this.

I want to create dynamic events that:

1. Have one or more causes
2. Have one or more effects for each cause

Where a cause is something such as a player interacting with a switch, and an effect is a door opening. One or more cause could be, for example, that the player must have a specific item and interact with a switch.

What I would do is represent the event triggers as entities in my game. This way you can add event triggers on your maps with tiled as you would do for regular entities.

Let's take the example of a really simple event opening a door when the player walk on the trigger (collides with the trigger entity).

So at this point you should have a collision system notifying when two entities are colliding. Once you know a collision occurred between your player entity (or whatever) and the event trigger entity. You can fire the event linked to this trigger so it can be processed by the relevant systems.

So my event trigger would be composed of two components :

• Collision Component
• Event component : holds the event that will be fired

The event by itself would be defined by an enum defining what kind of event it is (OpenDoor, DestroyEntity or whatever) and something like an hash containing more informations about the event, like the id(s) of the entity(ies) affected by the event.

So, finally, to 'fire' the event you will need a messaging system allowing messages to be dispatched between systems in your game. For my own game I use a 'messaging system'. This system holds a list of fired messages. When a system is interested by a particular message, it queries the list for this particular message, if the message is present, it then processes and decides or not to consume it (removes it from the list). It works alright and this way you don't break the entity component systems pattern by keeping the logic in the systems and don't have your systems inherit from kind of listener interfaces.

I use messaging for various things, for example, my collision system uses it to notify the game when a collision occurred. A message is fired containing data about the collision, which entities are colliding. So i could have a damage system that query for collisions messages and act accordingly.

To fire an event I would just represent it as a message (an event message holding the event) and fire it as a normal message.

This system can be highly improved I think but it works and does not break the pattern you are trying to follow.

• I see. This 'messaging system' you suggest, is it a literal MessagingSystem that is added to the game's engine like all other systems (such as the MovementSystem)? And it executes logic based on whether or not entities have a particular component, in this case for example, a MessageComponent which holds all of the messages that have been fired? – driima Oct 9 '15 at 19:09
• I don't think you need to bother with a message component. Just make a processing system (not bound to any component), expose some methods to add message or query for messages. Also you should maybe make sure your messages list does not contains out dated messages. – nathan Oct 9 '15 at 19:38
• For instance, my DoorSystem (bound to a DoorComponent for example) would query for DoorOpen messages and if one is found, do a doorCompoent.IsOpened = true. Something like that. – nathan Oct 9 '15 at 19:43