Jump to content

Instantiating separate event executor


Recommended Posts

Posted (edited)
1 hour ago, camaro 09 said:

I'm creating a framework for one script with multiple tasks.

Is it feasible to instantiate a separate event executor to run events?

What kind of other tasks are you trying to run while running the main task?

 

You could multi-thread: More Info Here

Class

Spoiler

class HelperThread implements Runnable {
    private volatile boolean run = true;
    private volatile Script script;

    public HelperThread(Script script) {
        this.script = script;
    }

    @Override
    public void run() {
        while (run) {
            if (conditionalSleep(() -> script.myPlayer().isAnimating(), 10000, 500)) {
                if (conditionalSleep(() -> !script.myPlayer().isAnimating(), 10000, 500)) {
                    script.log("No Longer Animating");
                }
            }
        }
    }

    private boolean conditionalSleep(BooleanSupplier condition, int maxTime, int checkTime) {
        long startTime = System.currentTimeMillis();
        while (!condition.getAsBoolean() && (System.currentTimeMillis() - startTime) < maxTime) {
            try {
                Thread.sleep(checkTime);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return condition.getAsBoolean();
    }

    public void stop() {
        this.run = false;
    }
}

Usage

Spoiler

import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

@ScriptManifest(name = "@Test", author = "BravoTaco", version = 0, info = "", logo = "")
public class Main extends Script {

    private HelperThread helperThread;

    @Override
    public void onStart() throws InterruptedException {
        helperThread = new HelperThread(this);
        new Thread(helperThread).start();
    }

    @Override
    public int onLoop() throws InterruptedException {

        log("Main Thread Running!");

        return random(1300, 2300);

    }

    @Override
    public void onExit() throws InterruptedException {
        helperThread.stop();
    }
}

 

Edited by BravoTaco
  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...