Skip to content

Your first class

Let's create a file src/main/kotlin/com/yourcompany/game/Simple.kt with the following content:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
package com.yourcompany.game

import godot.Spatial
import godot.annotation.RegisterClass
import godot.annotation.RegisterFunction
import godot.global.GD

@RegisterClass
class Simple: Spatial() {

    @RegisterFunction
    override fun _ready() {
        GD.print("Hello world!")
    }
}

The classes section covers in detail what we did here, but for now @RegisterClass will register the class to Godot. Now we can trigger a build.

1
./gradlew build

Once the build completes, you will be able to use your class in Godot. Simply attach the kt file containing registered class to a node like you would do in GDScript. If you rebuild the project while the editor is open, your classes will be reloaded automatically in Godot and you can use them.

Attach Node Script

Info

As Kotlin is a compiled language you can only use newly created classes after you have built them, otherwise, Godot will not be able to find them.

Configuring dependencies

The majority of the JVM ecosystem is at your disposal, just add a dependency like you would in a normal java based project to your build file:

1
2
3
dependencies {
    implementation("joda-time:joda-time:2.10.6")
}