Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Sleep method

Featured Replies


 
Item item = script.getInventory().getItemInSlot(1);
                if (item != null && script.getInventory().interact(1, "Drop")) {
                    new ConditionalSleep(5000, 500) {
                        public boolean condition() {
                            return item != null;
                        }
                    };
                }

Over this

Item item = script.getInventory().getItemInSlot(1);
if (item != null && script.getInventory().interact(1, "Drop")) {
      script.sleep(random(500, 3000));
}

Can someone please explain the difference between both methods, the idea behind them, how it would work, and why should/shouldn't I use that way?

Edited by inababila

The first sleep is an anonymous instance of a Conditional Sleep. The second is a static sleep.

Aside from the fact that the first one won't actually do anything since you never call the sleep() method, the conditional sleep would sleep for ~5000ms OR until the condition evaluates to true.

A static sleep is of fixed duration. for example, sleep(3000) will sleep for ~3000ms.

___

Uses:

Conditional sleep- all the time

Static sleep- never

__

If you don't like the size of declaring an anonymous instance every time you want to use a conditional sleep, you could define it globally, extend the class to create your own sleep, or even extend the class to make use of the BooleanSupplier functional interface, then define one-line conditional sleeps with lambda expressions.

GL!

Apa

Script.sleep is just a sleep. The bot thread just sleeps for the specified amount.

Now Conditional sleep, as the name implies means the bot sleeps UNTIL A CERTAIN condition.

Your example is a little shady, cause it sleeps until the item in inventory slot 1 is not null. Should move the checking logic inside the condition() method and check if the item IS NULL.

Basically, a conditional sleep sleeps until the public boolean condition() method returns true. This allows for more responsive behavior and in certain cases let's your script to be more smooth.

 

Edit:

A little example using your code (changed to work if I understodd correctly what you want to do)

Item item = script.getInventory().getItemInSlot(1);
                if (item != null && script.getInventory().interact(1, "Drop")) {
                    new ConditionalSleep(5000, 500) {
                        public boolean condition() {
                            return script.getInventory().getItemInSlot(1) == null;
                        }
                    };
                }

What this does is sleep for a maximum of 5 seconds, checking the condition  everyt .5 seconds. If the item in inventory slot 1 is null then the script stops to sleep and caries on.

If the item is still not null after 5 seconds - stops sleeping and caries on.

Edited by nosepicker

  • Author

@Apaec @nosepicker Thanks for that, was really useful. So I will be changing my condition to the opposite. Also, 5000,500 means the timeout is 5 seconds, check for the condition every half a second, right? And, what would happen if the 5 seconds passed and the item is still not null = exists? Like are there any failsafe or other ways I could implement to check or increase the timmer?

Edited by inababila

8 minutes ago, inababila said:

@Apaec @nosepicker Thanks for that, was really useful. So I will be changing my condition to the opposite. Also, 5000,500 means the timeout is 5 seconds, check for the condition every half a second, right? And, what would happen if the 5 seconds passed and the item is still not null = exists? Like are there any failsafe or other ways I could implement to check or increase the timmer?

If 5 seconds have passed the sleep will end and your on your own.

Best thing to do is to redo the whole loop if the item dropping failed. There's a simple rule (though not always easy to keep it this way): Only do one thing per one loop iteration.

If your drop failed, then it will hang for 5 seconds, but after that a new loop iteration will begin and will try to drop the item once more.

Hope you got the gist.

Regards

  • Author

@nosepicker Yup I understand, I will add item existence checker and do a recursive function. Thanks :)

Edited by inababila

1 hour ago, inababila said:

@nosepicker Yup I understand, I will add item existence checker and do a recursive function. Thanks :)

You shouldn't need recursion - onLoop itself is an infinite loop so should take advantage of that instead of potentially causing infinite recursion if you're not careful!

Also, ConditionalSleep.sleep method is boolean and will return condition() when it is finished sleeping - use this to determine whether or not a sleep was successful(:

Apa

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.