0
\$\begingroup\$

I'm trying to setup simplest Ashley project with only 2 entities and get this error:

Exception in thread "LWJGL Application" java.lang.IllegalStateException: createComponent(T::class.java).apply(configure) must not be null

Here is the code:

class AshleyScreen(game: Main): KtxScreen {

    private val engine = Engine()

    override fun show() {
        engine.add {
            entity {
                with<DrawableComponent> {
                    sprite = Sprite(Texture("image1.png"))
                }
                with<TransformComponent> {
                    position.set(0f, 400f)
                }
            }
            entity {
                with<DrawableComponent> {
                    sprite = Sprite(Texture("image2.png"))
                }
                with<TransformComponent> {
                    position.set(500f, 50f)
                }
            }
        }
        val renderSystem = RenderSystem()
        engine.addSystem(renderSystem)
    }

    override fun render(delta: Float) {
        engine.update(delta)
    }
}

// Components
class DrawableComponent(var sprite: Sprite): Component
class TransformComponent(val position: Vector2 = Vector2()): Component

//Systems
class RenderSystem: IteratingSystem(allOf(DrawableComponent::class).get()) {
    val batch = SpriteBatch()
    val tm = ComponentMapper.getFor(TransformComponent::class.java)
    val dm = ComponentMapper.getFor(DrawableComponent::class.java)

    override fun processEntity(entity: Entity, deltaTime: Float) {
        val t = tm[entity]
        val d = dm[entity]
        d.sprite.setPosition(t.position.x, t.position.y)

        clearScreen(0.8f, 0.8f, 0.8f)
        batch.begin()
        d.sprite.draw(batch)
        batch.end()
    }
}

Compiler complains on engine.add line.

\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

Well, you should just add default values for both components:

class DrawableComponent(var sprite: Sprite = Sprite(Texture("image.png"))): Component
class TransformComponent(val position: Vector2 = Vector2()): Component
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .