Skip to content

Commit a22d669

Browse files
committed
doc: Add scala support to documentation
1 parent c5ef5e2 commit a22d669

File tree

4 files changed

+78
-7
lines changed

4 files changed

+78
-7
lines changed

docs/src/doc/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ If you don't have Discord or you don't want to use it, please file an issue on G
4848

4949
## Supported languages
5050

51-
The main language supported is Kotlin. We do however support Java experimentally. It should be possible to support other JVM-based languages as well but this is not the focus of this project. If you want to have support for other languages, have a look at [support for other JVM-based languages](contribution/support-for-other-jvm-based-languages.md).
51+
The main language supported is Kotlin. We do however support Java and Scala experimentally. It should be possible to support other JVM-based languages as well but this is not the focus of this project. If you want to have support for other languages, have a look at [support for other JVM-based languages](contribution/support-for-other-jvm-based-languages.md).
5252

5353
## Supported platforms
5454

@@ -85,7 +85,7 @@ Contrary to the official binaries, there are two builds of the editor per Platfo
8585
`release` editors are the editors you use normally. `debug` editors provide debug symbols and are intended to provide better stacktraces in case of crashes of the editor. Please use those when submitting bugreports.
8686

8787
!!! warning
88-
This module will NOT work with the official Godot Editor and Export Templates! To be able to use Kotlin and Java scripts in Godot, you need our Editor and Export Templates builds.
88+
This module will NOT work with the official Godot Editor and Export Templates! To be able to use Kotlin, Java and Scala scripts in Godot, you need our Editor and Export Templates builds.
8989

9090
## Developer discussion
9191

docs/src/doc/user-guide/advanced/cleanup-operations.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
When running Kotlin/Java code, the JVM is embedded and managed directly by Godot, which offer little control when the game is closing.
1+
When running Kotlin/Java/Scala code, the JVM is embedded and managed directly by Godot, which offer little control when the game is closing.
22
If you use third-party library that needs resources to be freed/saved or threads to be closed.
33
To that end, we provide a simple method that allows you to register callbacks that will be called when the JVM is shutdown.
44

docs/src/doc/user-guide/api-differences.md

+19-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Godot Kotlin/JVM offers two different ways to attach scripts:
55
- Source files
66
- Registration files.
77

8-
## Source files .kt and .java
8+
## Source files .kt, .java and .scala
99

10-
Just like you would do with GDScript, you can directly attach your Kotlin/Java files to Nodes as scripts.
10+
Just like you would do with GDScript, you can directly attach your Kotlin/Java/Scala files to Nodes as scripts.
1111
This is the most straightforward method to use Kotlin scripts but not the most flexible.
1212

1313
The limitations are the following:
@@ -20,7 +20,7 @@ The limitations are the following:
2020
var test_script: MyScript = load("res://pathToScript/MyScript.kt").new() // Wrong
2121
var test_script: Node = load("res://pathToScript/MyScript.kt").new() // Correct
2222
```
23-
The same applies if you use a Godot object with a .kt/.java attached to it
23+
The same applies if you use a Godot object with a .kt/.java/.scala attached to it
2424

2525
If those limitations don't apply to you, feel free to use Kotlin source files directly.
2626
@@ -32,7 +32,7 @@ They have several benefits over source files:
3232
- .gdj can be placed wherever you want in your Godot project, you are not limited to the source set.
3333
- Each script get its own .gdj. This includes scripts in different modules and libraries.
3434
- If a source file contains several scripts. A different .gdj will be generated for each.
35-
- Registration files are language agnostic, they are generated for Kotlin and Java files with no difference.
35+
- Registration files are language agnostic, they are generated for Kotlin, Java and Scala files with no difference.
3636
- When creating a script from code using its registered name. The module is going to use the registration file as the script. Therefore, registration files are treated as the default way to use scripts inside the module.
3737
3838
By default, these files are generated into a folder called `gdj` in the root of your project.
@@ -182,6 +182,14 @@ This kind of operation can be costly so we provide extension functions which cac
182182
```
183183
///
184184
185+
/// tab | Scala
186+
```scala
187+
val stringName = StringNames.asCachedStringName("myString")
188+
val nodePath = NodePaths.asCachedNodePath("myNode/myChildNode")
189+
val snakeCaseStringName = StringNames.toGodotName("myString")
190+
```
191+
///
192+
185193
You can also use the non-cached version of them if you simply want ease of conversion:
186194
187195
/// tab | Kotlin
@@ -198,6 +206,13 @@ You can also use the non-cached version of them if you simply want ease of conve
198206
```
199207
///
200208
209+
/// tab | Scala
210+
```scala
211+
val stringName = StringNames.asStringName("myString")
212+
val nodePath = NodePaths.asNodePath("myNode/myChildNode")
213+
```
214+
///
215+
201216
202217
## Logging
203218

docs/src/doc/user-guide/signals_and_callables.md

+56
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ public MyScript extends Node {
3535
```
3636
///
3737

