godot-kotlin-jvm icon indicating copy to clipboard operation
godot-kotlin-jvm copied to clipboard

Getting nodes in `_ready()` method is annoying

Open Humberd opened this issue 3 years ago • 2 comments

Currently I do something like this:

@RegisterClass
class CreatureScene : Spatial() {
    private lateinit var body: CreatureBodyScene
    private lateinit var infoScene: CreatureInfoScene
    private lateinit var rigidBody: RigidBody
    private lateinit var collisionMesh: MeshInstance
    private lateinit var collisionShape: CollisionShape

    @RegisterFunction
    override fun _ready() {
        body = getNode("Creature Body")
        infoScene = getNode("Viewport/CreatureInfoScene")
        rigidBody = getNode("Collider")
        collisionMesh = getNode("Collider/CollisionMesh")
        collisionShape = getNode("Collider/CollisionShape")
    }
}

It would be cool to do it like this:

@RegisterClass
class CreatureScene : Spatial() {
    private val body: CreatureBodyScene by onReady { getNode("CreatureBody") }
    private val infoScene: CreatureInfoScene by onReady { getNode("Viewport/CreatureInfoScene") }
    private val rigidBody: RigidBody by onReady { getNode("Collider") }
    private val collisionMesh: MeshInstance by onReady { getNode("Collider/CollisionMesh") }
    private val collisionShape: CollisionShape by onReady { getNode("Collider/CollisionShape") }
}

Humberd avatar May 02 '21 14:05 Humberd

We have plans for a @InjectNode (temporary name ) annotation if I remember but it's not going to be implemented in the immediate future. Here the draft Cedric made a while ago. https://github.com/utopia-rise/godot-kotlin-jvm/pull/32

We decided to postpone that until we reach a more stable state as it's mostly QOL, not features.

CedNaru avatar May 02 '21 18:05 CedNaru

It would be cool to do it like this:

@RegisterClass
class CreatureScene : Spatial() {
    private val body: CreatureBodyScene by onReady { getNode("CreatureBody") }
    private val infoScene: CreatureInfoScene by onReady { getNode("Viewport/CreatureInfoScene") }
    private val rigidBody: RigidBody by onReady { getNode("Collider") }
    private val collisionMesh: MeshInstance by onReady { getNode("Collider/CollisionMesh") }
    private val collisionShape: CollisionShape by onReady { getNode("Collider/CollisionShape") }
}

In the meantime I just use the lazy delegate. i.e.

    private val testNode: Spatial by lazy { getNodeAs("Spatial")!! }

WinstonHartnett avatar May 30 '21 10:05 WinstonHartnett