Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

searching for components with specific data #123

Open
fl215 opened this issue Oct 6, 2024 · 5 comments
Open

searching for components with specific data #123

fl215 opened this issue Oct 6, 2024 · 5 comments
Labels
feature New feature or request needs design Needs more planning before continuing

Comments

@fl215
Copy link

fl215 commented Oct 6, 2024

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
@fl215 fl215 added the feature New feature or request label Oct 6, 2024
@fl215
Copy link
Author

fl215 commented Oct 6, 2024

additional information: you could rename where to whereAll and add a whereAny where any of the conditions must pass

@LastTalon LastTalon added the needs design Needs more planning before continuing label Oct 7, 2024
@LastTalon
Copy link
Member

This seems like a decent idea, but needs more consideration for the precise API and how it fits into our future plans for queries and relationships.

@jackTabsCode
Copy link
Contributor

jackTabsCode commented Oct 7, 2024

I would also note that in this specific example, you could benefit from just giving your entities with the Tool component additional components like Weapon and Potion. You'd also get increased performance out of Matter by doing so.

@fl215
Copy link
Author

fl215 commented Oct 7, 2024

I would also note that in this specific example, you could benefit from just giving your entities with the Tool component additional components like Weapon and Potion. You'd also get increased performance out of Matter by doing so.

you are right, I should have thought more about the examples, these examples may not seem very great but in more complex game structures it might pose a benefit.

@jackTabsCode
Copy link
Contributor

@fl215 Are you satisfied with the solutions we've come up with here? If so, we can close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request needs design Needs more planning before continuing
Projects
None yet
Development

No branches or pull requests

3 participants