Skip to content
minerkid08 edited this page Aug 20, 2025 · 1 revision

Usage

Most of the interaction with the library happens through an instance of the OpmodeLoader class. With it we can get all of the available opmodes, load the one we want, and call the start and update functions on it.

OpmodeLoader opmodeloader = new OpmodeLoader(); // create the instance of the OpmodeLoader class
String[] opmodes = opmodeloader.init(); // load the lua code and get a list of the available opmodes
opmodeloader.loadOpmode("exampleOpmode"); // load your opmode of choice

waitForStart();

opmodeloader.start(0); // (optional) calls the start function in the lua opmode. The first argument is the recognition id, it can be used for anything.

while(opmodeIsActive())
{
    opmodeloader.update(0.0, 0.0); // (optional) calls update on the lua opmode. The first argument is the delta time and the second is the elapsed time, they both can be used for anything.
}

opmodeloader.close(); // (optional) cleans up the lua state and frees all refrences to java objects. If not called, it gets called when a new opmode is initalised.

To make this example fully work we need to add some lua code.

Open lua/init.lua inside the quick start.

In this file we will define an opmode called exampleOpmode that prints a message on init, start and update

-- this is a comment

local exampleOpmode = { -- define an opmode with the name 'exampleOpmdoe'
    name = "exampleOpmode"
};

function exampleOpmode.init() -- define the init function
    print("init");
end
function exampleOpmode.start(recognitionId) -- define the start function
    print("start");
end
function exampleOpmode.update(deltaTime, elapsedTime) -- deine the update function 
    print("update");
end

addOpmode(exampleOpmode); -- add the opmode to the library

instructions for uploading the lua code is in Uploading section.

Exposing Functions to Lua

Now that we have some code running we should give the lua code access to more than just printing things to the logs.

In this example I will create a wrapper around the Telemetry class and expose it to lua.
Here is the class I will be starting with.

class LuaTelemetry
{
  private Telemetry telemetry;
  
  public LuaTelemetry(Telemetry telem)
  {
      telemetry = telem;
  }
  
  public void addData(String tag, String value)
  {
  	telemetry.addData(tag, value);
  }
  
  public void addLine(String tag)
  {
  	telemetry.addLine(tag);
  }
  
  public void update()
  {
  	telemetry.update();
  }
}

Simiply add the @OpmodeLoaderFunction annotation to all of the functions that you want to expose.

class LuaTelemetry
{
  private Telemetry telemetry;

  public LuaTelemetry(Telemetry telem)
  {
      telemetry = telem;
  }
  
  @OpmodeLoaderFunction
  public void addData(String tag, String value)
  {
  	telemetry.addData(tag, value);
  }
  
  @OpmodeLoaderFunction
  public void addLine(String tag)
  {
  	telemetry.addLine(tag);
  }
  
  @OpmodeLoaderFunction
  public void update()
  {
  	telemetry.update();
  }
}

Then in your opmode you can add these functions to the lua instance using a FunctionBuilder object.

FunctionBuilder builder = opmodeloader.getFunctionBuilder(); // get a FunctionBuilder instance

LuaTelemetry telem = new LuaTelemetry(telemetry); // create an instance of the object we want to add
builder.addObjectAsGlobal(telem); // add the object as a global

These functions can now be accessed as globals.

addLine("line");
update();

But to better organize them we can put them inside an object.

FunctionBuilder builder = opmodeloader.getFunctionBuilder();

builder.pushTable("telem"); // push a new table on to the stack with the name telem.
LuaTelemetry telem = new LuaTelemetry(telemetry);
builder.addObjectAsGlobal(telem);

// pop a table off of the stack.
// NOTE: if you do not pop a table off of the stack than you will not be able to access it.
builder.popTable(); 

Clone this wiki locally