Implementing Sleep Conditions and Delays
Using sleep conditions and delays in your scripts is essential to ensure that your bot behaves more like a human player. Sleeps and delays help to simulate the time taken to perform an action, such as waiting for an animation to complete or waiting for an item to be used. Sleep conditions and delays are also useful for avoiding spamming the game server with too many actions in a short period.
This guide explains how to use sleeps and delays in your scripts to improve the efficiency and realism of your bot.
Table of Contents
Using Sleep Conditions
The SimpleBot API provides the onCondition()
method to sleep the current thread until a specified condition is met. This method checks the condition every 500 milliseconds and sleeps for a maximum of 5 seconds. If the condition is met before the timeout, the method returns true
; otherwise, it returns false
.
The onCondition()
method has three overloaded versions:
onCondition(BooleanSupplier condition)
- This method sleeps for a maximum of 5 seconds, checking the condition every 500 milliseconds.onCondition(BooleanSupplier condition, int timeout)
- This method sleeps for a maximum oftimeout
milliseconds, checking the condition every 500 milliseconds.onCondition(BooleanSupplier condition, int timeout, int amount)
- This method sleeps for the specifiedtimeout * amount
milliseconds or until the condition is met.
The BooleanSupplier
interface is a functional interface that takes no arguments and returns a boolean value. You can use lambda expressions to define the condition to be checked.
Here's an example of how to use the onCondition()
method to wait for our player to start animating after attempting to attack an NPC. This will prevent the bot from trying to attack another NPC before we have finished attacking the current one:
final SimpleNpc goblin = ctx.npcs.populate().filter("Goblin").nextNearest();
if (goblin != null) {
goblin.click("Attack");
ctx.onCondition(() -> ctx.players.getLocal().isAnimating());// Wait for the player to start animating
System.out.println("Our sleep condition has ended.");
} else {
System.out.println("NPC Goblin not found.");
}
Using Sleeps
The SimpleBot API provides the sleep()
method to pause the current thread for a specified amount of time. This method is useful for adding delays between actions to simulate human-like behavior or to wait for a specific event to occur.
The sleep()
method has two overloaded versions:
sleep(int ms)
- This method sleeps forms
milliseconds.sleep(int min, int max)
- This method sleeps for a random amount of time betweenmin
andmax
milliseconds.
Here's an example of how to use the sleep()
method to add a delay of 2 seconds after attempting to attack an NPC:
final SimpleNpc goblin = ctx.npcs.populate().filter("Goblin").nextNearest();
if (goblin != null) {
goblin.click("Attack");
ctx.sleep(2000); // Wait for 2 seconds before attacking the next NPC
System.out.println("Our sleep has ended after 2 seconds.");
} else {
System.out.println("NPC Goblin not found.");
}
Best Practices
When using sleep conditions and delays in your scripts, consider the following best practices:
Use sleep conditions to wait for specific conditions to be met before proceeding with the next action. This helps to synchronize the bot's actions with the game state.
Use sleeps to add delays between actions to simulate human-like behavior and avoid spamming the game server with too many actions in a short period.
Use random sleeps to add variability to your bot's behavior and make it less predictable to other players or anti-botting systems.
Avoid using long sleep times, as this can make your bot appear slow and unresponsive. Instead, use sleep conditions to wait for specific events to occur before proceeding with the next action.
Use sleep conditions and delays judiciously to balance efficiency and realism in your bot's behavior.