Search Unity

FYI: Can create entities in Monobehaviours no problem.

Discussion in 'Entity Component System' started by auhfel, Mar 27, 2018.

  1. auhfel

    auhfel

    Joined:
    Jul 5, 2015
    Posts:
    92
    I tinkered a little with creating entities outside of a System. Found that creating entities was no problem at all, and being able to do that in Coroutines makes me feel a lot more at home with ECS. I think for now I'll probably do all UI/Player Input/Instantiation inside Monobehaviours, with actual computation and stuff inside Systems. These entities are being moved by the Job system, so it doesn't appear to be messing up multithreading or w/e.

    Some example code.



    public class EnemySpawner : MonoBehaviour {
    void Start () {
    StartCoroutine(SpawnEnemies());
    }
    IEnumerator SpawnEnemies()
    {
    WaitForSeconds wait = new WaitForSeconds(5);
    while(true)
    {
    NativeArray<Entity> enemies = new NativeArray<Entity>(5000, Allocator.Temp);
    EntityManager em = World.Active.GetOrCreateManager<EntityManager>();
    em.CreateEntity(Archetypes.basicEnemy, enemies);
    for (int i = 0; i < enemies.Length; i++)
    {
    em.SetComponentData<Position>(enemies[i], new Position { Value = Random.insideUnitSphere*20});
    em.SetComponentData<Speed>(enemies[i], new Speed { Value = Random.Range(1f,3f)});
    em.SetSharedComponentData<MeshInstanceRenderer>(enemies[i], Renderers.basicEnemy);
    }
    enemies.Dispose();
    yield return wait;
    }
    }
    }


    If any Unity guys reading this, How friendly are Monobehaviours/ECS going to be in the future? I really like this approach -- It's like taking the best of both worlds from Data-Oriented Design and Objected Oriented design.
     
    FastTurtle222 likes this.
  2. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,364
    High level dirty gameplay code spawns low level ECS goodness, I can see it become a thing, and it's probably easy to encase in a Helper ECS.Spawn(tons of parameters);
     
    joseph-t83 likes this.
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    This is absolutely a good approach, we call this hybrid mode.
     
  4. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    863
    Is that the only way to process things like Collisions? I was going to emit entities for collision events in MonoBehaviour, and then process them in ECS.
     
  5. GabrieleUnity

    GabrieleUnity

    Unity Technologies

    Joined:
    Sep 4, 2012
    Posts:
    116
    At the moment we don't have a tight integration with collision events from physics, so what you are suggesting works fine. Using Entities to represent events is a good approach in general, also for other use cases.

    In the future, we expect to improve more and more the integration of the various systems on top of ECS, including physics. Most likely the approach will be different than what we use today, driven by the fact that we want to reduce the usage of callbacks as much as possible in ECS, which is critical for things like collision events, which might happen with a quite high frequency on some games.
     
    FROS7 likes this.
  6. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    863
    @Gabriel I can Imagine there to be a standard components CollisionListener and TriggerListener that when added would make sure that Unity's system will add collision components to those entities when collisions occur.
     
  7. GabrieleUnity

    GabrieleUnity

    Unity Technologies

    Joined:
    Sep 4, 2012
    Posts:
    116
    I don't know yet what the final solution will be, but we definitely want to improve the hybrid mode integration and make it easier to use both ECS and MonoBehaviour code together. This will also help to simplify the transition from one system to the other, so it is definitely something very important to us.
     
  8. Xomanowar

    Xomanowar

    Joined:
    Apr 2, 2014
    Posts:
    40
    I'm my game I need to spawn a limited number of enemies because I just deactivate and activate them, in a polling system... My entities can have these monobehavior scripts? Or everything inside of theses entities need to be specialized ECS components?

    For example, my enemies have an script that keep checking the distance from the player, if the player go too far I deactivate this enemy and list it to be reutilize ahead. But I do not figured ou how to do this in ECS. Or some behaviors can continue to be regular unity components?