Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add coroutines await() to signals #660

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
47 changes: 47 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
Checks: 'clang-diagnostic-*,clang-analyzer-*,-*,cppcoreguidelines-pro-type-member-init,modernize-redundant-void-arg,modernize-use-bool-literals,modernize-use-default-member-init,modernize-use-nullptr,readability-braces-around-statements,readability-redundant-member-init'
WarningsAsErrors: ''
HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false
FormatStyle: none
CheckOptions:
- key: cert-dcl16-c.NewSuffixes
value: 'L;LL;LU;LLU'
- key: cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField
value: '0'
- key: cppcoreguidelines-explicit-virtual-functions.IgnoreDestructors
value: '1'
- key: cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic
value: '1'
- key: cppcoreguidelines-pro-type-member-init.IgnoreArrays
value: '1'
- key: cppcoreguidelines-pro-type-member-init.UseAssignment
value: '1'
- key: google-readability-function-size.StatementThreshold
value: '800'
- key: google-readability-namespace-comments.ShortNamespaceLines
value: '10'
- key: google-readability-namespace-comments.SpacesBeforeComments
value: '2'
- key: modernize-loop-convert.MaxCopySize
value: '16'
- key: modernize-loop-convert.MinConfidence
value: reasonable
- key: modernize-loop-convert.NamingStyle
value: CamelCase
- key: modernize-pass-by-value.IncludeStyle
value: llvm
- key: modernize-replace-auto-ptr.IncludeStyle
value: llvm
- key: modernize-use-bool-literals.IgnoreMacros
value: '0'
- key: modernize-use-default-member-init.IgnoreMacros
value: '0'
- key: modernize-use-default-member-init.UseAssignment
value: '1'
- key: modernize-use-nullptr.NullMacros
value: 'NULL'
- key: readability-braces-around-statements.ShortStatementLines
value: '0'
...

10 changes: 10 additions & 0 deletions .github/workflows/deploy_jvm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ jobs:
run: |
modules/kotlin_jvm/kt/gradlew -p modules/kotlin_jvm/kt/ :godot-library:publish publish -Prelease

- name: Publish godot-coroutine-library debug
shell: sh
run: |
modules/kotlin_jvm/kt/gradlew -p modules/kotlin_jvm/kt/ :godot-coroutine-library:publish publish -Pdebug

- name: Publish godot-coroutine-library release
shell: sh
run: |
modules/kotlin_jvm/kt/gradlew -p modules/kotlin_jvm/kt/ :godot-coroutine-library:publish publish -Prelease

- name: Publish godot-plugins-common
shell: sh
run: |
Expand Down
1 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ nav:
- Signals: user-guide/signals.md
- Functions: user-guide/functions.md
- Exporting: user-guide/exporting.md
- Coroutines: user-guide/coroutines.md
- Debugging: user-guide/debugging.md
- Consuming libraries: user-guide/consuming-libraries.md
- Versioning: user-guide/versioning.md
Expand Down
24 changes: 24 additions & 0 deletions docs/src/doc/user-guide/coroutines.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Using Kotlin coroutines in Godot

Coroutines are an opt-in feature that require an additional import in Kotlin.
We follow the same logic and keep them separated from the main library.

To use it, you need to add the following to your build.gradle:

```kotlin
godot {
enableGodotCoroutines.set(true)
}
```

It will automatically import our coroutine library and `kotlinx.coroutine` as a dependency.
That library adds a Godot specific coroutine scope and extensions to signals.
To use them, you simply need to write the following:

```kotlin
fun myMethod() = godotCoroutine {
doSomething()
mySignal.await() // the current coroutine will suspend until that signal is emitted.
doSomething2()
}
```
20 changes: 10 additions & 10 deletions harness/tests/Spatial.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

[ext_resource type="Script" path="res://scripts/godot/tests/Invocation.gdj" id="1"]

[sub_resource type="NavigationMesh" id="NavigationMesh_prd4u"]
[sub_resource type="NavigationMesh" id="NavigationMesh_1ex3u"]

[sub_resource type="NavigationMesh" id="NavigationMesh_tuyoa"]
[sub_resource type="NavigationMesh" id="NavigationMesh_q3pqa"]

[sub_resource type="NavigationMesh" id="NavigationMesh_bh4po"]
[sub_resource type="NavigationMesh" id="NavigationMesh_hn8kg"]

[sub_resource type="NavigationMesh" id="NavigationMesh_bdc7j"]
[sub_resource type="NavigationMesh" id="NavigationMesh_87cay"]