38+
/// tab | Scala
39+
```scala
40+
@RegisterClass
41+
class MyScript extends Node {
42+
@RegisterSignal("reverse")
43+
val mySignal: Signal1[Boolean] = Signal1.create(this, "mySignal") // Only one way to do it in Java.
44+
}
45+
```
46+
///
47+
3848
!!! warning Signal parameter count
3949
In GDScript, signals can have any number of arguments, this is not possible in Kotlin as it is a statically typed language.
4050
At the moment, you can create signals and expose them to Godot up to 16 parameters.
@@ -55,6 +65,12 @@ reverseChanged.emit(false);
5565
```
5666
///
5767

68+
/// tab | Scala
69+
```scala
70+
reverseChanged.emit(false)
71+
```
72+
///
73+
5874
## Callables
5975

6076
You can use a classic Callable referencing a Godot Object and one of its method or conveniently use to avoid to creating a separate function.
@@ -80,6 +96,19 @@ You can use a classic Callable referencing a Godot Object and one of its method
8096
```
8197
///
8298

99+
/// tab | Scala
100+
```scala
101+
val regularCallable = Callable.create(this, "myMethod".toGodotName())
102+
val customCallable = LambdaCallable1.create(
103+
classOf[Void],
104+
classOf[String],
105+
(string: String) => {
106+
println(string);
107+
}
108+
)
109+
```
110+
///
111+
83112
### Signals and Callables together
84113

85114
Kotlin and Java already have ways to pass functions around as References.
@@ -143,6 +172,33 @@ public class AnotherObject extends Object {
143172
```
144173
///
145174

175+
/// tab | Scala
176+
```scala
177+
@RegisterClass
178+
class SomeObject extends Object {
179+
@RegisterFunction
180+
def onReverseChanged(boolean reverse): Unit = {
181+
println(s"Value of reverse has changed: $reverse")
182+
}
183+
}
184+
185+
@RegisterClass
186+
class AnotherObject extends Object {
187+
@RegisterSignal("reverse")
188+
val mySignal: Signal1[Boolean] = Signal1.create(this, "mySignal")
189+
190+
private val targetObject = new SomeObject()
191+
192+
public AnotherObject() {
193+
// Here are 3 different ways to connect a signal to a registered method. The method reference syntax is not implemented for Java.
194+
mySignal.connect(Callable.create(targetObject, StringNames.toGodotName("onReverseChanged"))) // The recommanded way.
195+
mySignal.connect(Callable.create(targetObject, "on_reverse_changed")) // Unsafe, try to use snake_case in your code as least as possible.
196+
connect("my_signal", Callable.create(targetObject, "on_reverse_changed")) // Really, don't do that.
197+
}
198+
}
199+
```
200+
///
201+
146202
You can also use Kotlin lambdas directly to subscribe to signals
147203

148204
```kt

0 commit comments

Comments
 (0)