Skip to content

Runnables

GodDragoner edited this page Jun 19, 2018 · 4 revisions

What are runnables?

Runnables in TAJ can be used to schedule or time a specific piece of code. This means you can either schedule a code to run after x seconds or you can run code every x seconds no matter what part of the code you are in.

How to use them?

var TeaseRunnable = Java.type("me.goddragon.teaseai.api.runnable.TeaseRunnable");
var MyRunnable = Java.extend(TeaseRunnable , {
    run: function() {
        setTypeSpeed("INSTANT");
        sendMessage("Running runnable", 0);
	setTypeSpeed("MEDIUM");
    }
});
//Execute it every second with a first delay of 0
new MyRunnable().runTimer(0, 1000);

//OR

//Execute it every second with a first delay of 2000
new MyRunnable().runTimer(2000, 1000);

//OR

//Execute it once after 1 second
new MyRunnable().runLater(1000);

Example with cancel:

var TeaseRunnable = Java.type("me.goddragon.teaseai.api.runnable.TeaseRunnable");
var runs = 0;
var runnable;
var MyRunnable = Java.extend(TeaseRunnable, {
    run: function() {
        //Cancel itself after 5 runs
	if(runs == 5) {
	    runnable.cancel();
	    return;
	}
		
	setTypeSpeed("INSTANT");
        sendMessage("Running runnable" + runs, 0);
	setTypeSpeed("MEDIUM");
	runs++;
    }
});
runnable = new MyRunnable();
runnable.runTimer(0, 1000);

//... Your code and stuff
//And somewhere else you can also do this:
runnable.cancel();

Precision

How precise are the runnables when it comes to the timing? Well it heavily depends on the code that is running. The more inputs you use the more unprecise they will get. Why? Because while waiting for a sub message it can't check for runnables or anything else. However normally they are pretty precise and only tend to be 0 - 50 millis to late. But make sure to have a backup check if you really want a precise timing (for example compare unix timestamp at start to current timestamp).