[sub_resource type="NavigationMesh" id="NavigationMesh_abcao"]
[sub_resource type="NavigationMesh" id="NavigationMesh_b12lh"]

[node name="Spatial" type="Node3D" node_paths=PackedStringArray("button")]
script = ExtResource("1")
button = NodePath("CanvasLayer/Button")
resource_test = SubResource("NavigationMesh_prd4u")
resource_test = SubResource("NavigationMesh_1ex3u")
jvm_id = 319061373
nav_meshes = Array[NavigationMesh]([SubResource("NavigationMesh_tuyoa")])
nullable_array = Array[NavigationMesh]([SubResource("NavigationMesh_bh4po"), null])
nav_meshes = Array[NavigationMesh]([SubResource("NavigationMesh_q3pqa")])
nullable_array = Array[NavigationMesh]([SubResource("NavigationMesh_hn8kg"), null])
nav_meshes_dictionary = {
"AwesomeNavmesh": SubResource("NavigationMesh_bdc7j")
"AwesomeNavmesh": SubResource("NavigationMesh_87cay")
}
nullable_dictionary = {
"notnull": SubResource("NavigationMesh_abcao"),
"notnull": SubResource("NavigationMesh_b12lh"),
"null": null
}

Expand Down
2 changes: 1 addition & 1 deletion harness/tests/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import org.jetbrains.kotlin.konan.target.HostManager
import java.io.OutputStream

