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

getEntityComponents does not return component name #111

Open
Tresky opened this issue Mar 18, 2023 · 1 comment
Open

getEntityComponents does not return component name #111

Tresky opened this issue Mar 18, 2023 · 1 comment

Comments

@Tresky
Copy link
Contributor

Tresky commented Mar 18, 2023

When I call getEntityComponents for an entity it returns the instances of the components, but does not tell me what the names of those components are. Is there a preferred method for determining the name of the components that are on an entity?

I realize that there probably is no way bitecs would know the name because we don't initialize the components with a name, but perhaps there is some specific way people organize their code to be able to keep a name?

I looked into adding a name into the bitecs codebase and making a PR, but my understanding of the code is fairly prohibitive of me making changes at this point.

@NateTheGreatt
Copy link
Owner

The most convenient way would probably be to simply compare references:

const SomeComponent = defineComponent()

const entityComponents = getEntityComponents(world, eid)

for (const component of entityComponents) {
  if (component === SomeComponent) {
    // ...
  }
}

Another way could be to keep a mapping around:

const componentNameMap = new Map()
const SomeComponent = defineComponent()
componentNameMap.set("SomeComponent", SomeComponent)

You can also tack names onto the component objects themselves using symbols to avoid namespace pollution/collision:

const $componentName = Symbol("componentName")
const SomeComponent = defineComponent()
SomeComponent[$componentName] = "SomeComponent"

Or even use a symbol per component:

const $someComponent = Symbol("someComponent")
const SomeComponent = defineComponent()
SomeComponent[$someComponent] = true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants