EntityProvider - Quillraven/Fleks GitHub Wiki

EntityProvider is a new interface that got introduced in version 2.4 of Fleks. It provides a way to define your own ID creation for entities. This can be useful e.g. in network games where you want to synchronize and manage entity IDs across multiple clients.

Fleks provides a recycling EntityProvider implementation which reuses entities that get removed from the world. This implementation gets automatically used if no other provider is specified. If you want to specify your own provider then you need to set it via the entityProvider function inside the configureWorld DSL block. It is important to note that your provider must have a property of type World. This is necessary for the forEach implementation which must run within a world's context.

Here is an example code on how to register your own provider:

class CustomEntityProvider(
    override val world: World
) : EntityProvider {
    // implementation omitted
}

fun main() {
    configureWorld {
        entityProvider { CustomEntityProvider(this) }
    }
}

Here is the definition of the interface:

interface EntityProvider {

    val world: World

    fun numEntities(): Int

    fun create(): Entity

    fun create(id: Int): Entity

    operator fun minusAssign(entity: Entity)

    operator fun contains(entity: Entity): Boolean

    fun reset()

    fun forEach(action: World.(Entity) -> Unit)

}

And finally the link to the default Fleks implementation as a reference.