Skip to content

Instantly share code, notes, and snippets.

@SanderMertens
Last active January 22, 2021 17:46
Show Gist options
  • Select an option

  • Save SanderMertens/2b0aa555601e218ea27af1096a4cc838 to your computer and use it in GitHub Desktop.

Select an option

Save SanderMertens/2b0aa555601e218ea27af1096a4cc838 to your computer and use it in GitHub Desktop.

ECS Query DSL draft

Proposal for prolog-inspired ECS query DSL that can resolve components as well as entity relationships. Not a formal syntax/grammar definition.

Overview

Primitives

.                             // Self. Indicates iterated over entity or entity for given (arche)type
Entity                        // Entity identifier
X                             // Variable (all caps identifier)
_                             // Anonymous variable. Indicates that query is not interested in all permutations.

Type expressions:

[Position]                    // . with Position
[Position, Velocity]          // . with Position, Velocity
[(Likes, Bob)]                // . with Likes Bob

Query expressions:

.                             // Select entities with self
X                             // Select entities with X
Component                     // Select entities with Component
Component(.)                  // Select entities with Component
Component(X)                  // Constraint on X: must have Component
Position, Velocity            // Select entities with Position and Velocity
Position || Velocity          // Select entities with Position or Velocity
Position || [Speed, Velocity] // Select entities with Position or (Speed and Velocity)
Likes(., Subject)             // Select entities with (Likes, Subject)
Likes(Object, .)              // Select entities for Object with (Likes, .)
.(Object)                     // Select all entities that Object has

Examples w/evaluation pseudo code

Get all entities with Position

Position

Evaluation:

foreach . with [Position]:
  yield .

Get all entities with Position

Position(.)

Evaluation:

foreach . with [Position]:
  yield .

Get all entities that are singletons

.

Evaluation:

foreach . with [.]:
  yield .

Get all entities with Position and Velocity

Position, Velocity

Evaluation:

foreach . with [Position, Velocity]:
  yield .

Get all entities that like Bob

Likes(., Bob)

Evaluation:

foreach . with [(Likes, Bob)]:
  yield .

Get all entities Bob likes

Likes(Bob, .)

Evaluation:

Bob with [(Likes, .)]
  yield .

Get all entities that like anyone

Likes(., X)

Evaluation:

foreach . with [(Likes, X)]:
  foreach X:
    yield ., X

Get all entities anyone likes

Likes(X, .)

Evaluation:

foreach X with [(Likes, .)]
  foreach .:
    yield ., X

Get all entities that Bob has

.(Bob)

Evaluation:

Bob with [.]:
  foreach .:
    yield .

Get all entities that anyone has

.(X)

Evaluation:

foreach X with [.]:
  foreach .:
    yield ., X

Select all entities from all entities

X(.)

Evaluation:

foreach . with [X]:
  foreach X:
    yield ., X

Get all entities that like X where X likes the entity back

Likes(., X), Likes(X, .)

Evaluation:

foreach . with [(Likes, X)]
  foreach X with [(Likes, .)]:
    yield ., X

Get all entities that like X where X likes Y and Y likes the entity back

Likes(., X), Likes(X, Y), Likes(Y, .)

Evaluation:

foreach . with [(Likes, X)]
  foreach X with [(Likes, Y)]:
    foreach Y with [(Likes, .)]
      yield ., X, Y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment