Search Unity

[DOTS] Tips to improve my code and make sure its safe/correct

Discussion in 'Entity Component System' started by msivri, Feb 15, 2020.

  1. msivri

    msivri

    Joined:
    Jul 20, 2015
    Posts:
    2
    I started learning about DOTS and ECS with Unity yesterday night and wanted to write code purely with ECS without any MonoBehavior because before I heard about ECS, I was already doing single-responsibility "micro" MonoBehaviors so it wasn't a big leap. I wrote the code with the understanding that static functions are not safe to use in a scheduled job and they should be referenced in the main thread.

    I separate Components (IComponentData) and Services (ComponentSystems) for organizing the folders.

    The purpose of the below code is to select a "Selectable" entity when the user clicks the screen. I have one entity with ManagerTag that also has the component data ManagerCamera, ManagerInput, and ManagerSelection. I have another entity with the component data Selectable.

    The systems breakdown:
    • CameraSystem - Tracks the camera screen point to ray on the main thread and writes to ManagerCamera.
    • InputSystem - Tracks user input on the main thread and writes to ManagerInput
    • SelectionSystem - [Updates after Camera and Input] - Uses RayCast on a scheduled job with burst enabled and writes to ManagerSelection. Reads from ManagerCamera and ManagerInput.
    • SelectableSystem - This is the code I am not sure at all about. I need to get the Manager entity so that I can read what is selected. Then I run a job on the main thread that reads from the selected entity to check if the Entity in the ForEach expression is selected, and write to Selectable.IsSelected.
    It all compiles fine and Burst compiler runs fine with no warnings and the code works in my small test.

    Github (for easy viewing): https://github.com/msivri/ecs-experiment

    Documentation and Tutorials are sparse or basic, that is why I am posting here for advice before I take off with it and find out all my code is broken in production :)
     
    Last edited: Feb 15, 2020
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,269
    This is not true at all. Only static variables cannot be accessed. But static functions that take input arguments and output values or perfectly fine in Burst and Burst does a great job at inlining them and optimizing them. Writing stateless static classes is one of the best investments you can make in DOTS, especially if you make lots of smaller games.
     
  3. msivri

    msivri

    Joined:
    Jul 20, 2015
    Posts:
    2
    That makes sense. Thanks.