Jump to content

first script help :P


Colivar

Recommended Posts

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

}

Link to comment
Share on other sites

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;
		

		}
Link to comment
Share on other sites


    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;

        

        }

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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