From d0fb1a1b1d7f76f4ccbcc0c715a8f68daed3a929 Mon Sep 17 00:00:00 2001 From: Shane <58125798+outofbearspace@users.noreply.github.com> Date: Tue, 30 Apr 2024 18:45:49 -0700 Subject: [PATCH] return an EmptyQuery when querying for components that are not tied to any entities (#12) * Update init.lua * export Query type * replace EmptyQuery with static reference --- lib/init.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/init.lua b/lib/init.lua index 8bffd3e..907f821 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -360,8 +360,9 @@ local EmptyQuery = { without = noop } EmptyQuery.__index = EmptyQuery +setmetatable(EmptyQuery, EmptyQuery) -export type Query = typeof(setmetatable({}, EmptyQuery)) +export type Query = typeof(EmptyQuery) function World.query(world: World, ...: i53): Query local compatibleArchetypes = {} @@ -379,7 +380,7 @@ function World.query(world: World, ...: i53): Query for i, componentId in components do local map = componentIndex[componentId] if not map then - return setmetatable({}, EmptyQuery) + return EmptyQuery end if firstArchetypeMap == nil or map.size < firstArchetypeMap.size then @@ -415,7 +416,7 @@ function World.query(world: World, ...: i53): Query local lastArchetype, compatibleArchetype = next(compatibleArchetypes) if not lastArchetype then - return setmetatable({}, EmptyQuery) + return EmptyQuery end local preparedQuery = {} @@ -439,7 +440,7 @@ function World.query(world: World, ...: i53): Query lastArchetype, compatibleArchetype = next(compatibleArchetypes) if not lastArchetype then - return setmetatable({}, EmptyQuery) + return EmptyQuery end return self