Skip to content

Push Data To Managed Layer

What

Events generated from DOTS Layer can be queried by the Bridge Layer and later used in direct method calls to the Managed Layer object, making the Bridge Layer responsible for connecting the other two layers. These events happens occasionally, not every frame. Events could be: - Newly added entities, such as unit, army, building - Newly destroyed entities - State changes, such as provinces get captured Events are captured using systems and queries.

Why

So that the Managed Layer does not need to know how to generate these events.

How

Use Query to capture newly added Entities

Code from DOTS Layer

public struct AttackTarget: IComponentData{
    public Entity Value;
}

Use ICleanUpComponent and system in Bridge Layer

public partial struct AttackingEventSystem: SystemBase {
    public delegate void OnNewAttackDelegate(Entity attacker, Entity target);
    public event OnNewAttackDelegate OnNewAttack = null!;

    private struct CleanUp: ICleanUpComponentData {
        public Entity AttackTarget;
    }

    protected override void OnUpdate() {
            if (OnNewAttack == null) {
                return;
            }

            using EntityCommandBuffer ecb = new(Allocator.Temp);
            foreach ((Entity entity, AttackTarget attack) 
                     in SystemAPI.Query<AttackTarget>().WithNone<CleanUp>().WithEntityAccess()) {
                OnNewAttack(entity, attack.Value);
                ecb.AddComponent(entity, new CleanUp {
                    AttackTarget = Value,
                });
            }
            ecb.Playback(systemState.EntityManager);
        }
}