Search Unity

Designer-configured event chains and ECS

Discussion in 'Entity Component System' started by DreamingImLatios, Mar 31, 2018.

  1. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    I'm trying to figure out how to convert some common design patterns I use to work with ECS, or more specifically, Unity's ECS implementation.

    In many story-driven games or puzzle-style games, most of the game logic exists in event system chains. Ideally these chains are set off from physics triggers or inputs and have no per-frame update logic. This leads to zero per-frame polling, which is really useful when these events are relatively sparse.

    In Unity, I often have a couple of "smart" Monobehaviours which contain state info, rely on polymorphism and interfaces, talk to backends, and handle much of the complicated game logic. Usually I will have a couple of these attached to a GameObject. Then I have a bunch of other small components which are just event listeners that respond to triggers or input or other propagated events which call methods in the "smart" Monobehaviours.

    I'm really excited for ECS because much of the backend systems lend themselves directly to ECS systems. I'm most likely targeting a hybrid ECS approach since I have no idea how to do anything like this in pure ECS (if you know how to do this in pure ECS with thousands of unique sparsely-activated events with predefined references, I'm open ears). However, there's a couple of things that I'm sure are possible yet.

    First, I want to move some of the data in the "smart" Monobehaviours into IComponentData components, but I only want these to be accessible by the "smart" Monobehaviours (in response to the event handlers) and the systems. So I need to hide them in the inspector and generate them from script inside the Monobehaviour. What is the best way of doing this?

    Second, for event handling, I want to be able to detect certain events generated from systems. I can generate IComponentData tags from a system. But then I want to process associated event handlers. So if I have a DetectHitByBall ComponentSystem that adds a HitByBall IComponentData, I then want a ProcessHitByBall ComponentSystem that operates on entities with HitByBall and IHandleHitByBall in which IHandleHitByBall could be several different Monobehaviours that implement a void HandleHitByBall() method. The HandleHitByBall method could turn on a light, add a point to a player, or start a timeline. The ProcessHitByBall system would just iterate over these and call them polymorphically (Imagine UnityEvent). Is this even possible in Unity's hybrid ECS solution?

    And lastly, I'm worried about how entities and components are structured in memory. I have a lot of GameObjects that each have a unique combination of components, and many of those components do not matter to systems. How badly would this interfere with Unity's ability to pack ECS components in consecutive memory for systems?
     
    S_Darkwell likes this.