Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
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 rexiee
      help me choose safest way too bot
    • By rskiller2010
      i just purchase a vip extend, and i login with a works proxy,but always get issues.
       

    • By rilumz
      so I have the bot training outside of relleka and every time it leaves the area to re-aggro the crabs it doesn't walk back up and start training again. kind of annoying seeing as i paid for the VIP and the optimus combat bot. would appreciate any help regarding this malfunction
       
    • By zoxind
      I've written my first, I would say for now basic script for combat training. 
      One of the main reasons why I chose to use Tribot is because of abc2. But while reading the post on the tutorial on how to implement it in your own code (
      ) I didn't really understand the method(the way) on how to implement it in code. 
      Could anyone please send me a personal message with an explanation on how to implement it correctly, or write a reply here.
      It would be very nice, if you would include an actual code example of it, that would help me to understand it better.
      Thank you in advance.
    • By Iwish Ucould
      Hello, I am trying to purchase credits for VIP yet it says that my paypal isnt verified? I verified the email a little while ago and I keep getting the same error message.
    • 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 Fishies
      I'm having an issue on my linux server where I connect with VNC, startup the client and script and everything works fine. Even when I close out of the VNC my bot continues to run fine, but when I log back into the VNC (to checkup or whatever) it crashes the client and logs the bot out. I have all power management settings turned off, and have no idea why this is happening or how to fix it. It dosn't disable my farm, but is extremely annoying having to restart the client every time I want to check up on the bot. Thanks!
    • By Rouges
      Hi i havent used tribot in a while, thankfully i have somehow not logged out in like forever but i cannot remembermy password. i attempted to use"forgot password on the bots opening but no luck  keeps saying unavailable, can anyone help me out? am i doing something wrong
       
    • 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.
  • Our picks

    • This release will:

      Fix LG for both OSBuddy and RuneLite


      Fix issue where the resizable client isn't able to be made smaller (Thanks @JoeDezzy1)


      Fix detection of the logout game tab when resizable mode and side panels are enabled (Thanks @JoeDezzy1)


      Add initial support for Sentry to allow us to identify and easily debug exceptions happening with all TRiBot users


      Add methods to determine if the bank is actually loaded, and not just the overarching interface (Thanks @wastedbro)



      Upcoming updates:

      Improved CLI support


      Full Sentry support


      Much more
        • Like
      • 57 replies
    • This release will:

      Fix NPE in Camera API (Thanks @wastedbro)


      Update deposit box interface ids (Thanks @Encoded)


      Add various bank methods (Thanks @wastedbro)


      Banking#getWithdrawXQuantity


      Banking#getDefaultWithdrawQuantity


      Banking#arePlaceholdersOn




      Fix resizeable minimap bug (Thanks @wastedbro)


      Remove Java 8 requirement


      Please note: TRiBot is not yet fully compatible with Java 10+




      Fix the break handler issues by ensuring the break handler thread never gets paused


      Fix broken settings hooks



      Upcoming updates:

      Improved CLI support


      Much more



      Note: If you are using LG, please restart both the RS client and TRiBot
        • Like
      • 68 replies
    • This release will:

      Add support for using custom F key bindings to switch between game tabs (Thanks @erickho123)


      Fix tab opening for "Skills" and "Kourend Tasks" (Thanks @erickho123)



      Note: If you are using LG, please restart both the RS client and TRiBot
        • Like
      • 34 replies
    • This release will:

      Fix an issue where breaks would stop firing


      Fix Combat#getWildernessLevel, use dynamic search for text and cache ID for later calls


      Fix an NPE in the Combat API


      Fix Mouse#leaveGame bug where the mouse wouldn't actually leave the game screen
        • Like
      • 21 replies
    • This release will:

      Add LG support for Runelite


      Fix NPCChat issues


      Fix a bug where the camera angle setter would just hold down a key for 5 seconds (the timeout)


      Slightly adjust the rotation via keys to be more accurate


      Add the ability for asynchronous camera movement via keys


      Make Camera rotation via mouse more fluid, with more antiban, and work much better in resizable mode


      Add a "Camera#setCamera" method, allowing the rotation and angle to be set in parallel


      Increase the likelihood of using the mouse for camera movements


      Add support for adjusting the camera to positionable entities (Positionable#adjustCameraTo)



      Upcoming updates:

      Improved CLI support


      Much more



      Note: If you are using LG, please restart both the RS client and TRiBot
        • Thanks
        • Like
      • 59 replies
  • Recently Browsing   0 members

    No registered users viewing this page.

×