Skip to content

Generic Entity Config

Marko Koschak edited this page Aug 12, 2024 · 10 revisions

KorGE-Fleks uses generic entity config classes for creating game objects dynamically out of some given configuration. That configuration comes usually out of a level map object layer from Tiled or LDtk.

Some existing entity config data classes can be found in korlibs.korge.fleks.entity.config.

For creating your own entity config data classes start with following code snippet and check the explanation below.

Entity config data classes are derived from EntityConfig and contain 4 sections as shown below:

import com.github.quillraven.fleks.*
import korlibs.korge.fleks.entity.*

data class MyEntityConfig(
    override val name: String,  // (1) Name For the specific game object which is characterized by below config values

    // (2) Specific game object config which shall be changeable from outside
    private val <property1>: String,
    private val <property2>: Int,
    ...

) : EntityFactory.EntityConfig {

    // The configure function for setting up this entity with components and properties
    override fun World.entitycConfigure(entity: Entity) : Entity {
        entity.configure {
            // (3) Build up entity with components for specific game object as needed
            it += SomeComponent(
                      <property1> = this@MyEntityConnfig.<property1>,  // Take over specific game object config
                      <property2> = this@MyEntityConnfig.<property2>
                  )
            it += AnotherComponent(staticValue = 42f)  // Static configuration for game object
            ...
        }
        return entity
    }

    // (4) Init block which registers the entity config in the EntityFactory
    init {
        EntityFactory.register(this)
    }
}

Section (1) and (4) can be just taken over as is.

Section (2):

Section (3):

Layered EntityConfigs for common and specific part of game object config

TODO: Explain how to use layered EntityConfig classes for game oject setup

Live Template for EntityConfig

For saving some time to write EntityConfig data classes and also to make that process less error prone it is possible to use live templates in Intellij IDEA. Just open in Intellij Settings -> Live Templates. In the list choose Kotlin and press the + button at the top of the list box. Fill in below mentioned fields:

  • Abbreviation: fleksentityconfig
  • Description: creates a new EnityConfig data class for a Korge-fleks game
  • Template text:
import com.github.quillraven.fleks.*
import korlibs.korge.fleks.entity.*
import kotlinx.serialization.*

@Serializable @SerialName("$ENTITY_CONFIG$")
data class $ENTITY_CONFIG$(
    override val name: String
) : EntityFactory.EntityConfig {

    // Function for adding components to this entity
    override fun World.entityConfigure(entity: Entity) : Entity {
        
        entity.configure {
            
        }
        return entity
    }
    
    init {
        // Register entity config into entity factory for lookup by its name
        EntityFactory.register(this)
    }
}

Then click on Define and choose Kotlin from the drop-down menu. Finally press the Apply button to save the changes.