#Struggling to understand how to access the components I want...

1 messages · Page 1 of 1 (latest)

jaunty ravine
#

Hi guys. I'm struggling to understand what I guess is a fundamental concept. Let me explain what I want to do, and how I'd do it in a monobehaviour setting.
I'd have a Squad class with an ID (integer), let's say "1". Each squad class would have a list of members (Unit class), and each unit object would have a reference back to it's squad.

This way if a squad member gets hit, I could refer to its squad, and iterate through each member, telling them to attack the guy who just hit one of their squad members.

In my ECS system each entity has a SquadData component, that contains their squad id. If a unit gets hit, I add to it EventUnitAttacked component like so:

                    Ecb.AddComponent<EventUnitAttacked>(combat.Target, new EventUnitAttacked
                    {
                        AttackingEntity = entity,
                        AttackingSquad = squadData.SquadId
                    });```

Now how the heck do I write a job, which basically does what I described above? I'm just looking for the Execute definition I guess, or some help with understanding the concept. Obviously not looking for the whole implementation. Thanks a lot!
flat fractal
#

Not sure how your structure in ecs looks like but the easiest way would probably be:
Save units as a BufferElement in the squad entity
Each unit saves the entity of their squad
Then have a job which runs over the EventUnitAttacked and a BufferLookup for the units of the squad. With this you can iterate over all units in your squad and tell them to attack a specific enemy
(Can send a thorough solution in a few hours since I have an exam coming later)

#

But with this you wouldn't be able to scheduleparallel right out of the box, only schedule

jaunty ravine
flat fractal
jaunty ravine
flat fractal
# jaunty ravine Sadly I didn't have the time to go back to it, it's something for tomorrow

It could look like this, haven't tested it but it should give a better view of a possible implementation. But it definitely isn't the most performant one, but good for grasping what features exist

public struct Unit : IComponentData
{
    public Entity Squad;
}
public struct UnitTarget : IComponentData
{
    public Entity TargetToAttack;
}

public struct SquadUnit : IBufferElementData
{
    public Entity Unit;
}

public partial struct SquadAttackTarget : IJobEntity
{
    public BufferLookup<SquadUnit> blSquadUnit;
    public ComponentLookup<UnitTarget> clUnitTarget;
    
    public void Execute(in Unit unit, ref EventUnitAttacked e)
    {
        var attacker = e.AttackingEntity;
        var unitsInSquad = blSquadUnit[unit.Squad];
        
        foreach (var squadUnit in unitsInSquad)
        {
            var unitTarget = clUnitTarget.GetRefRW(squadUnit.Unit);
            unitTarget.ValueRW.TargetToAttack = attacker;
        }
    }
}
#

in the System you can then just call SystemAPI.GetBufferLookup<SquadUnit>() and SystemAPI.GetComponentLookup<UnitTarget>() to get the needed lookups