Jump to content

first script help :P


Recommended Posts

Posted

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");
	}

}

Posted

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;
		

		}
Posted


    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;

        

        }

Posted (edited)

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 by FrostBug
Posted

ouch, I guess after messing around too much I screwed up without realising, thanks! wink.png

 

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 :)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...