Skip to main content

CreateOnGo (Java)

Important

This only works if you extend the BlackOp class. More info about that here. It's essentially LinearOpMode with a few extra utilities, and it's made for use with the Blacksmith Library.

If you're using Kotlin, check out createOnGo/evalOnGo for a more versatile and idiomatic method

danger

This is partially in beta, so if you find your code crashing when you add this and it works if you remove it, don't be surprised. It should work though, from my testing.

tip

If you need to instantiate a class that needs parameters, or just want to use the return value of a method, check out @EvalOnGo instead!

You know how in a typical OpMode you have to be all like this or something?

private MyArmClass myArm;

@Override
public void init() throws InterruptedException {
myArm = new MyArmClass(...) // ugly duplication of class names & extra boilerplate smh my head
}

With @CreateOnGo, you can simply do this instead:

@CreateOnGo
private MyArmClass myArm; // (Sadly doesn't work with final variables D:)

Yep, that's it. Of course, there is one small teeny weeny itsy-bitsy caveat though:

The specific class type MUST have either a no-args constructor, or a constructor that takes in only a HardwareMap, or it will error.

If you want to use a constructor that takes in a HardwareMap only (e.g. SampleMecanumDrive), you must do @CreateOnGo(passHwMap = true) instead.

tip

On the bright side, BlackOp contains a global hwMap and a global mTelemetry you can use!

public class MyOp extends BlackOp {
@CreateOnGo
private MyClaw myClaw;

@CreateOnGo(passHwMap = true)
private SampleMecanumDrive drive;

@CreateOnGo
private MyWife maiWaife;
}

class MyClaw {
// Has no-args constructor, so it's valid
public MyClaw() {
this(BlackOp.hwMap())
}

// It's fine if there's additional multi-arg constructors
public MyClaw(HardwareMap hwMap) {
...
}
}

class SampleMecanumDrive {
// ...

// This is fine, just be sure to pass in 'passHwMap = true' to @CreateOnGo
public SampleMecanumDrive(HardwareMap hardwareMap) {
...
}

// ...
}

class MyWife {
// No no-args constructor, will throw exception when tries to @CreateOnGo
// (Also you shouldn't be objectifying women smh)
public MyWife(Tenges cost) {
...
}
}

Practical example

Link to Java example

You can check our the Kotlin version as well if you want to see how we really use it, but the syntax is much different for CreateOnGo/EvalOnGo

Link to Kotlin example