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 Best practices to modify different entities/components in one Job

Discussion in 'Entity Component System' started by Sarletor, Sep 5, 2023.

  1. Sarletor

    Sarletor

    Joined:
    Jan 4, 2018
    Posts:
    6
    Hey! I need to implement such a system in my game:
    Entity_1 has Resource component, he moves to Entity_2 that also has Resource component
    When Entity_1 is near Entity_2 we need to check if Entity_2 Money (another component) is enough.
    If so Entity_1 need to remove some amount of resources and add it to Entity_2. And also remove some money in Entity_2

    Currently I am implemented this with IJobEntity and ComponentLookup. Also I can't use ScheduleParallel because of that.
    I am not sure it's right solution, so would like to get an advice
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,976
    Single-threaded is usually going to be the most efficient here, at least for the modification part. Most likely, the expensive bit is going to be checking if Entity_1 is in range of Entity_2, as this can easily devolve into an O(n^2) algorithm when more entities are involved.

    If you really want things to be parallel, there are a few different ways to do it. This is what I would personally use for such a problem (not a recommendation per-se): https://github.com/Dreaming381/Lati...hock Physics#findpairs--a-multibox-broadphase
     
  3. MatanYamin

    MatanYamin

    Joined:
    Feb 2, 2022
    Posts:
    109
    Can you share your IJobEntity part?
     
  4. Sarletor

    Sarletor

    Joined:
    Jan 4, 2018
    Posts:
    6
    I am already rewritten it to event system through DynamicBuffer.
    It's not best solution from performance side, but it's enough
     
    MatanYamin likes this.