I’m trying to build a Stat - Affect system. My first attempt was a unique system, component, and affect buffer for each stat, but it smelled like a bad design. So, I thought maybe I could use a single dynamic buffer of affects where each affect holds a reference to whichever stat component it should modify. I’m unsure if there’s a proper way to do something akin to the code below, or if it’s a bad idea:
public struct Health : IComponentData
{
public int Value;
public int MaxValue;
}
// unknown number of other stats with same fields
public struct StatAffect : IBufferElementData
{
public Type TargetComponent;
public int Value;
public int Iterations;
public float Delay;
public float Time;
}
public class StatAffectSystem : SystemBase
{
protected override void OnUpdate ()
{
// [NativeDisableParallelForRestriction]
Entities.ForEach( (Entity entity, DynamicBuffer<StatAffect> statAffectBuffer) => {
foreach (var statAffect in statAffectBuffer)
{
// get some Entity component using statAffect.TargetComponent
// logic to modify component
// write to component with updated values
}
})
.ScheduleParallel();
}
}