tiny-ecs icon indicating copy to clipboard operation
tiny-ecs copied to clipboard

Feature Request: Entity Queries

Open charlesbthomas opened this issue 1 year ago • 1 comments

I find myself in need of the ability to query for and operate on more than one entity in systems sometimes. It would be nice tobe able to do something like:

local filter = tiny.requireAll("position", "otherComponent")
local entities = world.query(filter)

I understand that this library is considered feature complete, but this would be a handy feature!

charlesbthomas avatar Oct 23 '24 12:10 charlesbthomas

See 2nd request in #13 and https://bakpakin.github.io/tiny-ecs/doc/#System_functions: this can be done by using a vanilla system (a non-processing one, meaning it won't iterate over entities) and its entities field : )

Something like

local mySystem = tiny.system()
mySystem.filter = tiny.requireAll("position", "otherComponent")
function mySystem:update(dt)
    local entities =  self.entities
    -- operations on entities
end

or even without the update function

local mySystem = tiny.system()
mySystem.filter = tiny.requireAll("position", "otherComponent")
-- …
local filteredEntities =  mySystem.entities
-- operations on entities

Fringale avatar Jan 22 '25 15:01 Fringale

I know this is stale, but I would recommend to set up a system as above - this will be the most efficient. Otherwise, you could simply make a utility function to iterate through world.entities and collect entities you are looking for.

bakpakin avatar Aug 31 '25 18:08 bakpakin