Skip to content

Commit 6c0a1bc

Browse files
committed
docs: add example of using onInit for hardware-dependent properties in OpMode
1 parent 59fe153 commit 6c0a1bc

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

src/nextftc/concepts/opmodes.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,29 @@ class MyOpMode : NextFTCOpMode() {
3535
}
3636
```
3737

38+
There is also an `onInit` function that can be used to create properties that depend on hardware.
39+
It runs before the main `onInit` function,
40+
so properties created with it can be used in the main `onInit` function.
41+
42+
Here is an example OpMode that uses `onInit` to create a property for a color sensor,
43+
since there is no NextFTC wrapper for color sensors.
44+
45+
```kotlin
46+
class MyOpMode : NextFTCOpMode() {
47+
val sensor by onInit { hardwareMap.get(NormalizedColorSensor::class.java, "colorSensor") }
48+
49+
init {
50+
addComponents(/* vararg components */)
51+
}
52+
53+
override fun onInit() { }
54+
override fun onWaitForStart() { }
55+
override fun onStartButtonPressed() { }
56+
override fun onUpdate() { }
57+
override fun onStop() { }
58+
}
59+
```
60+
3861
== Java
3962

4063
```java

0 commit comments

Comments
 (0)