Ukendio / jecs

A fast, portable Entity Component System for Luau
https://ukendio.github.io/jecs/
MIT License
121 stars 20 forks source link

EmptyQuery does improper iteration #67

Closed EncodedVenom closed 2 months ago

EncodedVenom commented 2 months ago

The EmptyQuery instance will iterate on itself instead of an empty table, leading to unintended side effects like the following:

image

Note that this iteration appears to be exactly what is inside the EmptyQuery object

small example of the code behind this repro:

for e, cframe, bul in world:query(transform_component, bullet) do
    print(e, cframe, bul)
end

In no sense is this expected behavior, and this behavior can be changed to what is expected by modifying the EmptyQuery object as follows:

local EmptyQuery = {
--------REMOVE-------------
        __iter = iterNoop -- by the way, this function doesn't exist?
------------------------------
    next = noop,
    replace = noop,
    without = function(self)
        return self
    end
}

-----------ADD--------------
setmetatable(EmptyQuery, {__iter = noop})
------------------------------

An alternative that also works:

local EmptyQuery = {
    __iter = noop,
    next = noop,
    replace = noop,
    without = function(self)
        return self
    end
}

setmetatable(EmptyQuery, EmptyQuery)