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.

Checking to see if an object exists without crashing if it doesnt

Featured Replies

Hello, just trying to learn more about the api.

As of now I'm working on a simple cannon script but I don't know the best way to go about fixing the cannon incase it breaks. The cannon goes from "Dwarf multicannon" to "Broken multicannon" and I don't know how to interact with the Broken multicannon optimally.

I declare the working cannon as cannon = getObjects().closest("Dwarf multicannon")

But this only works assuming the working cannon does actually exist. If it doesn't exist, the script crashes.

So, for example, if I'm trying to check if the Broken multicannon exists, I can't use getObjects().closest("Broken multicannon") in anyway else bad stuff goes down.

 

So, my question is, is there a safer way of checking if something exists without everything breaking if it does not?

Thank you for your time.

  • Author

Go figure as soon as I post something I figure out an answer myself.

Just posting the resolution in hopes of helping somebody in the future.

 

if (getObjects().closest("Broken multicannon") != null){ // do stuff

} else { // object does not exist

}

One way, using null checking:

RS2Object object = getObjects.closest("Dwarf multicannon");
if(object != null) {
//do things
}


Another way, using streams:
 

       Optional<RS2Object> object = objects.getAll().stream().filter(i -> i.getName().equals("Dwarf multicannon")).findAny();
       if(object.isPresent()) {
           RS2Object cannon = object.get();
           //do stuff
       }

 

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.