Skip to content

Added/Changed filters for bevy queries#39

Merged
andreakarasho merged 16 commits intomainfrom
feat/change-detection-again
May 1, 2025
Merged

Added/Changed filters for bevy queries#39
andreakarasho merged 16 commits intomainfrom
feat/change-detection-again

Conversation

@andreakarasho
Copy link
Copy Markdown
Owner

@andreakarasho andreakarasho commented Apr 30, 2025

Finally I got it working.
Surprisingly there is not performance loss, it's actually faster once the query returns no entities.
The behaviour is quite close to Bevy. Each system stores a SystemTicks which gets updated at the begin/end of a system run.
Each SystemParam get updated throught the Lock/Unlock api.
Entity's component changed/added ticks get updated once you do something like: entity.Set(new Position()) for the moment.
I'll evaluate an alternative method.

Query<Data<Position, Velocity>,
      Changed<Position>> query;

foreach ((var pos, var vel) in query) {
   // parse entities with position changed
}
Query<Data<Position, Velocity>,
      Filter<Changed<Position>, Added<Velocity>> query;

foreach ((var pos, var vel) in query) {
   // parse entities with position changed and velocity added
}

Solves #18

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces updated filtering and query behavior to mimic Bevy’s approach while improving performance when no entities match a query. Key changes include:

  • Updates to the query builder and generated filter code with new type constraints and unconditional builder calls.
  • Integration of tick tracking in World and Archetype to mark component changes and additions.
  • Adjustments to scheduler system interfaces and query parameters across the codebase and sample projects.

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tools/TinyEcs.Generator/Program.cs Modified filter generation code and type constraints in query builders.
tests/FilterMatch.cs Test file refactored (now commented out) to match new filtering APIs.
src/World.cs Added tick tracking and integrated tick updates when modifying components.
src/Term.cs, src/Query.cs, src/Ptr.cs, src/Match.cs, src/Archetype.cs Updated API signatures and internal behaviors to use new types and tick logic.
samples/TinyEcsGame/Program.cs, samples/MyBattleground/Program.cs Updated sample code to reflect scheduler system and query API changes.
Comments suppressed due to low confidence (6)

samples/MyBattleground/Program.cs:15

  • [nitpick] Consider removing unused commented-out code blocks to enhance code readability.
// scheduler.OnUpdate((Query<Data<Position>, Changed<Position>> query) => {

tools/TinyEcs.Generator/Program.cs:82

  • Ensure that the unconditional call to 'builder.With<T{j}>();' is intentional, as its removal of the previous conditional check may affect filter inclusion logic.
var queryBuilderCalls = GenerateSequence(i + 1, "\n", j => $"builder.With<T{j}>();");

tools/TinyEcs.Generator/Program.cs:214

  • [nitpick] Review the new type constraint 'allows ref struct' to ensure it meets the design requirements for all IFilter implementations.
var genericsArgsWhere = GenerateSequence(i + 1, "\n", j => $"where T{j} : struct, IFilter<T{j}>, allows ref struct");

src/Query.cs:216

  • [nitpick] Verify that switching to 'Value.Value' for the data reference assignment preserves the intended pointer arithmetic semantics.
data.Value.Value = ref Unsafe.Add(ref reference, _startSafe);

src/Ptr.cs:50

  • [nitpick] Double-check that advancing the pointer using AddByteOffset and assigning to Value.Value maintains the desired behavior.
Value.Value = ref Unsafe.AddByteOffset(ref Value.Ref, Size);

src/Archetype.cs:123

  • Confirm that using 'row & Archetype.CHUNK_THRESHOLD' correctly computes the index for marking changes within the chunk layout.
Columns![column].MarkChanged(row & Archetype.CHUNK_THRESHOLD, ticks);

@andreakarasho andreakarasho merged commit 7fa54a1 into main May 1, 2025
1 check passed
@andreakarasho andreakarasho deleted the feat/change-detection-again branch May 1, 2025 09:42
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

Successfully merging this pull request may close these issues.

2 participants