Jump to content

Sleep until one of two widgets is visible?


Recommended Posts

Posted

So I'm trying to use sleep until one of my widgets in query is visible, how would I do this?

 

Example of current code - 

 

        RS2Widget displaytaken = getWidgets().getWidgetContainingText(558, "Sorry, this display name is not available");
        RS2Widget displayabl = getWidgets().getWidgetContainingText(558, "Great! This display is available");

               keyboard.typeString(rsn);
               
               // I want to sleep after the above until " displayabl " or " displaytaken " is visible ( due to the delay of server response and also to make my script more efficient )
               // It would also be useful to know in this example for future reference, as straight the if statement below, there's another response that has a delay

                if(displaytaken != null && displaytaken.isVisible()) {

                    log("Display name is taken!");

                }
                else if(displayabl !=null && displaytaken.isVisible()) {

                    log("Setting display name!");
                    setname.hover();
                    mouse.click(false);

                }

 

Posted (edited)

I'm still pretty new to Botting and I know while/recursion loops are discouraged, but maybe this is an okay time to use one? I also haven't used widgets, but maybe this would work.

while(!displaytaken.isVisible() && !displayabl.isVisible()) {
sleep(10);
}

I don't know if I fully understand what you're asking, but I hope this helped.

Edited by jakealaka9
  • Like 1
Posted

Best way to do this without freezing the whole script when it fails is this:

 

package mac;

public interface Condition {
	
	boolean evaluate();

}

 

	public boolean waitUntil(Condition c, int interval, int tries) throws InterruptedException {
		while (!c.evaluate()) {
			sleep(interval);
			if (tries == 0)
				return false;
			tries--;
		}
		return true;
	}

 

and then to use these

waitUntil(() -> widget.isVisible(), 200, 50);
if(!widget.isVisible()) return; //or whatever

//then here your amazing code

 

Which will try to see if the widget is visible every 200ms and try 50 times. 

 

  • Like 1
Posted (edited)

Use a ConditionalSleep, it's exactly what they're for. 

The following snippet means "Sleep for 3 seconds, or until condition() returns true, where condition() is checked every 600ms"
 

new ConditionalSleep(3000, 600) {
    @Override
    public boolean condition() {
        RS2Widget widget = getWidgets().getWidgetContainingText(
                               558,
                               "Sorry, this display name is not available",
                               "Great! This display is available"
                           );
        return widget != null && widget.isVisible();
    }
}.sleep();


I already have a solution for the display name setter, if you would like to take a look. It just types some random String into the box, and then select one of the suggestions

https://github.com/Explv/Tutorial-Island/blob/master/src/sections/RuneScapeGuideSection.java#L82

Edited by Explv
  • Like 3

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...