Jump to content
Sphiinx

[TUTORIAL] Dynamic Sleeping/Conditional Sleeping

Recommended Posts

Conditional Sleeping

upSYdVQ.png

 

 

             Description
7ANo6V1.png

  • I see a lot of new users who are just beginning scripting using static sleep methods. This is really inefficient and a bad habit because your script will sleep every time it reaches that point instead of just when it needs to.

 

  • I will be showing you how to use Tribot's method for Conditional Sleeping.

 

 

 

             What is it?
7ANo6V1.png

  • Conditional sleeping is a way to allow the script to sleep for a given amount of time until the given condition returns a true value.

 

 

 

      Why should I use it?
7ANo6V1.png

  • This will allow your script to continue without sleeping if the current action succeeded, if the action didn't succeed before the timeout it will return false. This will make the overall efficiency of your scripts will be quicker and more human like.

 

 

 

         API Information
7ANo6V1.png

 

 

 

        Implementation
7ANo6V1.png

  • Firstly you must import the Timing class and the Condition class from the Tribot API.
    • import org.tribot.api.Timing;import org.tribot.api.types.generic.Condition;

 

 

  • Next we must call Timing#waitCondition and give it the correct parameters. We will create a new condition.
    • Timing.waitCondition(new Condition() {
                  @Override
                  public boolean active() {
                      General.sleep(100); // Add this in to reduce CPU usage
                      return false;
                  }
              })

       

 

  • Lastly we must return a value and create a timeout.
    • Timing.waitCondition(new Condition() {
                  @Override
                  public boolean active() {
                      General.sleep(100);
                      return //TODO Return value
                  }
              }, General.random(1000, 1200));

       

 

 
               Example
7ANo6V1.png
  • Here is an example of this being used.
    • public void openBank() {
              if (!Banking.isBankScreenOpen()) {
                  if (Banking.openBank()) {
                      Timing.waitCondition(new Condition() {
                          @Override
                          public boolean active() {
                              General.sleep(100); // Add this in to reduce CPU usage
                              return Banking.isBankScreenOpen();
                          }
                      }, General.random(750, 1000));
                  }
              } else {
                  // Do something
              }
          }

       

Edited by Sphiinx
  • Like 10

Share this post


Link to post
Share on other sites

I think the language used in this could be edited to sound more intuitive, you are kind of describing the broad outline of conditional sleeping, rather than specifically what it does or a line by line analysis.

 

For example, you could display that the condition is simply an object which has a boolean method, which returns the boolean if it is true before the timeout, or after the timeout parameter if the boolean has not been fulfilled.

 

You could also go on to say how one should save conditions for additional usage and why a random timeout is used, e.g anti-ban.

 

Possibly even discuss why you would used wait condition over similar methods users have created in the past.

  • Like 2

Share this post


Link to post
Share on other sites

I think the language used in this could be edited to sound more intuitive, you are kind of describing the broad outline of conditional sleeping, rather than specifically what it does or a line by line analysis.

 

For example, you could display that the condition is simply an object which has a boolean method, which returns the boolean if it is true before the timeout, or after the timeout parameter if the boolean has not been fulfilled.

 

You could also go on to say how one should save conditions for additional usage and why a random timeout is used, e.g anti-ban.

 

Possibly even discuss why you would used wait condition over similar methods users have created in the past.

 

The API explains that pretty well and I didn't want to copy the API line by line when its already there for the user. Good points though, thanks!

Share this post


Link to post
Share on other sites

I thought it is useless to use timing.waitcondition, because banking.openbank will return true when the bankscreen isopen,otherwise it will return false.

 

openBank

public static boolean openBank()

Opens the bank by either RSObject or RSNPC. Will use PathFinding.aStarWalk(..) if the bank is not on the screen.
Returns: True if the bank was successfully opened; false otherwise.

Share this post


Link to post
Share on other sites
On 5/29/2016 at 8:36 PM, Sphiinx said:

Nope

Bump!

thanks for the response, Im getting a new class for every time i use the following in my script...

 

                      if (item[0].click("Use")) {
                        Timing.waitCondition(new Condition() {
                          @Override
                          public boolean active() {
                            General.sleep(100); // Add this in to reduce CPU usage
                            return Game.isUptext("item uptext");
                          }
                        }, General.random(2000, 3000));
                      }

how do you implement it so it doesn't create a new class every time?

Edited by seany123456

Share this post


Link to post
Share on other sites
Just now, seany123456 said:

thanks for the response, Im getting a new class for every time i use the following in my script...

 

                      if (item[0].click("Use")) {
                        Timing.waitCondition(new Condition() {
                          @Override
                          public boolean active() {
                            General.sleep(100); // Add this in to reduce CPU usage
                            return Game.isUptext("Use Limpwurt seed ->");
                          }
                        }, General.random(2000, 3000));
                      }

how do you implement it so it doesn't create a new class every time?

 

 

That's odd. it shouldn't be creating a new class. Depending on your IDE, If you type "Timing.waitCondition(new Condition()" or part of it, you should be able to autogenerate the rest. if that doesn't work, I would just copy and paste it into the class until you figure out the issue.

