Description
When one connects to a signal using a lambda:
mySignal.connect { arg1, arg2 ->
// do stuff
}
one receives a godot Error object as a return type from the connect
function.
While this allows to check if the connection was successful, one can never disconnect that lambda again from the signal.
After some discussions on discord, we settled on a design which returns a handle for that connect function call. The handle will look something like this:
Note: names not final and subject to change. Probably they will be called Connector
or similar
class SignalHandle(
private val signal: Signal,
private val callable: Callable,
) {
fun connect(flags: Int = 0): Error = signal.connect(callable, flags)
fun disconnect(): Unit = signal.disconnect(callable)
fun isConnected(): Boolean = signal.isConnected(callable)
fun isNull(): Boolean = signal.isNull()
}
The connect call taking in the lambda will then return such a handle which allows to later disconnect and reconnect the lambda at will while still retaining the fire and forget (or better setup and forget) way of working which we have today and is quite easy to use:
// setup and forget:
mySignal.connect { arg1, arg2 -> }
// ---
// using handle:
val handle = mySignal.connect { arg1, arg2 -> }
// later
if (handle.isConnected()) {
handle.disconnect()
}
// later
handle.connect()
// later
handle.disconnect()
The generated code would look like something this:
public inline fun <reified P0> Signal1<P0>.connect(flags: Int = 0, noinline method: (p0: P0) -> Unit): SignalHandle = SignalHandle(
signal = this,
callable = method.asCallable(),
).also { it.connect(flags) }
This would ideally be implemented after #760 to take advantage of the api gen's new design.