Problem
there is some boilerplate in this code that I don't think people should have to always be writing and there could be mistakes done, like writing return instead of continue
for id, tool in world:query(Tool) do
if not(tool.type == "weapon")then continue end
end
Proposed solution
fixing this solution by introducing the where method
local needs = {
[Tool] = {type = "weapon"}
}
for id, tool in world:query(Tool):where({ [Tool] = {type = "weapon"} }) do
-- code without that boilerplate
end
reading it also pretty intuitive:
query Tool where Tool's type is weapon
some more useful examples:
for id, tool, model, player in world:query(Tool, Model, Player):where({
[Tool] = {type = "weapon"},
[Player] = {name = "Loduha", id = 23},
}) do
-- query tool, model and player where the tool's type is weapon and the player's name and id are Loduha and 23 respectively
end
there can also be functions
local acceptedToolTypes = {
["weapon"] = true,
["potion"] = true,
}
local needs = {
[Tool] = function(tool) return acceptedToolTypes[tool.type] end
}
for id, tool, model, player in world:query(Tool, Model, Player):where(needs) do
-- query the tool where the tool's type is either weapon or potion
end
Problem
there is some boilerplate in this code that I don't think people should have to always be writing and there could be mistakes done, like writing
returninstead ofcontinueProposed solution
fixing this solution by introducing the
wheremethodreading it also pretty intuitive:
query Tool where Tool's type is weapon
some more useful examples:
there can also be functions