Jump to content

Sleep method


inababila

Recommended Posts


 
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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

@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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...