godot-kotlin-jvm
godot-kotlin-jvm copied to clipboard
Getting nodes in `_ready()` method is annoying
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") }
}
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.
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")!! }