Skip to content

searching for components with specific data #123

@unforseenconsequ3nces

Description

@unforseenconsequ3nces

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or requestneeds designNeeds more planning before continuing

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions