Skip to content

v3.5.0

Choose a tag to compare

@friflo friflo released this 09 Mar 16:41
· 412 commits to main since this release

New feature Map component / tag types to enum ids

Check new feature at gitbook.io docs

Arbitrary component / tag types can now be mapped to enum ids.
It simplifies code and improves performance by replacing multiple branches (required before) by a switch statement.
This enables the compiler to generate a fast jump table for all cases.

public enum StatusEffect
{
    None = 0,
    [MapComponent<Burning>]   Burning,
    [MapComponent<Frozen>]    Frozen,
    [MapComponent<Poisoned>]  Poisoned,
    [MapComponent<Stunned>]   Stunned,
}

Those enum ids can than be used in a switch statement. E.g

store.OnComponentAdded += (componentChanged) =>
{
    switch (componentChanged.ComponentType.AsEnum<StatusEffect>())
    {
        case StatusEffect.Burning:  ShowFlameParticles(componentChanged.Entity);  break;
        case StatusEffect.Frozen:   ShowIceOverlay(componentChanged.Entity);      break;
        case StatusEffect.Poisoned: ShowPoisonIcon(componentChanged.Entity);      break;
        case StatusEffect.Stunned:  ShowStunStars(componentChanged.Entity);       break;
    }
};

Before multiple branches were required to implement the same behavior.

store.OnComponentAdded += (componentChanged) =>
{
    var type = componentChanged.ComponentType.Type;
    if      (type == typeof(Burning))  { ShowFlameParticles(componentChanged.Entity); }
    else if (type == typeof(Frozen))   { ShowIceOverlay(componentChanged.Entity); }
    else if (type == typeof(Poisoned)) { ShowPoisonIcon(componentChanged.Entity); }
    else if (type == typeof(Stunned))  { ShowStunStars(componentChanged.Entity); }
};

Full Description at: New feature: Map component / tag types to enum ids