Skip to content

Registering your code

Register your script classes and the members Godot needs to use by adding annotations.

The guide uses the default registration settings. @Script exposes a class, @Export exposes a property in the Inspector, and @Register exposes a function to Godot.

A Godot subclass uses @Script. @Export makes a property editable in the Inspector, and @Register makes an ordinary function callable by Godot, another script, or the editor.

For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.game

import godot.annotation.Export
import godot.annotation.Register
import godot.annotation.Script
import godot.api.Node
import godot.global.GD

@Script
class Player : Node() {
    @Export
    var health: Int = 100

    @Register
    fun heal(amount: Int) {
        health = (health + amount).coerceAtMost(100)
    }

    override fun _ready() {
        GD.print("Player is ready")
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.example.game;

import godot.annotation.Export;
import godot.annotation.Register;
import godot.annotation.Script;
import godot.api.Node;
import godot.global.GD;

@Script
public class Player extends Node {
    @Export
    public int health = 100;

    @Register
    public void heal(int amount) {
        health = Math.min(health + amount, 100);
    }

    @Override
    public void _ready() {
        GD.print("Player is ready");
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
package com.example.game

import godot.annotation.{Export, Register, Script}
import godot.api.Node
import godot.global.GD

@Script
class Player extends Node {
  @Export
  var health: Int = 100

  @Register
  def heal(amount: Int): Unit = {
    health = Math.min(health + amount, 100)
  }

  override def _ready(): Unit = {
    GD.print("Player is ready")
  }
}