Jump to content

Sleep until one of two widgets is visible?


osrspking

Recommended Posts

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);

                }

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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