Skip to content

Add optional component query filter - #365

Merged
waynemwashuma merged 5 commits into
wimaengine:devfrom
waynemwashuma:optional-filter
Sep 21, 2026
Merged

waynemwashuma merged 5 commits into
wimaengine:devfrom
waynemwashuma:optional-filter

Conversation

@waynemwashuma

Copy link
Copy Markdown
Collaborator

Objective

Introduces an optional ECS query filter that allows queries to return entities even when a selected component is absent.

Solution

Queries now distinguish between required and optional component descriptors. Optional components do not affect archetype matching and resolve to undefined when absent.

Changes Made

  • Added the Optional query filter and optional() helper.
  • Added QueryFilter.isRequired() for determining which components must exist in an archetype.
  • Updated queries to match archetypes using only required descriptors.
  • Added type-level filter transforms so optional components are typed as Component | undefined.
  • Applied transformed tuple types to get(), single(), each(), and eachCombination().
  • Exported the new filter types.

Why This Approach

This keeps the component selection order unchanged while allowing systems to access related components without creating separate queries for every component combination. The type transformation also ensures consumers handle potentially absent components safely.

Showcase

Before

const query = new Query(world, [Position, Velocity])

Only entities containing both Position and Velocity matched the query.

After

const query = new Query(world, [Position, Velocity], [optional(Velocity)])

query.each(([position, velocity]) => {
  // velocity is `Velocity | undefined`
})

Entities containing Position now match regardless of whether they contain Velocity or not.

Migration Guide

No migration required.

Checklist

  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.

@waynemwashuma waynemwashuma self-assigned this Sep 21, 2026
@waynemwashuma waynemwashuma added the type:enhancement New feature or request label Sep 21, 2026
@github-actions github-actions Bot added scope:config mod:relationship This PR/issue involves relationship package labels Sep 21, 2026
@waynemwashuma
waynemwashuma merged commit 8106d19 into wimaengine:dev Sep 21, 2026
7 checks passed
@waynemwashuma
waynemwashuma deleted the optional-filter branch September 21, 2026 12:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mod:ecs mod:relationship This PR/issue involves relationship package scope:config type:enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant