Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some Style question I guess? #1057

Closed
eterlan opened this issue Feb 24, 2023 · 3 comments
Closed

Some Style question I guess? #1057

eterlan opened this issue Feb 24, 2023 · 3 comments

Comments

@eterlan
Copy link

eterlan commented Feb 24, 2023

Take some times ues the amazing project, great works! 馃槅
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
Copy link
Owner

sschmid commented Mar 8, 2023

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

@eterlan
Copy link
Author

eterlan commented Mar 9, 2023

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
Copy link
Owner

sschmid commented May 17, 2023

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

@sschmid sschmid closed this as completed May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

2 participants