Skip to content

Commit 36b4f63

Browse files
authored
Update doc regarding caching (#699)
* Update doc regarding caching
1 parent ed7b18b commit 36b4f63

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,22 +168,32 @@ Currently this feature except abstract classes.
168168

169169
## Caching
170170

171-
Several Godot functions take `StringName` or `NodePath` as a parameter.
171+
Several Godot functions take `StringName` or `NodePath` as a parameter.
172+
It's often more convenient to directly use a String that need to be converted.
172173

173-
Extension functions have been provided `String.asStringName()`, `String.asNodePath()` and `StringName.asNodePath()`, to allow for the easy creation of these.
174-
As an optimisation, these `StringName` and `NodePath` instances are cached to reduce the overhead of creating new instances unnecessarily.
174+
This operation can be costly so we provide extension functions which cache the result of the conversion for later calls:
175+
176+
- `String.asCachedStringName()`
177+
- `String.asCachedNodePath()`
178+
- `StringName.asCachedNodePath()`
175179

176180
```kotlin
177181
// This first call to the extension function creates the cache entry.
178-
val firstCall = "Test".asStringName()
182+
val firstCall = "Test".asCachedStringName()
179183

180184
// This second call for the same String value, will return the previously cached instance.
181-
val secondCall = "Test".asStringName()
185+
val secondCall = "Test".asCachedStringName()
182186

183187
// This third call will create a second entry in the cache due to the different key value.
184-
val thirdCall = "OtherTest".asStringName()
188+
val thirdCall = "OtherTest".asCachedStringName()
185189
```
186190

191+
You can also use the non-cached version of them if you simply want ease of conversion:
192+
193+
- `String.asStringName()`
194+
- `String.asNodePath()`
195+
- `StringName.asNodePath()`
196+
187197
## Logging
188198

189199
If you want logs to appear both in CLI and in the Godot Editor you will have to use the print functions inside the `GD` singleton like:

kt/godot-library/src/main/kotlin/godot/core/bridge/NodePath.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,17 @@ class NodePath : NativeCoreType {
169169
}
170170

171171
fun String.asNodePath(): NodePath {
172-
return MemoryManager.getOrCreateNodePath(this)
172+
return NodePath(this)
173173
}
174174

175175
fun StringName.asNodePath(): NodePath {
176+
return NodePath(this.toString())
177+
}
178+
179+
fun String.asCachedNodePath(): NodePath {
176180
return MemoryManager.getOrCreateNodePath(this)
177181
}
182+
183+
fun StringName.asCachedNodePath(): NodePath {
184+
return MemoryManager.getOrCreateNodePath(this.toString())
185+
}

kt/godot-library/src/main/kotlin/godot/core/memory/MemoryManager.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,6 @@ internal object MemoryManager {
113113
}
114114
}
115115

116-
fun getOrCreateNodePath(key: StringName): NodePath {
117-
return getOrCreateNodePath(key.toString())
118-
}
119-
120116
fun registerCallback(callback: () -> Unit) {
121117
cleanupCallbacks.add(callback)
122118
}

0 commit comments

Comments
 (0)