Skip to content

Rework Signal and Callable #831

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions harness/tests/src/main/java/godot/tests/JavaTestClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

@RegisterClass
public class JavaTestClass extends Node {
@RegisterSignal
public Signal0 testSignal = Signal0.create(this, "test_signal");

@RegisterSignal(parameters = {"param1", "param2"})
public Signal2<String, String> testSignal2 = Signal2.create(this, "test_signal_2");
//@RegisterSignal
//public Signal0 testSignal = Signal0.create(this, "test_signal");
//
//@RegisterSignal(parameters = {"param1", "param2"})
//public Signal2<String, String> testSignal2 = Signal2.create(this, "test_signal_2");

// The following should NOT work as we cannot extract parameter names. The compiler checks should catch that and throw a build error
// @RegisterSignal
Expand Down Expand Up @@ -71,15 +71,15 @@ public String greeting() {
@RegisterProperty
public Dictionary<Float, String> dictionary = new Dictionary<>(Float.class, String.class);

public LambdaCallable<Void> lambdaCallable = LambdaCallable0.create(
Void.class,
() -> {
System.out.println("Hello from Callable");
return null;
}
);

public NativeCallable methodCallable = Callable.create(this, StringNames.asStringName("DummyName"));
//public LambdaCallable<Void> lambdaCallable = LambdaCallable0.create(
// Void.class,
// () -> {
// System.out.println("Hello from Callable");
// return null;
// }
//);
//
//public NativeCallable methodCallable = Callable.create(this, StringNames.asStringName("DummyName"));

@RegisterFunction
@Override
Expand All @@ -96,12 +96,12 @@ public void _ready() {

@RegisterFunction
public void connectAndTriggerSignal() {
connect(
StringNames.asStringName("test_signal"),
new NativeCallable(this, StringNames.asStringName("signal_callback")),
(int) ConnectFlags.ONE_SHOT.getId()
);
emitSignal(StringNames.asStringName("test_signal"));
//connect(
// StringNames.asStringName("test_signal"),
// new NativeCallable(this, StringNames.asStringName("signal_callback")),
// (int) ConnectFlags.ONE_SHOT.getId()
//);
//emitSignal(StringNames.asStringName("test_signal"));
}

@NotNull
Expand Down
15 changes: 8 additions & 7 deletions harness/tests/src/main/kotlin/godot/tests/FuncRefTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import godot.annotation.RegisterFunction
import godot.annotation.RegisterProperty
import godot.annotation.RegisterSignal
import godot.annotation.Rpc
import godot.core.callable0
import godot.core.callable1
import godot.core.signal0
import godot.extension.call
import godot.extension.callDeferred
import godot.extension.connectMethod

@RegisterClass
class FuncRefTest : Node() {
Expand All @@ -30,7 +31,7 @@ class FuncRefTest : Node() {

@RegisterFunction
override fun _ready() {
test.connect(this, FuncRefTest::testSignalCallback)
test.connectMethod(this, FuncRefTest::testSignalCallback)
}

@Rpc
Expand All @@ -51,12 +52,12 @@ class FuncRefTest : Node() {

@RegisterFunction
fun testCallWithoutParam() {
call(this::withoutParamCallback)
callable0(this::withoutParamCallback).call()
}

@RegisterFunction
fun testCallDeferredWithoutParam() {
callDeferred(this::withoutParamCallback)
callable0(this::withoutParamCallback).callDeferred()
}

@RegisterFunction
Expand All @@ -66,11 +67,11 @@ class FuncRefTest : Node() {

@RegisterFunction
fun testCallWithParam() {
call(this::withParamCallback, true)
callable1(this::withParamCallback).call(true)
}

@RegisterFunction
fun testCallDeferredWithParam() {
callDeferred(this::withParamCallback, true)
callable1(this::withParamCallback).callDeferred(true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package godot.tests
import godot.api.Node
import godot.api.PackedScene
import godot.api.ResourceLoader
import godot.extension.asStatic
import godot.extension.api.asStatic

object GodotStaticDelegateTest {
var ref = (ResourceLoader.load("res://Spatial.tscn") as PackedScene?).asStatic()
Expand Down
5 changes: 3 additions & 2 deletions harness/tests/src/main/kotlin/godot/tests/Invocation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ import godot.core.Vector2
import godot.core.Vector3
import godot.core.dictionaryOf
import godot.core.variantArrayOf
import godot.extension.getNodeAs
import godot.registration.Range
import godot.tests.subpackage.OtherScript
import godot.common.util.RealT
import godot.extension.api.getNodeAs
import godot.extension.connectMethod
import org.joda.time.DateTime

enum class TestEnum {
Expand Down Expand Up @@ -370,7 +371,7 @@ class Invocation : Node3D() {
val path = NodePath("CanvasLayer/Button")
val getNode2 = getNodeAs<Button>(path)

(getNodeOrNull(path) as Button?)?.pressed?.connect(
(getNodeOrNull(path) as Button?)?.pressed?.connectMethod(
invocation,
OtherScript::hookNoParam
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import godot.annotation.RegisterProperty
import godot.annotation.RegisterSignal
import godot.core.Signal3
import godot.core.asCallable
import godot.core.connect
import godot.core.signal0
import godot.core.signal3
import godot.extension.connectLambda

@RegisterClass
class LambdaCallableTest : Node() {
Expand Down Expand Up @@ -40,11 +40,11 @@ class LambdaCallableTest : Node() {

@RegisterFunction
override fun _ready() {
signalNoParam.connect {
signalNoParam.connectLambda {
hasSignalNoParamBeenTriggered = true
}

signalWithParams.connect { p0, p1, p2 ->
signalWithParams.connectLambda { p0, p1, p2 ->
signalString = p0
signalLong = p1
signalNode = p2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import godot.api.Node
import godot.annotation.RegisterClass
import godot.annotation.RegisterFunction
import godot.annotation.RegisterProperty
import godot.core.NativeCallable
import godot.core.Callable0
import godot.core.Callable1
import godot.core.Callable2
import godot.core.Callable3
import godot.core.VariantArray
import godot.core.callable3
import godot.core.variantArrayOf
import godot.global.GD

Expand All @@ -16,22 +20,28 @@ class CallableMethodBindTest: Node() {

@RegisterFunction
fun callWithMethodWithAllBinds() {
NativeCallable(this, CallableMethodBindTest::readySignalMethodBindTest).bind(1, 2, 3).call()
val unboundCallable: Callable3<Unit, Int, Int, Int> = callable3(CallableMethodBindTest::readySignalMethodBindTest)
val boundCallable: Callable0<Unit> = unboundCallable.bind(1, 2, 3)
boundCallable.call()
}

@RegisterFunction
fun callWithMethodWithTwoBinds() {
NativeCallable(this, CallableMethodBindTest::readySignalMethodBindTest).bind(2, 3).call(0)
val unboundCallable: Callable3<Unit, Int, Int, Int> = callable3(CallableMethodBindTest::readySignalMethodBindTest)
val boundCallable: Callable1<Unit, Int> = unboundCallable.bind(5, 6)
boundCallable.call(4)
}

@RegisterFunction
fun callWithMethodWithOneBind() {
NativeCallable(this, CallableMethodBindTest::readySignalMethodBindTest).bind(3).call(0, 0)
val unboundCallable: Callable3<Unit, Int, Int, Int> = callable3(CallableMethodBindTest::readySignalMethodBindTest)
val boundCallable: Callable2<Unit, Int, Int> = unboundCallable.bind(9)
boundCallable.call(7, 8)
}

@RegisterFunction
fun callWithMethodWithNoBind() {
NativeCallable(this, CallableMethodBindTest::readySignalMethodBindTest).bind().call(0, 0, 0)
callable3(CallableMethodBindTest::readySignalMethodBindTest).call(10, 11, 12)
}

@RegisterFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ class CoroutineTest : Object() {
@RegisterFunction
fun asyncLoadResource() {
godotCoroutine {
val resource = ResourceLoader.awaitLoadAs<PackedScene>("res://Spatial.tscn") { progress ->
GD.print("Resource load progress: $progress")
}
val resource = ResourceLoader.awaitLoadAs<PackedScene>("res://Spatial.tscn")

GD.print("Resource: $resource")

Expand Down
31 changes: 16 additions & 15 deletions harness/tests/src/main/kotlin/godot/tests/signal/SignalTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import godot.core.Signal2
import godot.core.VariantArray
import godot.core.Vector2
import godot.core.asStringName
import godot.core.connect
import godot.core.signal0
import godot.core.signal1
import godot.core.signal2
import godot.extension.connectLambda
import godot.extension.connectMethod
import godot.tests.subpackage.OtherScript

@RegisterClass
Expand Down Expand Up @@ -44,34 +45,34 @@ class SignalTest : Node() {
@RegisterFunction
override fun _ready() {

noParamSignalDelegate.connect(otherScript, OtherScript::hookNoParam)
oneParamSignalDelegate.connect(otherScript, OtherScript::hookOneParam)
twoParamSignalDelegate.connect(otherScript, OtherScript::hookTwoParam)
noParamSignalDelegate.connectMethod(otherScript, OtherScript::hookNoParam)
oneParamSignalDelegate.connectMethod(otherScript, OtherScript::hookOneParam)
twoParamSignalDelegate.connectMethod(otherScript, OtherScript::hookTwoParam)

noParamSignalDelegate.connect { println("noParam signal emitted") }
oneParamSignalDelegate.connect { b -> println("oneParam signal emitted with $b") }
twoParamSignalDelegate.connect { p0, p1 -> println("twoParam signal emitted with $p0 and $p1") }
noParamSignalDelegate.connectLambda { println("noParam signal emitted") }
oneParamSignalDelegate.connectLambda { b -> println("oneParam signal emitted with $b") }
twoParamSignalDelegate.connectLambda { p0, p1 -> println("twoParam signal emitted with $p0 and $p1") }

noParamSignalDelegate.emit()
oneParamSignalDelegate.emit(false)
twoParamSignalDelegate.emit("My Awesome param !", this)


noParamSignalField.connect(otherScript, OtherScript::hookNoParam)
oneParamSignalField.connect(otherScript, OtherScript::hookOneParam)
twoParamSignalField.connect(otherScript, OtherScript::hookTwoParam)
noParamSignalField.connectMethod(otherScript, OtherScript::hookNoParam)
oneParamSignalField.connectMethod(otherScript, OtherScript::hookOneParam)
twoParamSignalField.connectMethod(otherScript, OtherScript::hookTwoParam)

noParamSignalField.connect { println("noParam signal emitted") }
oneParamSignalField.connect { b -> println("oneParam signal emitted with $b") }
twoParamSignalField.connect { p0, p1 -> println("twoParam signal emitted with $p0 and $p1") }
noParamSignalField.connectLambda { println("noParam signal emitted") }
oneParamSignalField.connectLambda { b -> println("oneParam signal emitted with $b") }
twoParamSignalField.connectLambda { p0, p1 -> println("twoParam signal emitted with $p0 and $p1") }

noParamSignalField.emit()
oneParamSignalField.emit(false)
twoParamSignalField.emit("My Awesome param !", this)


signalWithMultipleTargets.connect(this, SignalTest::targetFunctionOne)
signalWithMultipleTargets.connect(this, SignalTest::targetFunctionTwo)
signalWithMultipleTargets.connectMethod(this, SignalTest::targetFunctionOne)
signalWithMultipleTargets.connectMethod(this, SignalTest::targetFunctionTwo)
signalWithMultipleTargets.emit(Vector2(0, 0))
}

Expand Down
12 changes: 6 additions & 6 deletions harness/tests/test/unit/test_call_java_class.gd
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func test_field_set():
java_scene.free()


func test_signal():
var java_scene: JavaTestClass = load("res://java_test_scene.tscn").instantiate()
get_tree().root.add_child(java_scene)
await get_tree().create_timer(1).timeout
java_scene.connect_and_trigger_signal()
assert_true(java_scene.signal_emitted, "Signal should've been emitted in java")
#func test_signal():
# var java_scene: JavaTestClass = load("res://java_test_scene.tscn").instantiate()
# get_tree().root.add_child(java_scene)
# await get_tree().create_timer(1).timeout
# java_scene.connect_and_trigger_signal()
# assert_true(java_scene.signal_emitted, "Signal should've been emitted in java")
18 changes: 9 additions & 9 deletions harness/tests/test/unit/test_callable_method_bind.gd
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ func test_method_bind_passing_correct_binds_with_one_args_provided() -> void:
binds_test_script.call_with_method_with_two_binds()
assert_eq(
binds_test_script.method_binds[0],
0,
4,
"Incorrect bind value passed"
)
assert_eq(
binds_test_script.method_binds[1],
2,
5,
"Incorrect bind value passed"
)
assert_eq(
binds_test_script.method_binds[2],
3,
6,
"Incorrect bind value passed"
)
binds_test_script.free()
Expand All @@ -46,17 +46,17 @@ func test_method_bind_passing_correct_binds_with_two_args_provided() -> void:
binds_test_script.call_with_method_with_one_bind()
assert_eq(
binds_test_script.method_binds[0],
0,
7,
"Incorrect bind value passed"
)
assert_eq(
binds_test_script.method_binds[1],
0,
8,
"Incorrect bind value passed"
)
assert_eq(
binds_test_script.method_binds[2],
3,
9,
"Incorrect bind value passed"
)
binds_test_script.free()
Expand All @@ -66,17 +66,17 @@ func test_method_bind_passing_correct_binds_with_three_args_provided() -> void:
binds_test_script.call_with_method_with_no_bind()
assert_eq(
binds_test_script.method_binds[0],
0,
10,
"Incorrect bind value passed"
)
assert_eq(
binds_test_script.method_binds[1],
0,
11,
"Incorrect bind value passed"
)
assert_eq(
binds_test_script.method_binds[2],
0,
12,
"Incorrect bind value passed"
)
binds_test_script.free()
Loading
Loading