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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: Pass traits into relations to filter target entities by #4

Open
krispya opened this issue Oct 21, 2024 · 2 comments
Open

Query: Pass traits into relations to filter target entities by #4

krispya opened this issue Oct 21, 2024 · 2 comments

Comments

@krispya
Copy link
Member

krispya commented Oct 21, 2024

When using relations in a query you can query for a specific target:

const results = world.query(Contains(gold))

Or a wildcard, which matches any entity targeted by that relation:

const results = world.query(Contains('*'))

The feature request is to extend this by passing traits which filters by any entity that is a target of the relation and also has the matching traits.

const results = world.query(Contains(IsActive))

Challenges: We are currently using the same API for both adding relations as traits and querying entities. This mixes concerns in a way that feels unsafe. Either strong typing needs to address this or there needs to be some symmetry between adding relations and querying them.

@Ctrlmonster
Copy link
Collaborator

My preferred approach would be to go for symmetry and enable bulk operations in all contexts. Examples:

isGold = trait();

// ------------------------------------------------------------------

// adding with `...trait[]` arg
myEntity.add(Contains(isGold));

// translates to
world.query(isGold).forEach(goldEntity => myEntity.add(Contains(goldEntity)))

// ------------------------------------------------------------------

// removing with `...trait[]` arg
myEntity.remove(Contains(isGold));

// translates to
world.query(isGold).forEach(goldEntity => myEntity.remove(Contains(goldEntity)))

// ------------------------------------------------------------------

// querying for a relation with `...trait[]` arg
world.query(A, Contains(isGold))

// translates to (pseudo code implementation)
containsGold = world.query(Contains(isGold))
world.query(A, Contains("*")).filter(e => containsGold.some(e2 => e2 === e.targetFor(Contains)))

@krispya
Copy link
Member Author

krispya commented Oct 23, 2024

This looks good to me. My monkee brain understands what is being expressed in each case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants