Quillraven / Fleks

Fast, lightweight, multi-platform entity component system in Kotlin
MIT License
174 stars 19 forks source link

Added entities to the arguments for lifecycle methods #105

Closed geist-2501 closed 1 year ago

geist-2501 commented 1 year ago

I was updating the Fleks Wiki to remove component hooks in place of the new lifecycle methods, when I realised the example in the Wiki was no longer possible 🗿.

val onAdd: ComponentHook<Box2dComponent> = { entity, b2dCmp ->
    b2dCmp.body = inject<PhysicWorld>().createBody( /* body creation code omitted */)
    b2dCmp.body.userData = entity // No longer possible, no entity reference from the component!
}

So instead we should pass the respective entity as a parameter to the component lifecycle methods.

override fun World.onAddComponent(entity: Entity) {
    body = inject<PhysicWorld>().createBody( /* body creation code omitted */)
    body.userData = entity // 🎉
}
geist-2501 commented 1 year ago

Yeah it was my bad, I'm 100% certain the old tests for component hooks did have an assert for the expected entity but I removed it and never replaced it 😅.