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

What's the difference between "IGroup.OnEntityAdded" and ReativeSystem when using? #953

Closed atkdefender closed 3 years ago

atkdefender commented 3 years ago

I write some code below. Found that they looks like doing the same thing. If they are totally equal, I will write the shorter one ;)

public sealed class MoveReactiveSystem : ReactiveSystem<GameEntity>,IInitializeSystem
{
    readonly Contexts _contexts;
    public MoveReactiveSystem(Contexts contexts) : base(contexts.game)
    {
        _contexts = contexts;
    }

    public void Initialize()
    {
        _contexts.game.GetGroup(GameMatcher.Moveable).OnEntityAdded += (group, entity, index, component) => {
            Debug.Log("GroupTrigger [" + entity.creationIndex + "]:" + entity.isMoveable);
        };
    }
    protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context)
    => context.CreateCollector(GameMatcher.Moveable.Added());

    protected override bool Filter(GameEntity entity) => entity.isMoveable;

    protected override void Execute(List<GameEntity> entities)
    {
        foreach(var entity in entities)
        {
            Debug.Log("ReactiveSys [" + entity.creationIndex + "]:" + entity.isMoveable);
        }
    }
}
rglobig commented 3 years ago

The difference is that the OnEntityAdded method is called immediately after the change happened. The Execute method of the Reactive System is relative to the execution order you do yourself with Systems or Feature. I suggest to only use the group events if you really need it. More infos: https://github.com/sschmid/Entitas-CSharp/wiki/Systems

atkdefender commented 3 years ago

The difference is that the OnEntityAdded method is called immediately after the change happened. The Execute method of the Reactive System is relative to the execution order you do yourself with Systems or Feature. I suggest to only use the group events if you really need it. More infos: https://github.com/sschmid/Entitas-CSharp/wiki/Systems

Ah...I forget ReactiveSys can react in order. Thx for reminding, before I went wrong.