September 14, 201510 yr Hi, I`m trying to learn to script in OSbot, I was wondering if anyone could take the time and tell me what I am doing wrong. I get 3 errors that I dont quite understand. Thanks in advance! import org.osbot.rs07.api.LocalWalker; import org.osbot.rs07.api.map.Position; import org.osbot.rs07.api.model.Entity; import org.osbot.rs07.api.model.Player; import org.osbot.rs07.script.Script; import org.osbot.rs07.script.ScriptManifest; import org.osbot.rs07.api.model.*; @ScriptManifest(author = "", info = "", logo = "", name = "", version = 0) public class test extends Script { @Override public void onStart() { log("Welcome"); } private enum State { idle, walktobank, walktotrees, atbank, attrees } private State getState() { Entity tree = objects.closest("Willow Tree"); Entity booth = objects.closest("Bank Booth"); if (!myPlayer().isAnimating() && (!myPlayer().isMoving() && (tree == null))); return State.attrees; if (!myPlayer().isAnimating() && (!myPlayer().isMoving() && (booth == null))); return State.atbank; if (!myPlayer().isAnimating() && (!myPlayer().isMoving()) && (tree == null) && (inventory.isEmpty())); return State.walktotrees; if (!myPlayer().isAnimating() && !myPlayer().isMoving() && (booth == null) && (inventory.isFull())); return State.walktobank; return State.idle; } public int onLoop() throws InterruptedException { switch (getState()) { case idle: sleep(random(500, 700)); break; case attrees: Entity tree = objects.closest("Willow Tree"); tree.interact("Chop"); break; case walktobank: Entity booth = objects.closest("Bank Booth"); LocalWalker.walk(booth); break; case walktotrees: Entity tree_2 = objects.closest("Willow Tree"); LocalWalker.walk(tree_2); break; } return random(200,300); } public void onExit() { log("Bye"); } }
September 14, 201510 yr which lines have errors? or is the script throwing errors? Edited September 14, 201510 yr by Precise
September 14, 201510 yr Author ah wow... I feel foolish, thanks. only one error left: private State getState() { Entity tree = objects.closest("Willow Tree"); Entity booth = objects.closest("Bank Booth"); if (!myPlayer().isAnimating() && (!myPlayer().isMoving() && (tree == null))); return State.attrees; if (!myPlayer().isAnimating() && (!myPlayer().isMoving() && (booth == null))); // error: unreachable code return State.atbank; if (!myPlayer().isAnimating() && (!myPlayer().isMoving()) && (tree == null) && (inventory.isEmpty())); return State.walktotrees; if (!myPlayer().isAnimating() && !myPlayer().isMoving() && (booth == null) && (inventory.isFull())); return State.walktobank; return State.idle; }
September 14, 201510 yr private State getState() { Entity tree = objects.closest("Willow Tree"); Entity booth = objects.closest("Bank Booth"); if (!myPlayer().isAnimating() && !myPlayer().isMoving() && tree == null) return State.attrees; if (!myPlayer().isAnimating() && !myPlayer().isMoving() && booth == null) return State.atbank; if (!myPlayer().isAnimating() && !myPlayer().isMoving() && tree == null && inventory.isEmpty()) return State.walktotrees; if (!myPlayer().isAnimating() && !myPlayer().isMoving() && booth == null && inventory.isFull()) return State.walktobank; return State.idle; }
September 14, 201510 yr Author ouch, I guess after messing around too much I screwed up without realising, thanks! ;)
September 14, 201510 yr hint: if (!myPlayer().isAnimating() && !myPlayer().isMoving() && tree == null) return State.attrees; If the tree is null, there are no trees nearby. And if there are no trees nearby when in the "attrees" state, you'll get an error (NullPointerException) here: case attrees: Entity tree = objects.closest("Willow Tree"); tree.interact("Chop"); <---- NullPointerException because tree is null break; Edited September 14, 201510 yr by FrostBug
September 14, 201510 yr ouch, I guess after messing around too much I screwed up without realising, thanks! To elaborate on why you were having those issues: if (x == y); return z; The idea with the if block is that it tests logic. Now, what you want to do after the if block (if it returns true) is to execute all the code in that block. When you use the semicolon, you're effectively terminating the block. It would look something like this: if (x == y) { } return z; //Note how this is outside the block //Any code after here is UNREACHABLE because we already return z So to fix, we remove the semicolons from our if statements to look like this: if (x == y) return z; Which would look more like this: if (x == y) { return z; } //Code here is only executed when (x == y) evaluates to FALSE (or in better words, when (x != y)) Hope this makes some sense
September 15, 201510 yr Thanks, this is really helpful! If you have any more questions feel free to PM me
Create an account or sign in to comment