Welcome to TRiBot Forums

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

Sphiinx

[TUTORIAL] Dynamic Sleeping/Conditional Sleeping

21 posts in this topic

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
8 people like this

Share this post


Link to post
Share on other sites

To reduce CPU usage, it is recommended to have a sleep of around 100-200 milliseconds in each condition check loop.

It runs at 10ms I believe, and is unnecessarily fast by default.

2 people like this

Share this post


Link to post
Share on other sites

To reduce CPU usage, it is recommended to have a sleep of around 100-200 milliseconds in each condition check loop.

It runs at 10ms I believe, and is unnecessarily fast by default.

 

Fixed, thanks @Final Calibur @Worthy!

2 people like this

Share this post


Link to post
Share on other sites

Just another thing I don't see much people use, but Timing#waitCondition returns a boolean , so you can turn that method into a boolean method

 

private boolean openBank() {

if(Banking.openBank())

return Timing.waitCondition(c, 1000);

}

1 person likes this

Share this post


Link to post
Share on other sites

To randomize character's actions a little more, shouldn't you be using a random sleep?

 

General.sleep(100);  ->  General.sleep(100,200);
Edited by Beg

Share this post


Link to post
Share on other sites

 

To randomize character's actions a little more, shouldn't you be using a random sleep?

 

General.sleep(100);  ->  General.sleep(100,200);

ehh, that part isn't the actual sleep, its just a delay to reduce the cpu usage.

1 person likes this

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.

1 person likes this

Share this post


Link to post
Share on other sites

Cool tutorial. I was amazed the first time I switched all my scripts to using proper waits & conditions rather than arbitrary waits. It makes it run so much smoother. Good job on the tutorial :)!

1 person likes this

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

for every time i use this in my script it creates a new class once the script has compiled, is this supposed to happen?

Edited by seany123456

Share this post


Link to post
Share on other sites
On 5/22/2016 at 3:05 AM, seany123456 said:

for every time i use this in my script it creates a new class once the script has compiled, is this supposed to happen?

Nope

Bump!

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

Those are made when compiling, it's somewhat normal.

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

  • Recently Browsing   0 members

    No registered users viewing this page.