plugins {
// no need to apply kotlin jvm plugin. Our plugin already applies the correct version for you
Expand All @@ -14,6 +13,7 @@ repositories {
godot {
registrationFileBaseDir.set(projectDir.resolve("scripts").also { it.mkdirs() })
isRegistrationFileHierarchyEnabled.set(true)
enableGodotCoroutines.set(true)

//uncomment to test android
// isAndroidExportEnabled.set(true)
Expand Down
3 changes: 1 addition & 2 deletions harness/tests/scripts/CoreTypePropertyChecks.gdj
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ signals = [
]
properties = [
exported_normal_core_type,
normal_core_type,
normal_non_null_core_type
normal_core_type
]
functions = [

Expand Down
4 changes: 1 addition & 3 deletions harness/tests/scripts/godot/tests/Invocation.gdj
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,5 @@ functions = [
set_vector3_in_packed_array,
resize_vector3_packed_array,
is_sent_xr_same_instance_as_jvm_singleton,
nullable_string_is_null,
nullable_return_type,
create_variant_array_of_user_type
]
]
4 changes: 3 additions & 1 deletion harness/tests/scripts/godot/tests/JavaTestClass.gdj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ properties = [
exported_string,
exported_byte,
exported_button,
signal_emitted
signal_emitted,
variant_array,
dictionary
]
functions = [
greeting,
Expand Down
25 changes: 25 additions & 0 deletions harness/tests/scripts/godot/tests/coroutine/CoroutineTest.gdj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// THIS FILE IS GENERATED! DO NOT EDIT OR DELETE IT. EDIT OR DELETE THE ASSOCIATED SOURCE CODE FILE INSTEAD
// Note: You can however freely move this file inside your godot project if you want. Keep in mind however, that if you rename the originating source code file, this file will be deleted and regenerated as a new file instead of being updated! Other modifications to the source file however, will result in this file being updated.

registeredName = CoroutineTest
fqName = godot.tests.coroutine.CoroutineTest
relativeSourcePath = src/main/kotlin/godot/tests/coroutine/CoroutineTest.kt
baseType = Object
supertypes = [
godot.Object,
godot.core.KtObject,
kotlin.Any
]
signals = [
signal_without_parameter,
signal_with_parameters,
signal_with_many_parameters
]
properties = [
step
]
functions = [
start_coroutine_without_parameter,
start_coroutine_with_parameters,
start_coroutine_with_many_parameters
]
2 changes: 2 additions & 0 deletions harness/tests/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ includeBuild("../../kt") {
substitute(module("com.utopia-rise:godot-gradle-plugin")).using(project(":godot-gradle-plugin"))
substitute(module("com.utopia-rise:godot-library-debug")).using(project(":godot-library"))
substitute(module("com.utopia-rise:godot-library-release")).using(project(":godot-library"))
substitute(module("com.utopia-rise:godot-coroutine-library-debug")).using(project(":godot-coroutine-library"))
substitute(module("com.utopia-rise:godot-coroutine-library-release")).using(project(":godot-coroutine-library"))
substitute(module("com.utopia-rise:godot-kotlin-symbol-processor")).using(project(":godot-kotlin-symbol-processor"))
substitute(module("com.utopia-rise:godot-entry-generator")).using(project(":godot-entry-generator"))
}
Expand Down
5 changes: 0 additions & 5 deletions harness/tests/src/main/java/godot/tests/JavaTestClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
import godot.Node;
import godot.annotation.*;
import godot.core.*;
import godot.signals.Signal;
import godot.signals.Signal2;
import godot.signals.SignalProvider;
import kotlin.Unit;
import kotlin.jvm.functions.Function2;
import org.jetbrains.annotations.NotNull;

@RegisterClass
Expand Down
2 changes: 1 addition & 1 deletion harness/tests/src/main/kotlin/godot/tests/FuncRefTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import godot.annotation.RegisterFunction
import godot.annotation.RegisterProperty
import godot.annotation.RegisterSignal
import godot.annotation.Rpc
import godot.core.signal
import godot.extensions.call
import godot.extensions.callDeferred
import godot.signals.signal

@RegisterClass
class FuncRefTest : Node() {
Expand Down
6 changes: 4 additions & 2 deletions harness/tests/src/main/kotlin/godot/tests/Invocation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ import godot.core.Vector3
import godot.core.asStringName
import godot.core.dictionaryOf
import godot.core.variantArrayOf
import godot.coroutines.GodotCoroutine
import godot.coroutines.await
import godot.extensions.getNodeAs
import godot.registration.Range
import godot.signals.connect
import godot.signals.signal
import godot.core.connect
import godot.core.signal
import godot.tests.subpackage.OtherScript
import godot.util.RealT
import org.joda.time.DateTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import godot.annotation.RegisterClass
import godot.annotation.RegisterFunction
import godot.annotation.RegisterProperty
import godot.annotation.RegisterSignal
import godot.core.callable.asCallable
import godot.signals.connect
import godot.signals.signal
import godot.core.asCallable
import godot.core.connect
import godot.core.signal

@RegisterClass
class LambdaCallableTest : Node() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package godot.tests.coroutine


import godot.annotation.RegisterClass
import godot.core.Dictionary
import godot.core.VariantArray
import godot.Object
import godot.annotation.RegisterFunction
import godot.annotation.RegisterProperty
import godot.annotation.RegisterSignal
import godot.core.Vector2
import godot.core.signal
import godot.coroutines.GodotCoroutine
import godot.coroutines.await
import kotlinx.coroutines.CoroutineStart

@RegisterClass
class CoroutineTest : Object() {

@RegisterSignal
val signalWithoutParameter by signal()

@RegisterSignal
val signalWithOneParameter by signal<Int>("int")

@RegisterSignal
val signalWithManyParameters by signal<Int, Float, Vector2, String>("int", "float", "vector2", "string")

@RegisterProperty
var step: Int = 0

@RegisterFunction
fun startCoroutineWithoutParameter() = GodotCoroutine {
step = 1
signalWithoutParameter.await()
step = 2
}
CedNaru marked this conversation as resolved.
Show resolved Hide resolved

@RegisterFunction
fun startCoroutineWithOneParameter() = GodotCoroutine {
step = 3
step = signalWithOneParameter.await()
}

@RegisterFunction
fun startCoroutineWithManyParameters() = GodotCoroutine {
step = 5
val (int, _, _, _) = signalWithManyParameters.await()
step = int
}

@RegisterFunction
fun startCoroutineUndispatched() = GodotCoroutine(start = CoroutineStart.UNDISPATCHED) {
step = 7
signalWithoutParameter.await()
step = 8
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import godot.annotation.RegisterClass
import godot.annotation.RegisterFunction
import godot.annotation.RegisterProperty
import godot.annotation.RegisterSignal
import godot.signals.signal
import godot.core.signal

@RegisterClass
class AbstractClassInheritanceChild: AbstractClassInheritanceParent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import godot.annotation.Export
import godot.annotation.RegisterFunction
import godot.annotation.RegisterProperty
import godot.annotation.RegisterSignal
import godot.signals.signal
import godot.core.signal

// register class annotation is optional for abstract classes
abstract class AbstractClassInheritanceParent: Node() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import godot.annotation.RegisterProperty
import godot.annotation.RegisterSignal
import godot.core.GodotNotification
import godot.global.GD
import godot.signals.signal
import godot.core.signal

@RegisterClass
class ClassInheritanceChild : ClassInheritanceParent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import godot.annotation.RegisterProperty
import godot.annotation.RegisterSignal
import godot.core.GodotNotification
import godot.global.GD
import godot.signals.signal
import godot.core.signal

@RegisterClass
open class ClassInheritanceParent : Node() {
Expand Down
Loading