Jump to content

Better Conditional Sleep


apa

Recommended Posts

I was messing around with the conditional sleep class last night and it was very inconsistent. The deviations, intervals and sleep times seemed to have random effects and I couldn't figure out what the problem was. I ended up pretty much rewriting a conditional sleep and now it works how I expected it should.

import org.osbot.rs07.script.MethodProvider;

public abstract class BotTimer {
    
    int maxSleep;
    int intervalCheck;
    int deviation;
    
    public BotTimer(int maxSleep, int intervalCheck) {
        this.maxSleep = maxSleep;
        this.intervalCheck = intervalCheck;
        deviation = 0;
    }
    
    public BotTimer(int maxSleep, int intervalCheck, int deviation) {
        this.maxSleep = maxSleep;
        this.intervalCheck = intervalCheck;
        this.deviation = deviation;
    }
    
    public boolean sleep() throws InterruptedException {
        int randSleep;
        while (maxSleep > 0 && !breakCondition()) {
            randSleep = MethodProvider.random(intervalCheck - deviation, intervalCheck + deviation);
            maxSleep -= randSleep;
            MethodProvider.sleep(randSleep);
        }
        return true;
    }
    
    public abstract boolean breakCondition();

}

Using it in your bot would still be the same:

new BotTimer(5000, 100) {
    public boolean breakCondition() {
        return interfaceOnScreen(465, 21, 25);
    }
}.sleep();

This timer would, at a maximum, call the breakCondition() up to 50 times exactly, or until the interface is shown on screen. Whereas, with ConditionalSleep(5000, 100), it would've execute around 1800 times for me.

Edited by apa
Link to comment
Share on other sites

  • 8 months later...

Since conditionalSleep is no longer in the API documentation, is it preferred to use this method? How well does it work compared to the one from the API? 

 

you don't see it in the api documents but it still part of osbot.

 

example call conditional sleep to have it wait

            if (getInventory().contains(getEnergy())) {
                getBank().depositAll();
                new ConditionalSleep(2000, 100) {
                    @Override
                    public boolean condition() throws InterruptedException {
                        return getInventory().isEmpty();
                    }
                }.sleep();
            }
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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