QueryResult
A result from the World:query
function.
Calling the table or the next
method allows iteration over the results. Once all results have been returned, the
QueryResult is exhausted and is no longer useful.
for id, enemy, charge, model in world:query(Enemy, Charge, Model) do
-- Do something
end
Functions
next
Returns the next set of values from the query result. Once all results have been returned, the QueryResult is exhausted and is no longer useful.
INFO
This function is equivalent to calling the QueryResult as a function. When used in a for loop, this is implicitly done by the language itself.
-- Using world:query in this position will make Lua invoke the table as a function. This is conventional.
for id, enemy, charge, model in world:query(Enemy, Charge, Model) do
-- Do something
end
If you wanted to iterate over the QueryResult without a for loop, it's recommended that you call next
directly
instead of calling the QueryResult as a function.
local id, enemy, charge, model = world:query(Enemy, Charge, Model):next()
local id, enemy, charge, model = world:query(Enemy, Charge, Model)() -- Possible, but unconventional
without
QueryResult:
without
(
) →
(
)
→
(
id
,
)
--
Iterator of entity ID followed by the requested component values
Returns an iterator that will skip any entities that also have the given components. The filtering is done at the archetype level, and so it is faster than manually skipping entities.
for id in world:query(Target):without(Model) do
-- Do something
end
snapshot
QueryResult:
snapshot
(
) →
{
{
entityId:
number
,
...
}
}
Creates a "snapshot" of this query, draining this QueryResult and returning a list containing all of its results.
By default, iterating over a QueryResult happens in "real time": it iterates over the actual data in the ECS, so changes that occur during the iteration will affect future results.
By contrast, QueryResult:snapshot()
creates a list of all of the results of this query at the moment it is called,
so changes made while iterating over the result of QueryResult:snapshot
do not affect future results of the
iteration.
Of course, this comes with a cost: we must allocate a new list and iterate over everything returned from the QueryResult in advance, so using this method is slower than iterating over a QueryResult directly.
The table returned from this method has a custom __iter
method, which lets you use it as you would use QueryResult
directly:
for entityId, health, player in world:query(Health, Player):snapshot() do
end
However, the table itself is just a list of sub-tables structured like {entityId, component1, component2, ...etc}
.
view
Creates a View of the query and does all of the iterator tasks at once at an amortized cost. This is used for many repeated random access to an entity. If you only need to iterate, just use a query.
local inflicting = world:query(Damage, Hitting, Player):view()
for _, source in world:query(DamagedBy) do
local damage = inflicting:get(source.from)
end
for _ in world:query(Damage):view() do end -- You can still iterate views if you want!