Interacting with Objects
In Old School RuneScape (OSRS), objects play a crucial role in the game world. From doors and gates to trees and rocks, interacting with them effectively is essential for successful gameplay automation. This page explains how to query and interact with objects using the SimpleBot API.
Table of Contents
Querying Objects
To interact with objects, you first need to find them within the game world. The SimpleBot API provides functions to search for objects by various attributes such as name, ID, or actions they can perform. You can learn more about querying in the Querying guide.
Finding an Object by Name
To find an object by name, use the objects.populate().filter()
method. This returns the nearest object that matches the specified name. Here's an example:
final SimpleObject door = ctx.objects.populate().filter("Door").nextNearest();
if (door != null) {
System.out.println("Found object: " + door.getName());
} else {
System.out.println("Object Door not found.");
}
Finding an Object by ID
To find an object by ID, use the objects.populate().filter()
method with the object ID. This returns the nearest object with the specified ID. Here's an example:
final SimpleObject tree = ctx.objects.populate().filter(1276).nextNearest();
if (tree != null) {
System.out.println("Found object: " + tree.getName());
} else {
System.out.println("Object ID 1276 not found.");
}
Interacting with Objects
Once you have found an object, you can interact with it in various ways. The SimpleBot API provides two methods to interact with objects: click()
and menuAction()
.
For more information on these two methods, refer to the Interacting with Items guide.
Interacting with an Object using click()
To interact with an object using the click()
method, you can specify the action name or action index.
Note: interacting with objects using the click()
method requires the object to be within the viewport of the game client. If the object is not visible, you may need to move the camera or character to interact with it.
Validating Interactability
The api offers the validateInteractable()
method to check if the object is within the viewport and can be interacted with. If the object is not interactable, it will move the camera and or character in an attempt to position the object within the viewport.
Here's an example of how to interact with an object using the click()
method:
final SimpleObject door = ctx.objects.populate().filter("Door").nextNearest();
if (door != null) {
if (door.validateInteractable()) {
door.click("Open");
} else {
System.out.println("Object Door is not interactable.");
}
} else {
System.out.println("Object Door not found.");
}
Interacting with an Object using menuAction()
To interact with an object using the menuAction()
method, you can specify the action name or action index.
Using the menuAction()
method allows you to interact with objects that are not within the viewport of the game client. This method sends a menu action to the object directly without requiring the object to be visible.
Here's an example of how to interact with an object using the menuAction()
method:
final SimpleObject door = ctx.objects.populate().filter("Door").nextNearest();
if (door != null) {
door.menuAction("Open");
} else {
System.out.println("Object Door not found.");
}