Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Showcase Entities Events - Intersystem messaging for ECS

Discussion in 'Entity Component System' started by annulusgames, Oct 8, 2023.

  1. annulusgames

    annulusgames

    Joined:
    Sep 24, 2020
    Posts:
    7
    What is this?
    Entities Events is a library designed to add event functionality for the Entity Component System (ECS). This allows for easier communication between systems and enables more flexible system designs.

    Where can I get it?
    It is available as open-source software on GitHub:
    https://github.com/AnnulusGames/EntitiesEvents

    How to use
    In Entities Events, event reading and writing are performed based on the type of event. First, you need to define a structure to be used for events. This event structure cannot contain reference types and must be an unmanaged type.

    The event type you want to use must be registered in advance using [RegisterEvent] attribute. Adding this attribute generates code that includes the necessary system and assembly attributes during compilation.

    Here's an example:

    Code (CSharp):
    1.  
    2. using Unity.Burst;
    3. using Unity.Entities;
    4. using EntitiesEvents;
    5.  
    6. public struct MyEvent { }
    7.  
    8. // Add RegisterEvent attribute to the assembly
    9. [assembly: RegisterEvent(typeof(MyEvent))]
    10.  
    11. [BurstCompile]
    12. public partial struct WriteEventSystem : ISystem
    13. {
    14.     // Cache the obtained EventWriter within the System
    15.     EventWriter<MyEvent> eventWriter;
    16.  
    17.     [BurstCompile]
    18.     public void OnCreate(ref SystemState state)
    19.     {
    20.         // Obtain the EventWriter with GetEventWriter
    21.         eventWriter = state.GetEventWriter<MyEvent>();
    22.     }
    23.  
    24.     [BurstCompile]
    25.     public void OnUpdate(ref SystemState state)
    26.     {
    27.         // Publish the event with eventWriter.Write()
    28.         eventWriter.Write(new MyEvent());
    29.     }
    30. }
    31.  
    32. [BurstCompile]
    33. public partial struct ReadEventSystem : ISystem
    34. {
    35.     // Cache the obtained EventReader within the System
    36.     EventReader<MyEvent> eventReader;
    37.  
    38.     [BurstCompile]
    39.     public void OnCreate(ref SystemState state)
    40.     {
    41.         // Obtain the EventReader with GetEventReader
    42.         eventReader = state.GetEventReader<MyEvent>();
    43.     }
    44.  
    45.     [BurstCompile]
    46.     public void OnUpdate(ref SystemState state)
    47.     {
    48.         // Read unread events with eventReader.Read()
    49.         foreach (var eventData in eventReader.Read())
    50.         {
    51.             Debug.Log("received!");
    52.         }
    53.     }
    54. }
    How it works
    The event buffer is held by a singleton entity and is cleared every frame. However, event information persists from the frame it was sent to the next frame, allowing the receiving system to read events even if it executes before the sending system in the next frame.
    If you do not read events every frame, there is a possibility of losing events. If this is undesirable, you can create a custom event system using the `Events<T>` which is a custom NativeContainer.
     
  2. DatCong

    DatCong

    Joined:
    Nov 5, 2020
    Posts:
    77
    what make this useful? for "event" in ecs i ussually create an entity and add a component on it.
     
  3. annulusgames

    annulusgames

    Joined:
    Sep 24, 2020
    Posts:
    7
    Yes, the concept of an event is certainly unnatural from a pure ECS perspective, and it should be expressed using a component.

    However, when actually using ECS, it does not work so well. Adding Components causes structural changes and reduces performance. Also, many of the implementations of events via Components are verbose and reduce readability. Therefore, I think that this kind of event function is effective in Unity ECS as well.

    Bevy Engine, which uses ECS as its base, also has similar functionality, and Entities Events was implemented based on this. I've used Bevy to develop games before, and I like the idea.
     
    xVergilx likes this.