osrspking Posted January 6, 2019 Posted January 6, 2019 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); }
jakealaka9 Posted January 6, 2019 Posted January 6, 2019 (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 January 6, 2019 by jakealaka9 1
BobSmokey Posted January 6, 2019 Posted January 6, 2019 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. 1
Explv Posted January 6, 2019 Posted January 6, 2019 (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 suggestionshttps://github.com/Explv/Tutorial-Island/blob/master/src/sections/RuneScapeGuideSection.java#L82 Edited January 6, 2019 by Explv 3