Skip to content

Your first Kotlin class

Let's create a first class that prints a simple hello world message when the node enters the scene tree.

Create src/main/kotlin/com/yourcompany/game/Player.kt with:

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

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

@RegisterClass
class Player : Node() {
    @RegisterFunction
    override fun _ready() {
        GD.print("Hello from Kotlin")
    }
}

Create src/main/java/com/yourcompany/game/Player.java with:

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

import godot.annotation.RegisterClass;
import godot.annotation.RegisterFunction;
import godot.api.Node;
import godot.global.GD;

@RegisterClass
public class Player extends Node {
    @RegisterFunction
    @Override
    public void _ready() {
        GD.print("Hello from Java");
    }
}

Create src/main/scala/com/yourcompany/game/Player.scala with:

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

import godot.annotation.{RegisterClass, RegisterFunction}
import godot.api.Node
import godot.global.GD

@RegisterClass
class Player extends Node {
  @RegisterFunction
  override def _ready(): Unit = {
    GD.print("Hello from Scala")
  }
}

This small example already shows the main building blocks:

  • @RegisterClass makes the class visible to Godot.
  • @RegisterFunction exposes _ready() to Godot.
  • GD.print(...) writes to the Godot output.

The signals and callables guide is a good next step once you have this basic class working.

Now trigger a build:

1
./gradlew build

Once the build completes, you will be able to use your class in Godot. Simply attach the generated gdj file (by default, generated in the gdj/ directory at the root of the project; for more details, read the user guide) to a node like you would 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 the JVM languages are compiled, you can only use newly created classes after you have built them. Otherwise, Godot will not be able to find them.

Final project structure

Depending on the language you picked, the final project should include one of these source roots:

1
2
3
4
5
6
7
src/
  main/
    kotlin/
      com/
        yourcompany/
          game/
            Player.kt
1
2
3
4
5
6
7
src/
  main/
    java/
      com/
        yourcompany/
          game/
            Player.java
1
2
3
4
5
6
7
src/
  main/
    scala/
      com/
        yourcompany/
          game/
            Player.scala