Classes and inheritance
Script classes
A script inherits godot.api.Object or a derived engine class such as Node or Resource. The source language must be identifiable as Kotlin, Java, or Scala. Inferred and Explicit require @Script, directly or through a meta-annotation such as @Tool; Automatic registers every public Godot subclass. A registered class must be public and cannot be a Kotlin or Scala object: Godot creates its own instances.
The following declaration works in all three modes:
1 2 3 4 5 | |
1 2 3 4 5 | |
1 2 3 4 5 | |
Constructors
Only public no-argument constructors are exposed to Godot. A class can be registered without one, but Godot cannot instantiate it. Constructors with parameters remain JVM-only. A constructor's default arguments matter only if the compiler also emits a public no-argument constructor.
Abstract classes
@Script is optional on an abstract base that only contributes inherited members. Add it when the abstract class itself should be registered. An abstract registered class is known to Godot but cannot be instantiated or attached as a concrete script. Concrete descendants can inherit its registered declarations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
Put the classes in separate source files.
An override without annotations keeps the parent declaration's registration, and Godot calls the override. To change the registered settings, such as an RPC configuration, annotate the override and keep its parameter list: the annotated override replaces the parent's entry.
Inherited members
Registered properties, functions, and signals from parent classes are included in the descendant's effective registration. This includes selected members of intermediate parents that are not themselves registered. Interfaces can contribute registered functions.
Local declarations take precedence over inherited duplicates. Properties and signals are deduplicated by name; functions by name and parameter-type signature. Built-in engine members remain part of the engine API; inheritance does not register every built-in method again.
Names and source files
The registered Godot name and the JVM fully qualified class name are different identifiers. @Script(className = ...) changes the base registered name. Naming policies determine prefixes and sanitization. The source-file association still uses the JVM class name.
Use concrete Godot-compatible types in registered signatures. A JVM generic type parameter does not define a Godot Variant type.