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