Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ECS jobs and transactions

Discussion in 'Entity Component System' started by Ivan-Pestrikov, Mar 5, 2019.

  1. Ivan-Pestrikov

    Ivan-Pestrikov

    Joined:
    Aug 8, 2014
    Posts:
    21
    What is the recommended way of creating simulations based on transactions?

    As far as I understand the ECS, most of entities interactions consist of two steps:
    1) Gather and process data, required to change components (chunk jobs)
    2) Apply changes in bulk (chunk jobs or a command buffer)

    What if we need to implement a transaction bases simulation, where:
    1) Every change of data involves two entities, like debit and credit.
    For example: a machine takes energy from a battery. Energy in the machine grows, in the battery goes down.
    2) Every following transaction must consider the changed state of entities by the previous transactions.
    3) There are dozens of different types of debit-credit transactions between entities, and some of them can invoke a chain of reactive transactions in the same frame (again, each of them must know about the current state of the involved entities).

    On the main thread, the solution is clear. Using entityManager.GetComponentData & SetComponentDate. But this is very slow. How to implement it using jobs?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    In JobComponentSystem you have GetComponentDataFromEntity<MyComponent>() this lets you put that "Entity -> specific component data lookup" into a job and thus make it be parallel and bursted.

    Naturally the random access of the memory due to the entity based lookup of the data is not optimal for performance. But some problems do require random access and thats totally fine. Moving it to a job & burst makes that as fast as it can be.
     
  3. Ivan-Pestrikov

    Ivan-Pestrikov

    Joined:
    Aug 8, 2014
    Posts:
    21
    Thank you! This is exactly what I was looking for.