Search Unity

ECS - Rebuild of Unity's upcoming - Entity Component System - FREE

Discussion in 'Scripting' started by Spy-Shifty, Nov 5, 2017.

  1. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    Well at the moment: if you have components which changes often use classes (position,...)
    If you have components which stays the same use struct because of faster access.
     
  2. vdwtanner

    vdwtanner

    Joined:
    Jun 10, 2015
    Posts:
    8
    Ah dang :/ Thanks for giving it a shot though! Fingers crossed that the Unity team has some magic code up their sleeves that is able to solve that issue.
     
  3. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    962
    I think you can ignore this for now. This issue can only be solved new compiler tech and something like a ref to a struct. In this regard, setting lots of structs takes so long that the approach costs more than it gains. On that note, isn't a ref struct already possible in 2018.1?

    Anyway, I'm using classes 99%, which solves the issue and your ECS is really fun to use with it. If you don't have entity counts 100k+ you are safe.
     
    Spy-Shifty likes this.
  4. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    Yes I do the same for my project.

    ref struct are part of C# 7. I didn't test 2018.1 yet. So I don't know about the support.

    Anyway I'm going forward to improve my framework. But I need some practice with it :D
     
  5. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    @Spy-Shifty

    Could you please clarify current best practice with regards to classes / structs. The templates indicate that struct is the way to go, but it seems this is not to be the case for the time being...
    - Are Classes best practice for your current state of ECS? (faster now)
    - Are Structs best practice once Unity implements this natively? (faster later)
     
  6. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    I can only speak about my ECS.

    Well, if you have not thousends of entities, just use classes.
    At the current stage, structs are only faster at reading not writing.
    So if you have Components that won't change often/sparsely over the livetime of an entity, you can use structs.
    But if you have a component that frequently changes (like position) use a class.

    I only can imagine how the upcoming C# Jobsystem and the Unity ECS performs. But the preview show us great performance in the use of structs.

    I believe the key lays in the NativeArray... and the special compiler.
     
  7. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    Do classes have to implement IColonable or only if wrapped in the Editor?

    Code (CSharp):
    1.     #if UNITY_EDITOR
    2.         [Serializable]
    3.         public class PositionComponent : IComponent, ICloneable
    4.         {
    5.             public Vector2 position;
    6.        
    7.             public object Clone()
    8.             {
    9.                 return MemberwiseClone();
    10.             }
    11.         }  
    12.        
    13.         [DisallowMultipleComponent]
    14.         public class Position : ComponentWrapper<PositionComponent>{}  
    15.     #else
    16.         [Serializable]
    17.         public class PositionComponent : IComponent
    18.         {
    19.             public Vector2 position;
    20.         }
    21.     #endif
     
  8. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    There is support for some C#7 features, I'm not sure if "ref-locals" is what you're referring to?
    https://forum.unity.com/threads/c-7-support.512661/#post-3362532

    There is also Tuples which I imagine might help a bit?
     
  9. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
  10. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546

    Yes, only for the wrapper.

    I wasn't sure what happens with linked references, if you initialize a gameobject and so on. (I didn't want to think of it a lot.. I was just lazy :D)

    This might be outdated... I'll check if we can remove the ICloneable interface.
     
  11. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    Hi there...just checking if you are still working on the system...
     
  12. Antares

    Antares

    Joined:
    Jan 10, 2013
    Posts:
    7
    Hey Spy-Shifty, it looks pretty nice!
    I really like having something before Unity releases their stuff.

    Comming from mobile applications I would like to give some feedback that moves me away of this approach.

    1) I never like seeing using System.Linq; Comming from iOS I only had bad experiences with Linq, and wouldn't recommend it in any project using AOT.

    2) I love IoC, but I hate having Dependency Injection using reflection. Again comming from mobile platforms Its not nice having to resolve things using reflection here and there.

    I would leave the (Dependency Injection / Reflection) on a per project choice, and not embbed it into a ECS solution.

    Cheers
     
  13. stepan-stulov

    stepan-stulov

    Joined:
    Nov 26, 2013
    Posts:
    28
    Hey guys, just watched the keynote.

    Will systems support injection of multiple kinds of tuples as well as explicitly declaring the tuples? Right now this parallel track arrays that happen to be of the same length is a bit of a hack. Besides, right now, if I understood everything correctly, each system can only work with only one kind of tuples at the same time. This severely limits the potential complexity of systems. I propose defining tuples (I prefer calling them aspects) explicitly, and allowing systems, in a similar attribute-injection manner to receive lists of different tuples. Such systems will allow more complex behaviours "marrying" 2, 3, 4 etc. tuple kinds with each other.

    Something like this (the syntax is a bit made up):

    Code (CSharp):
    1. [Aspect]
    2. public struct FlyingAspect
    3. {
    4.    [Component] public Transform Transform;
    5.    [Component] public Flyable Flyable;
    6. }
    7.  
    8. [Aspect]
    9. public struct CrawlingAspect
    10. {
    11.    [Component] public Transform Transform;
    12.    [Component] public Crawlable Crawlable;
    13. }
    14.  
    15. [System]
    16. public class HuntingSystem
    17. {
    18.    [Aspects] public AspectList<FlyingAspect> FlyingAspects;
    19.    [Aspects] public AspectList<CrawlingAspect> CrawlingAspects;
    20.  
    21. public override void OnUpdate()
    22. {
    23.    // Now kiss :)
    24.    // Let everything that crawls hunt everything that flies.
    25. }
    26. }
    Hope this helps.
     
    Last edited: Feb 2, 2018
  14. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    It already supports it!

    Code (CSharp):
    1. class MySystem : ComponentSystem {
    2.     [InjectTuple("groupA")]
    3.     ComponentArray<ComponentA> componentA;
    4.     ComponentArray<ComponentC> componentC;
    5.  
    6.     [InjectTuple("groupB")]
    7.     ComponentArray<ComponentA> componentA;
    8.     ComponentArray<ComponentB> componentB;
    9. }
     
  15. stepan-stulov

    stepan-stulov

    Joined:
    Nov 26, 2013
    Posts:
    28
    Ok, that's good to know! Sorry for not paying enough attention!
     
    Last edited: Feb 2, 2018
    Spy-Shifty likes this.
  16. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    Well I won't change this for now.
     
  17. stepan-stulov

    stepan-stulov

    Joined:
    Nov 26, 2013
    Posts:
    28
    Is this new ECS the beginning of the end of the old component system, or will they co-exist?
     
  18. Manato

    Manato

    Joined:
    Jun 11, 2015
    Posts:
    28
    I believe this will co-exist, because not everthing should be wrapped to ECS. Sometimes it is more easy to write a single script to change the behaviour. You should use ECS for frequent used behaviour like physics and animation and only change them in a script (as it is currently!). But also health and collision should be wrapped to a component to access them in a system, still this is not possible (you can't define your own component (health) or system (collision) to acces them).
    The benefit of ECS is to lay your data in a cpu cache friendly way and reduce debug time because all functionality lays down in the systems.
     
    stepan-stulov likes this.
  19. SpellChucker

    SpellChucker

    Joined:
    Sep 30, 2016
    Posts:
    3
    I might be a noob, but I can't seem to get this compiling in my project. I just added the ECS folder to my project and I get 19 errors when compiling:

    Code (CSharp):
    1.     Assets/ECS/Core/EntityManager.cs(10,6): error CS0616: `ECS.InjectableDependencyAttribute': is not an attribute class
    2.    Assets/ECS/Core/Injection/InjectableDependencyAttribute.cs(12,6): error CS0641: Attribute `System.AttributeUsageAttribute' is only valid on classes derived from System.Attribute
    3.     Assets/ECS/Core/Injection/InjectableDependencyAttribute.cs(15,16): error CS1729: The type `Attribute' does not contain a constructor that takes `0' arguments
    4.     Assets/ECS/Core/Injection/InjectDependencyAttribute.cs(8,6): error CS0641: Attribute `System.AttributeUsageAttribute' is only valid on classes derived from System.Attribute
    5.    Assets/ECS/Core/Injection/InjectDependencyAttribute.cs(9,18): error CS1729: The type `Attribute' does not contain a constructor that takes `0' arguments
    6.    Assets/ECS/Core/Injection/InjectTupleAttribute.cs(8,6): error CS0641: Attribute `System.AttributeUsageAttribute' is only valid on classes derived from System.Attribute
    7.     Assets/ECS/Core/Injection/InjectTupleAttribute.cs(13,16): error CS1729: The type `Attribute' does not contain a constructor that takes `0' arguments
    8.     Assets/ECS/Core/Injection/InjectTupleAttribute.cs(17,16): error CS1729: The type `Attribute' does not contain a constructor that takes `0' arguments
    9.     Assets/ECS/Core/Injection/InjectTupleAttribute.cs(22,16): error CS1729: The type `Attribute' does not contain a constructor that takes `0' arguments
    10.     Assets/ECS/Core/SystemRoot.cs(10,6): error CS0616: `ECS.InjectableDependencyAttribute': is not an attribute class
    11.    Assets/ECS/Core/SystemRoot.cs(16,10): error CS0616: `ECS.InjectDependencyAttribute': is not an attribute class
     
  20. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    I need more information to help you!
    What Unity version do you use?
    Did you downloaded it completely?
    Is it an empty project?

    ...
     
  21. SpellChucker

    SpellChucker

    Joined:
    Sep 30, 2016
    Posts:
    3
    Sorry! It's not an empty project and we're using Unity 2017.1.2p3 on MacOS

    EDIT:

    It works fine on an brand new project, will investigate!

    EDIT2:

    Was an issue with my code! Woo awesome work man!!!
     
    Last edited: Feb 12, 2018
    Spy-Shifty likes this.
  22. J0hn4n

    J0hn4n

    Joined:
    May 11, 2015
    Posts:
    24
    Hello there , i had been seeking for a ECS in unity and i come to this doubt wheres is the differences between: Svelto.ECS, Entitas.ECS and your ECS aproach?
     
  23. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    This was ask one or two times in this thread!
    Please search for the post!

    If you have further questions please ask me.
     
  24. SpellChucker

    SpellChucker

    Joined:
    Sep 30, 2016
    Posts:
    3
    I have a general design question here. I love the ECS approach and this implementation is stellar. I'm am struggling with where to put this in a top-down RPG game when you have multiple scenes. You want your user data to cross scenes and some scene data to persist when you leave a scene and come back.

    Where would you put this ECS tool? Should there be an initializer in every scene? I think there _has_ to be because you can't instantiate game objects in the current scene if you stick a DontDestroyOnLoad on the ECS, because the ECS will live in it's own little scene. Any ideas?
     
  25. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    Well you can still use scriptable objects to store data persistent between scenes. So each scene has its own controller and an entity or even a field in the conrtoller to store the reference to that scriptable object that holds the states...
     
  26. igor_rst

    igor_rst

    Joined:
    Apr 20, 2017
    Posts:
    40
    Spy-Shifty likes this.
  27. jheiling

    jheiling

    Joined:
    Sep 28, 2010
    Posts:
    65
    Because I'm a lazy bastard I wrote a little F# script to generate components and systems (sans logic of course). It enforces some standard naming conventions, sorry for that ;)
    If you're interested, you can find it here: https://github.com/jheiling/BrokenBricksECSGenerator
     
    Spy-Shifty likes this.
  28. OneToMany

    OneToMany

    Joined:
    May 25, 2018
    Posts:
    6
    hi , i have question . how can i install ecs on my unity3d ?
     
  29. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    First enable .Net 4.x for the Scripting Runtime Version under the player settings.

    Now go to:
    Windows -> package manager
    Under the tab All
    Install IncrementalCompiler first
    than install Entities
    you need both to work well!

    done

    For more information visit this forum there is all you need:
    https://forum.unity.com/forums/entity-component-system-and-c-job-system.147/
     
  30. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    Hi Spy-Shifty,
    How would you compare your ECS to current Unity ECS, in terms of features and performance?
     
  31. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    I don't develop it anymore! Use Unity!
     
    MadeFromPolygons and Antypodish like this.
  32. Leopotam

    Leopotam

    Joined:
    Mar 26, 2012
    Posts:
    3
    Maybe better to add something to README at github project that development was stopped?
     
    MadeFromPolygons likes this.