sschmid / Entitas

Entitas is a super fast Entity Component System (ECS) Framework specifically made for C# and Unity
MIT License
7.08k stars 1.11k forks source link

Is that some way to make ReplaceComponent() shorter? Sometimes I don't want to change the component data, but I do need to trigger the ReactiveSystem. #975

Closed atkdefender closed 3 years ago

atkdefender commented 3 years ago

Is that some way to make ReplaceComponent() shorter? Sometimes I don't want to change the component data, but I do need to trigger the ReactiveSystem. like: e.ReplaceCommand(e.command.cmdid, e.command.pickIds, e.command.repeat, e.command.nNum); Can it become: e.RefreshCommand();

Or maybe this is just my problem. I place a timer in the core logic. The change timing and trigger-system timing are not always the same.

mhm90 commented 3 years ago

Hi,

I used a simple extension to make Entitas trigger component replacement:

public static class EntityExtensions
{

    public static Entity TriggerComponentReplacement(this Entity entity, int componentIndex)
    {
        Assert.IsTrue(entity.HasComponent(componentIndex));
        var component = entity.GetComponent(componentIndex);
        entity.ReplaceComponent(componentIndex, component);
        return entity;
    }

    public static GameEntity TriggerComponentReplacement(this GameEntity entity, Type componentType)
    {
        var componentIndex = -1;
        for (int i = 0; i < GameComponentsLookup.componentTypes.Length; i++)
        {
            if (GameComponentsLookup.componentTypes[i] == componentType)
            {
                componentIndex = i;
                break;
            }
        }
        Assert.IsTrue(componentIndex >= 0);
        Assert.IsTrue(entity.HasComponent(componentIndex));
        var component = entity.GetComponent(componentIndex);
        entity.ReplaceComponent(componentIndex, component);
        return entity;
    }
}

And then you can use e.TriggerComponentReplacement(GameComponentsLookup.Command) to trigger change in component. I hope you'll find it helpful.

Cheers

SirMetathyst commented 3 years ago

@atkdefender if your problem has been solved, please close this issue.