Jump to content

Enum with Switch troubles


Recommended Posts

Posted

Hello guys, I read Apaec's guide: "A beginners guide to writing OSbot scripts" and got excited to use enum with switch.
So I'm exploring this method but get weird results, I will try to explain as clearly as I can.

What (I think) my script is supposed to be doing right now:

  1. Check if within the area
  2. If not walk to sellpoint, send message "walking back"
  3. Check if more than 1 law rune in inventory
  4. If true send message "test"

What is happening when the script is run:

  1. When my player is in the area it spams "walkinig back" but is not walking back
  2. When my player is NOT in the area it walks back and is not spamming "walking back"

  

 

 

    private final
    Area lumbyBank = new Area(3207, 3217, 3210, 3220);
    Position sellPoint = new Position (3208, 3217, 2);
   

    private enum State {
        WALK, TRADE, EXIT
    }

private State getState() {
        if (!lumbyBank.contains(myPosition()))
                return State.WALK;      
        if (getInventory().getItem("Law rune").getAmount() >= 1)
                return State.TRADE;
        return State.EXIT; 
    }
    
    @Override
    public int onLoop() throws InterruptedException {
        switch (getState()) {
        case WALK:
                getWalking().walk(sellPoint);
                log("walk back");
            break;
        case TRADE:
            log("test");
            break;
        case EXIT:
            stop();
            break;
        }    
        
        return random(200, 300);
    }
   

Posted (edited)
5 minutes ago, evert123 said:

Hello guys, I read Apaec's guide: "A beginners guide to writing OSbot scripts" and got excited to use enum with switch.
So I'm exploring this method but get weird results, I will try to explain as clearly as I can.

What (I think) my script is supposed to be doing right now:

  1. Check if within the area
  2. If not walk to sellpoint, send message "walking back"
  3. Check if more than 1 law rune in inventory
  4. If true send message "test"

What is happening when the script is run:

  1. When my player is in the area it spams "walkinig back" but is not walking back
  2. When my player is NOT in the area it walks back and is not spamming "walking back"

  

 

 

    private final
    Area lumbyBank = new Area(3207, 3217, 3210, 3220);
    Position sellPoint = new Position (3208, 3217, 2);
   

    private enum State {
        WALK, TRADE, EXIT
    }

private State getState() {
        if (!lumbyBank.contains(myPosition()))
                return State.WALK;      
        if (getInventory().getItem("Law rune").getAmount() >= 1)
                return State.TRADE;
        return State.EXIT; 
    }
    
    @Override
    public int onLoop() throws InterruptedException {
        switch (getState()) {
        case WALK:
                getWalking().walk(sellPoint);
                log("walk back");
            break;
        case TRADE:
            log("test");
            break;
        case EXIT:
            stop();
            break;
        }    
        
        return random(200, 300);
    }
   

 

Firstly your Lumbridge bank area is incorrect, I would recommend you use the constant Banks.LUMBRIDGE_UPPER instead.

Secondly your logic in general seems weird. I would recommend not using the state pattern because it just confuses things.

 

Edited by Explv
Posted
5 minutes ago, Explv said:

 

Firstly your Lumbridge bank area is incorrect, I would recommend you use the constant Banks.LUMBRIDGE_UPPER instead.

Secondly your logic in general seems weird. I would recommend not using the state pattern because it just confuses things.

 

Thank you very much, nice response time to :)

I will try a different method and do some figuring out myself

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...