Adding generic components to an entity #135
Answered
by
Quillraven
Hellwoodoo
asked this question in
Q&A
-
Just wondering why
does not work with plusAssign, and if there is an alterative to doing this or if I am doing something wrong. |
Beta Was this translation helpful? Give feedback.
Answered by
Quillraven
Jan 11, 2024
Replies: 1 comment 4 replies
-
Hi, the reason is that you are not adding a component type to an entity. You want to add a component itself (an instance of a component). That's why Fleks internally has a function like this (note the component argument instead of component type): inline operator fun <reified T : Component<T>> Entity.plusAssign(component: T) {
val compType: ComponentType<T> = component.type()
compMasks[this.id].set(compType.id)
val holder: ComponentsHolder<T> = componentService.holder(compType)
holder[this] = component
} There is also an unsafe version that takes a list of components that might fit your use-case better: operator fun Entity.plusAssign(components: List<Component<*>>) {
components.forEach { cmp ->
val compType = cmp.type()
compMasks[this.id].set(compType.id)
val holder = componentService.wildcardHolder(compType)
holder.setWildcard(this, cmp)
}
} |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
Hellwoodoo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
the reason is that you are not adding a component type to an entity. You want to add a component itself (an instance of a component). That's why Fleks internally has a function like this (note the component argument instead of component type):
There is also an unsafe version that takes a list of components that might fit your use-case better: