Search Unity

Points & Team Scoring: Loop X that has flag, then update Y with same flag.

Discussion in 'Entity Component System' started by shaunnortonAU, Jul 15, 2019.

  1. shaunnortonAU

    shaunnortonAU

    Joined:
    Jan 19, 2018
    Posts:
    9
    Hello, I am making a team sport-like game. I am trying to get my head around scoring. I think the below information describes the scenario.

    Data & Entity Structure:

    EntityA
    - Component: Score { int Value }
    - Component: TeamRedFlag
    - Exists as part of round initialization.

    EntityB, C, D, E...
    - Component: ScorePoint { int Value }
    - Component: TeamRedFlag
    - Created during play when a goal is reached.

    I am not currently storing Entity references or buffers.
    TeamRedFlag is IComponentData { bool Flag }, not SharedComponent, and not Tag.

    Desired Outcome:
    For Team Red, Score.Value = Sum(ScorePoint.Value)

    In other words, each frame I want to process all the scorepoints and calculate the score for that team, store it in Score, then destroy the scorepoint. I will also calculate Blue team, but I'm fine with duplicate code/hardcoded teams for now.

    What I've tried:
    I could probably figure this out without Jobs, but I've tried to avoid Entities.ForEach in OnUpdate because in my early ECS scripts - for other features - I used nested ForEaches and I think it was pretty terrible.
    These same early features used SharedComponentData, but I've also avoided these too. When I used them, I found their pattern of usage either really helpful (SetFilter) or unhelpful (unable to use them like I do with components).
    I tried using the Boids as a reference for using data from one job in another, but the sample is just too complex to make sense of. I think there's a need to store data in a third place, but I'm just not sure how, when or where.
    I tried providing the job with ComponentDataFromEntity and NativeArray in various combinations. I had trouble setting data in the end.

    So right now my Job is just empty. I really don't know how to do this.

    I am open to structural changes if you're certain it'll work :)

    Thanks for your help.

    EDIT: I think this is related to 2 other recent threads.
    https://forum.unity.com/threads/cycle-in-a-cycle-by-means-of-dots.707372/
    https://forum.unity.com/threads/how...ueue-from-one-parallel-job-to-another.710393/
     
    Last edited: Jul 15, 2019
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,262
    Entities.ForEach<Score>
    IJobForEach<ScorePoint>.Run()

    Write your result to a NativeArray of size 1. That should get you at least working. You can optimize later. But really, TagComponents would simplify the problem quite a bit.