Search Unity

Feedback Is there a better way for hybrid approach?

Discussion in 'Entity Component System' started by Evgeni_Incineration, Oct 26, 2022.

  1. Evgeni_Incineration

    Evgeni_Incineration

    Joined:
    Jul 4, 2022
    Posts:
    27
    Hello everyone,

    In our company we started exploring ECS and we decided to create a top-down shooter where there are going to be a lot of enemies and projectiles. We are struggling to figure out a better way of gameobject-world to entites-world communication aka the hybrid approach. We do not see any value everything to be entities, we want only the performance critical systems to be entities (projectiles and enemies).

    We have implemented basic top-down player controller with game objects and we want to spawn projectiles inside in entities system. We struggled a lot with it and we are looking for a better option than what we have now. Here is a repo to demonstrate: https://github.com/Zammy/TopDownShooterECS

    Here is our projectile spawning system: https://github.com/Zammy/TopDownSho...sets/Scripts/Systems/ProjectileSpawnSystem.cs We access the player as a global singleton. When we shoot we set a Shoot bool property to true and in ProjectileSpawnSystem we check if the flag is true and we spawn a projectile.

    Looks quite ugly and we are looking for a cleaner approach to this. Any tips how we can approach this in a better way are very much appreciated!

    Thank you
    -Evgeni
     
    YuriyPopov likes this.
  2. YuriyPopov

    YuriyPopov

    Joined:
    Sep 5, 2017
    Posts:
    237
    Bump from me
     
  3. msfredb7

    msfredb7

    Joined:
    Nov 1, 2012
    Posts:
    163
    If you are going with "Presentation in GameObjects" + " Simulation in ECS", I recommend the simulation to be in its own assembly, with no reference to gameobject code. Otherwise, you'll have bidirectional dependencies.

    So instead of your ECS system referencing the player, the player script should send "inputs" to your ECS world. These inputs could be booleans that you set to true, entities you create, or polymorphic objects you push in a list.

    On my project, we use the 3rd option. We have a SimInput base class and we have an ECS system called "HandleSimInputSystem". The presentation captures player inputs, creates SimInputs (eg. SimInputJump) and adds them in the HandleSimInputSystem's list. Later, the HandleSimInputSystem will read the input list and perform entity transformations in the simulation (usually, these transformations are just flipping bools on entities or adding into a DynamicBuffer for other systems to react). NB: Handling those player inputs is done on the main thread (no job, no burst), so we try to keep the logic as light as possible and dispatch work instead.

    EDIT: If you go with option 3 like we did, consider using a strategy to avoid allocations every frame (like pooling player input objects, or use structs without inheritance)
     
    Last edited: Oct 28, 2022
    RaL and Evgeni_Incineration like this.