Skip to content

Working with Godot types

Find child nodes, instantiate scenes, and update Godot values from JVM code.

Find a child node

For a scene with a child named Timer, the following code accesses it from _ready(). getNode returns a nullable node; cast it to the expected type.

1
2
3
4
5
6
7
import godot.api.Timer
import godot.core.NodePath
import godot.extension.getNodeAs

val child = getNode(NodePath("Timer")) // Node?, nullable
val timer = getNodeAs<Timer>("Timer")
timer?.start(1.0)
1
2
3
4
5
6
7
import godot.api.Timer;
import godot.core.NodePath;

Timer timer = (Timer) getNode(new NodePath("Timer"));
if (timer != null) {
    timer.start(1.0);
}
1
2
3
4
5
6
7
import godot.api.Timer
import godot.core.NodePath

val timer = getNode(new NodePath("Timer")).asInstanceOf[Timer]
if (timer != null) {
  timer.start(1.0)
}

Load a scene

This example loads a PackedScene named bullet.tscn, instantiates its Node2D root, and adds it as a child from a node method.

1
2
3
4
5
6
7
8
9
import godot.api.Node2D
import godot.api.PackedScene
import godot.global.GD

val scene = GD.load<PackedScene>("res://bullet.tscn")
if (scene != null) {
    val bullet = scene.instantiate() as Node2D
    addChild(bullet)
}
1
2
3
4
5
6
7
8
9
import godot.api.Node2D;
import godot.api.PackedScene;
import godot.global.GD;

PackedScene scene = GD.load("res://bullet.tscn");
if (scene != null) {
    Node2D bullet = (Node2D) scene.instantiate();
    addChild(bullet);
}
1
2
3
4
5
6
7
8
9
import godot.api.Node2D
import godot.api.PackedScene
import godot.global.GD

val scene = GD.load[PackedScene]("res://bullet.tscn")
if (scene != null) {
  val bullet = scene.instantiate().asInstanceOf[Node2D]
  addChild(bullet)
}

Value types are copies

Assign value types back

Value types such as Vector3 are copies when read from Godot properties. Changing the copy does not update the node.

This changes only a copy:

1
2
val node3D = Node3D()
node3D.rotation.y += 10f
1
2
Node3D node3D = new Node3D();
node3D.getRotation().setY(node3D.getRotation().getY() + 10f);
1
2
val node3D = new Node3D()
node3D.getRotation.setY(node3D.getRotation.getY + 10f)

To update rotation, change the copy and assign it back to the property:

1
2
3
val rotation = node3D.rotation
rotation.y += 10f
node3D.rotation = rotation
1
2
3
Vector3 rotation = node3D.getRotation();
rotation.setY(rotation.getY() + 10f);
node3D.setRotation(rotation);
1
2
3
val rotation = node3D.getRotation
rotation.setY(rotation.getY + 10f)
node3D.setRotation(rotation)

Kotlin's mutation helpers combine these steps and write the changed value back automatically. In Java and Scala, use the read-modify-write form above.

1
2
3
node3D.rotationMutate {
  y += 10f
}

Collections share storage

VariantArray and Dictionary share their contents with Godot. Value-type elements still need read-modify-write: retrieve the element, change it, then assign it back to the same index or key.

Use your language's type checks for engine classes and your own scripts: is in Kotlin, instanceof in Java, and pattern matching in Scala.

Use a singleton

Engine singletons are accessed through their generated class. Global helpers are on godot.global.GD.

1
2
3
4
import godot.api.Engine
import godot.global.GD

GD.print(Engine.getFramesPerSecond())
1
2
3
4
import godot.api.Engine;
import godot.global.GD;

GD.print(Engine.getFramesPerSecond());
1
2
3
4
import godot.api.Engine
import godot.global.GD

GD.print(Engine.getFramesPerSecond())