sschmid / Entitas

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

Some Style question I guess? #1057

Closed eterlan closed 1 year ago

eterlan commented 1 year ago

Take some times ues the amazing project, great works! :satisfied: Just wondering, why in the tutorial and samples, author prefer to cache the context reference in the system but not use the Contexts.sharedInstance? It might be trivial, just curious.

sschmid commented 1 year ago

Hi, do you have some links to examples that you mean?

eterlan commented 1 year ago

Hi, do you have some links to examples that you mean?

Yes sir. What I mean is why you guys prefer caching context and group in the constructor?


public sealed class BoardSystem : ReactiveSystem<GameEntity>, IInitializeSystem
{
    readonly Contexts _contexts;
    readonly IGroup<GameEntity> _pieces;

    public BoardSystem(Contexts contexts) : base(contexts.game)
    {
        _contexts = contexts;
        _pieces = contexts.game.GetGroup(AllOf(Piece, Position));
    }
    ...   
}
Is there any problem to use these things in execute method? Sounds like not what ECS suggest, but I guess it's not a big deal right?
sschmid commented 1 year ago

You can use both directly in the execute if you want and in some cases this can be better, e.g. when your game support some sort of soft-reboot where contexts re-created.

However, for me that's never the case and by caching the groups you basically avoid a dictionary lookup. References to groups like _pieces are always up to date, so it's fine to store them in a field like this