Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Does ISystem no longer force implementation of OnCreate, OnDestroy, OnUpdate?

Discussion in 'Entity Component System' started by Coeusul, Jul 11, 2023.

  1. Coeusul

    Coeusul

    Joined:
    Nov 4, 2021
    Posts:
    9
    Hi all,

    When I started my creating an ECS system using ISystem for the first time post-Entities 1.0 release just now, I found that ISystem doesn't force the implementation of the three methods OnCreate, OnDestroy, OnUpdate contrary to what was shown on the tutorials I've seen. However, the update method seems to still be called and the system itself appears to be running just fine as shown in the attached picture.

    I couldn't find any mention of this behaviour in my searches (so far). Is this a feature, a bug, or something wrong with my (code editor/unity editor) configurations? Then again, I may have just missed out some mention of this in the change log/update notes...

    Any information on this would be greatly appreciated.

    Many thanks.

    ISystem interface implementation query.png
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    ISystem interface has default empty implementation (newer C# feature). So its not enforced.
     
    apkdev and Coeusul like this.
  3. Coeusul

    Coeusul

    Joined:
    Nov 4, 2021
    Posts:
    9
    Thanks for the fast reply!

    Is the "default empty implementation" you mentioned related to the [RequireImplementors] and [RequiredMember] that can be seen inside the ISystem.cs file? They did look unfamiliar... time to do more research on what those mean.
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    Its a C# feature, important part is this:
    Code (CSharp):
    1. public interface ISystem
    2. {
    3.     ...
    4.     void OnCreate(ref SystemState state) { }
    5.     ...
    6.     void OnDestroy(ref SystemState state) { }
    7.     ...
    8.     void OnUpdate(ref SystemState state) { }
    9.  
    10.     // Instead of
    11.     void OnCreate(ref SystemState state);
    12.     ...
    13. }
    [RequiredMember] is there to prevent stripping when building. See comments at example:
    https://docs.unity3d.com/ScriptReference/Scripting.RequiredMemberAttribute.html

    Same goes for [RequireImplementors] (except its for interfaces)
    https://docs.unity3d.com/ScriptReference/Scripting.RequireImplementorsAttribute.html
     
    apkdev and Coeusul like this.
  5. Coeusul

    Coeusul

    Joined:
    Nov 4, 2021
    Posts:
    9
    Now that you mention it, those are curly braces { } at the end - completely missed that detail.

    Learned something new today. Thanks again!
     
    apkdev and xVergilx like this.