Skip to content

Commit

Permalink
Rename node helpers (#187)
Browse files Browse the repository at this point in the history
* rename node helpers to getNodeAs<>
  • Loading branch information
CedNaru authored Apr 25, 2021
1 parent d3fdf6e commit 1bee833
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions harness/tests/src/main/kotlin/godot/tests/FuncRefTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import godot.annotation.RegisterClass
import godot.annotation.RegisterFunction
import godot.annotation.RegisterProperty
import godot.annotation.RegisterSignal
import godot.call
import godot.callDeferred
import godot.extensions.call
import godot.extensions.callDeferred
import godot.signals.signal

@RegisterClass
Expand Down
9 changes: 8 additions & 1 deletion harness/tests/src/main/kotlin/godot/tests/Invocation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import godot.core.Vector2
import godot.core.Vector3
import godot.core.dictionaryOf
import godot.core.variantArrayOf
import godot.extensions.getNodeAs
import godot.signals.signal
import godot.tests.subpackage.OtherScript
import godot.util.RealT
Expand Down Expand Up @@ -364,10 +365,16 @@ class Invocation : Spatial() {
name = formerName
val test = DateTime.now() //external dependency to test dependency inclusion in dummyCompilation

val getNode = getNodeAs<Button>("CanvasLayer/Button")

val path = NodePath("CanvasLayer/Button")
val getNode2 = getNodeAs<Button>(path)

signalNoParam.connect(invocation, invocation::hookNoParam)
signalOneParam.connect(invocation, invocation::hookOneParam)
signalTwoParam.connect(invocation, invocation::hookTwoParam)
(getNodeOrNull(NodePath("CanvasLayer/Button")) as Button?)?.signalPressed?.connect(

(getNodeOrNull(path) as Button?)?.signalPressed?.connect(
invocation,
invocation::hookNoParam
)
Expand Down
10 changes: 10 additions & 0 deletions kt/godot-library/src/main/kotlin/godot/extensions/NodeExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package godot.extensions

import godot.Node
import godot.core.NodePath

@Suppress("NOTHING_TO_INLINE", "UNCHECKED_CAST")
inline fun <T : Node> Node.getNodeAs(path: String) = getNode(NodePath(path)) as T?

@Suppress("NOTHING_TO_INLINE", "UNCHECKED_CAST")
inline fun <T : Node> Node.getNodeAs(nodePath: NodePath) = getNode(nodePath) as T?
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package godot
package godot.extensions

import godot.core.NodePath
import godot.Object
import godot.core.VariantArray
import godot.signals.Signal
import godot.util.camelToSnakeCase
import kotlin.reflect.KFunction

@Suppress("NOTHING_TO_INLINE", "UNCHECKED_CAST")
inline fun <T : Node?> Node.getNode(path: String) = getNode(NodePath(path)) as T

@Suppress("NOTHING_TO_INLINE", "UNCHECKED_CAST", "EXTENSION_SHADOWED_BY_MEMBER")
inline fun <T : Node?> Node.getNode(nodePath: NodePath) = getNode(nodePath) as T

/**
* **Note:** The function name is converted to snake_case
* Use [Object.callDeferred] to call functions by string or [callDeferredRawName] for an unconverted version of this function
Expand Down

0 comments on commit 1bee833

Please sign in to comment.