aniki432789 Posted May 17, 2018 Share Posted May 17, 2018 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. Quote Link to comment Share on other sites More sharing options...
aniki432789 Posted May 17, 2018 Author Share Posted May 17, 2018 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 } 1 Quote Link to comment Share on other sites More sharing options...
Alek Posted May 17, 2018 Share Posted May 17, 2018 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 } 2 Quote Link to comment Share on other sites More sharing options...