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

Discussion Best practice on how to organize code ?

Discussion in 'Entity Component System' started by sesky4_unity, Jun 21, 2023.

  1. sesky4_unity

    sesky4_unity

    Joined:
    Sep 1, 2019
    Posts:
    6
    During my journal on DOTS programming. I found it hard to organize my code because every system may touch a bunch of different components. It's not very clear which category one system should belongs to (However it's clear which category a component belongs to).

    If I organize the code by "object class" like Character / Spell / Props categories, which category should "MoveSystem" fall into ?

    Maybe organize the code by "functional" like Movement / Physics / Network is more clear ?


    Any idea on this ? Thanks : )
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,623
    IMO there's only 1 hard rule you should follow the rest is really personal preference. The rule being

    You should separate your Components and Authoring into a separate assembly from your Systems.

    Why? So that when you make logic changes in your systems subscenes don't need to rebake.

    Now you can separate your components and authoring into 2 separate assemblies or keep them together it doesn't matter it's up to you; they just need to be separate from (non-baking) systems.
     
    Last edited: Jun 21, 2023
  3. Arnold_2013

    Arnold_2013

    Joined:
    Nov 24, 2013
    Posts:
    262
    This seemed like a good idea. So I tried to just put a few systems into a 'system_assembly' and a few authoring components into a 'authoring_assembly'. So now you get a big pile of errors, because you are missing assembly references.

    So I add a reference from my System assembly to my Authoring assembly.
    I also add a big list of Unity.Entities, Unity.Burst, ect. to both of my new assemblies.

    So far so good? (I think).

    But now I am stuck, because I cannot reference anything that was in unity's default assembly. (Assembly-CSharp). So does this meen I need to 'assemalate' the entire project? (put entire project into custom assemblys) Before I can see if it works?

    If the answer is 'yes', I can work with that. If there is a better way please let me know.
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,623
    Generally yeah it's good practice to put all your code inside assemblies. It's a bit of a pain if you end up making a lot of separate modules of assemblies...
    So a long time ago I wrote a tool to generate and add references automatically to a bunch of assemblies (as always found in my core library.)

    upload_2023-6-21_21-40-28.png

    How I can handle making so many assemblies without going insane

    upload_2023-6-21_21-41-46.png
     
    Last edited: Jun 21, 2023
    Occuros, bb8_1 and Arnold_2013 like this.
  5. Arnold_2013

    Arnold_2013

    Joined:
    Nov 24, 2013
    Posts:
    262
    Thanks for the info. Because its already quite a big project, I wanted to be able to improve the structure over time. This is great work for the last 15 minutes of the day, since it can break the entire build :D.

    So what I did was create 3 assembly definition assets. [Systems -> Authoring-> Global], where Systems can reach everything, but nothing can reach systems (except for systems). I've added a Assembly Definition Reference Asset to all the folders containing my own code and referenced the SystemAssemblyAsset in all of them. Now everything compiles again.
    Now I take some components and put them in a sub folder together with a new AssemblyAssetReference that references the AuthoringAssemblyAsset. This way I can slowly peal off the components from the main assembly while still having a working game.

    So the only downside is that you need more Assembly references in the SystemAssemblyAsset than you would have in the optimal split. This is the list needed for my project to compile again (the top 2 are my own). :

    upload_2023-6-21_16-32-48.png