Share this post


Link to post
Share on other sites
10 minutes ago, Sphiinx said:

 

That's odd. it shouldn't be creating a new class. Depending on your IDE, If you type "Timing.waitCondition(new Condition()" or part of it, you should be able to autogenerate the rest. if that doesn't work, I would just copy and paste it into the class until you figure out the issue.

Im not sure then when i type Timing.wait eclipse gives the option of waitCondition, although i would still need to type in the newCondition(){} part in myself.

 

with this code for example: 

package scripts;

import org.tribot.api.General;
import org.tribot.api.Timing;
import org.tribot.api.types.generic.Condition;
import org.tribot.api2007.Game;
import org.tribot.script.Script;

public class TimerTest extends Script {

	@Override
	public void run() {
		// TODO Auto-generated method stub
        Timing.waitCondition(new Condition(){
            public boolean active() {
                General.sleep(100); // Add this in to reduce CPU usage
                return Game.isUptext("Test");
              }
        }, General.random(2000, 3000));
        
        Timing.waitCondition(new Condition(){
            public boolean active() {
                General.sleep(100); // Add this in to reduce CPU usage
                return Game.isUptext("Test");
              }
        }, General.random(2000, 3000));
	}
}

it gives the the following files.

timertestclass.png

Share this post


Link to post
Share on other sites

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

  • Similar Content

    • By grammatoncleric
      Super basic script, I am very very very new to Java, so please be nice... No this script is not finished, but at minimum it should at least gather trout/salmon and bank it, but i cant find the script or it hasnt loaded in tribot.
       
      A picture of my  hierarchy: ImgurLink
      package scripts.LowLevelCooker; import org.tribot.api.DynamicClicking; import org.tribot.api.General; import org.tribot.api.Timing; import org.tribot.api.types.generic.Condition; import org.tribot.api2007.*; import org.tribot.api2007.types.RSObject; import org.tribot.api2007.types.RSTile; import org.tribot.script.Script; import org.tribot.script.ScriptManifest; import org.tribot.script.interfaces.MessageListening07; // import org.tribot.script.interfaces.Painting; import java.awt.*; @ScriptManifest(authors = ("noobalicious"), name = "Low Level Gatherer", category = "Money Making", description = ("gathers trout & salmon")) public class LowLevelCooker extends Script implements MessageListening07 { final String[] AXE_NAMES = { "Bronze axe", "Iron axe", "Black axe", "Steel axe", "Mithril axe", "Adamant axe", "Rune axe", "Dragon axe"}; private final RSTile gatherTile = new RSTile(3100, 3425); private final RSTile bankTile = new RSTile(3094, 3491); private final int MINING_ANIMATION = 625; private RSObject[] depositBox; private final int DEPOSITBOX_ID = 6943; @Override public void run() { while (true) { sleep(100); if (atFood()) { if (Inventory.isFull()) { walkToBank(); } else { gatherFood(); } } else if (atBank()) { if (Inventory.isFull()) { bank(); } else { walkToFood(); } } else { if (Inventory.isFull()) { walkToBank(); } else { walkToFood(); } } } } private boolean walkToBank() { return PathFinding.aStarWalk(bankTile); } private boolean atBank() { return Player.getPosition().distanceTo(bankTile) < 5; } private boolean bank() { depositBox = Objects.findNearest(20, DEPOSITBOX_ID); if (!Banking.isDepositBoxOpen()) { if (depositBox.length > 0) { if (!DynamicClicking.clickRSObject(depositBox[0], "Deposit")) return false; } } else { if (Banking.depositAllExcept(AXE_NAMES) < 1) return false; } return Timing.waitCondition(new Condition() { @Override public boolean active() { return !Inventory.isFull(); } }, General.random(3000, 4000)); } private boolean walkToFood() { return PathFinding.aStarWalk(gatherTile); } private boolean atFood() { return Player.getPosition().distanceTo(gatherTile) < 10; } private void gatherFood() { final RSObject foodID[] = Objects.findNearest(5, 335, 333, 331, 329); RSObject foodToGather = foodID[0]; if (foodToGather.isOnScreen()) { if (DynamicClicking.clickRSObject(foodToGather, "Take")) { Timing.waitCondition(new Condition() { @Override public boolean active() { General.sleep(20, 30); return true; } }, General.random(75, 100)); } } } @Override public void duelRequestReceived(String s, String s1) { } @Override public void personalMessageReceived(String s, String s1) { } @Override public void serverMessageReceived(String s) { } @Override public void playerMessageReceived(String s, String s1) { } @Override public void tradeRequestReceived(String s) { } @Override public void clanMessageReceived(String s, String s1) { } }  
    • By MilkyWay
      iv never been able to get looking glass working since I joined, but I recently had a bit success and got it to hook on to osbuddy,
      but when I click start scripts nothing happens for me.
    • By Rabadon2013
      I dont know if im on the right section but i need help with the paypal payment option.
      I verfied my paypal email address but if i pay with paypal i get this Error message in the top of the website :Connection Exception - Got Http response code 400 when accessing https://api.paypal.com/v1/payments/payment/PAY-6AX07555PM1948029LK322YA/execute.
       pls help i dont have a credit card right now^^
      and sry for my bad english, i only have these basic english skills from the school.
    • By klown8
      I start the script and get nothing.... says this
       
      [02:00:32] Starting client. [02:03:26] Downloading script 'Tau Grand Exchange'. [02:03:35] Script Started: Tau Grand Exchange. [02:03:35] java.lang.SecurityException: Internet Connection Denied: www.mediafire.com [02:03:35] at obf.Qc.checkPermission(gj:601) [02:03:35] at java.lang.SecurityManager.checkConnect(Unknown Source) [02:03:35] at sun.net.www.http.HttpClient.openServer(Unknown Source) [02:03:35] at sun.net.www.http.HttpClient.(Unknown Source) [02:03:35] at sun.net.www.http.HttpClient.New(Unknown Source) [02:03:35] at sun.net.www.http.HttpClient.New(Unknown Source) [02:03:35] at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source) [02:03:35] at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source) [02:03:35] at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source) [02:03:35] at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source) [02:03:35] at java.security.AccessController.doPrivileged(Native Method) [02:03:35] at java.security.AccessController.doPrivilegedWithCombiner(Unknown Source) [02:03:35] at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source) [02:03:35] at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source) [02:03:35] at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source) [02:03:35] at sun.net.www.protocol.http.HttpURLConnection.access$200(Unknown Source) [02:03:35] at sun.net.www.protocol.http.HttpURLConnection$9.run(Unknown Source) [02:03:35] at sun.net.www.protocol.http.HttpURLConnection$9.run(Unknown Source) [02:03:35] at java.security.AccessController.doPrivileged(Native Method) [02:03:35] at java.security.AccessController.doPrivilegedWithCombiner(Unknown Source) [02:03:35] at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) [02:03:35] at java.net.URL.openStream(Unknown Source) [02:03:35] at scripts.starfox.api.util.w.D(Downloader.java:40) [02:03:35] at scripts.starfox.api2007.entities.z.D(Items07.java:87) [02:03:35] at scripts.starfox.api2007.entities.z.D(Items07.java:76) [02:03:35] at scripts.starfox.api.s.h(Tau.java:53) [02:03:35] at scripts.scripting.grandexchange.TauGrandExchangeV1.run(TauGrandExchangeV1.java:71) [02:03:35] at java.lang.Thread.run(Unknown Source) [02:03:35] Script Ended: Tau Grand Exchange.  
       
       
       
      please help...
    • By pureleaf
      [17:42:27] Mar 23, 2018 5:42:27 PM java.util.prefs.WindowsPreferences WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5. getting this code and the bot doesnt know how much any item is worth now
       
    • By pureleaf
      when i execute a webwalking command when the inventory is full to the bank no matter what tiles i try the account runs to random locations not the tile i selected. im using x and y as well as the plane? any ideas thank you
       
       
    • By born4war
      well my tribot was lagging because my RAM was full so i closed it with task manager now whenever i open tri bot it says please close other instance you can only run 1 script at a time how do i fix this?
       

  • Our picks

    • This release will:

      Fix key event handling issue


      Fix other event handling issue


      Fix RSServer hook


      Update world hopper to have it use OCR, thanks to Todd


      Use proper disposal of old Graphics objects


      Reformat code


      Rearrange code


      Organize code imports


      Apply around 8000 automated code refactorings


      Make preparations for Java 9


      Fix 11 various bugs


      Add more reliable debugging support


      Fix mouseEntered/Exited event dispatching bug


      Fix minimap walking bug where it opens the map


      Fix broken hooks for today's game update
        • Thanks
        • Like
      • 55 replies
    • This update will:

      Fix GE inventory item positioning bug


      Fix broken object hooks
        • Like
      • 27 replies
    • This release will:

      Fix some ClosedChannelException bug


      Fix bug in RSObject#getAllTiles


      Add game tab support for "Kourend Favour"
        • Like
      • 15 replies
    • This release will:

      Fix Settings UI placement bug


      Fix game object location bug


      Fix small layout bug making the client shift up and down


      Fix client crashing bug where loading the client with a small display area will cause the client to crash


      Fix annoying Linux bug relating to painting events and peers


      Fix settings saving bug where settings are saved to disk more often than they should


      Fix RSInterface#isBeingDrawn bug affecting a limited amount of people


      Drop Java 1.7 bytecode version for 1.8


      Important: Since the downloadable RS client uses Java 7, it will no longer be compatible with Looking Glass. To make up for this, we will add support for using other clients such as RuneLite (at a later date).


      This change was necessary to allow us to use Java 8 syntax. It also paves the way for Java 9/10/11 support.
        • Like
      • 40 replies
    • This update will:

      Fix the RSMenuNode bug which also fixes the bug with bank opening


      Fix the incorrect object positions bug


      Fix and re-enable the LG Objects API Accelerator


      Fix the RSObject#getAllTiles bug
        • Like
      • 22 replies
  • Recently Browsing   0 members

    No registered users viewing this page.

×