Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Am I going crazy?


    swiftdv

    Recommended Posts

    Posted

    I am currently writing my script using Abstract Script. I am using enums and a getState method. (See code below)

     

    For some reason when it loops through, everything in the loop is being executed. This makes no sense to me since this the switch statement is using whatever getState() returns.

     

    Essentially what I am trying to say is, why is the LOOT case being executed when the state is never set to "LOOT"?

        private State getState(){
            Area wild = new Area(3106, 3548, 3056, 3523);
    
            if(getInventory().isEmpty() && !wild.contains(getLocalPlayer().getTile())){
                return State.WALK_TO_WILD;
            }
            else 
                return null;
        }
    
        @Override
        public int onLoop() {
                Combat cb = new Combat(getClient());
                boolean auto = cb.isAutoRetaliateOn();
                if(auto){
                    cb.toggleAutoRetaliate(false);
                }
            state = getState();
            switch(state) {
                case WALK_TO_WILD:
                    walkToWild();
                case LOOT:
                    log("THIS SHOULD NOT BE EXECUTED");
    
            }
            return Calculations.random(200,300);
        }
    
    
    Posted

    You need to have a break statement at the end of every case in the switch block, otherwise all the cases below the first matching case will be executed!

     

    Like this:

    switch(state) {
        case WALK_TO_WILD:
            walkToWild();
            break;
        case LOOT:
            log("THIS SHOULD NOT BE EXECUTED");
            break;
    }
    
    Posted

     

    I am currently writing my script using Abstract Script. I am using enums and a getState method. (See code below)

     

    For some reason when it loops through, everything in the loop is being executed. This makes no sense to me since this the switch statement is using whatever getState() returns.

     

    Essentially what I am trying to say is, why is the LOOT case being executed when the state is never set to "LOOT"?

        private State getState(){
            Area wild = new Area(3106, 3548, 3056, 3523);
    
            if(getInventory().isEmpty() && !wild.contains(getLocalPlayer().getTile())){
                return State.WALK_TO_WILD;
            }
            else 
                return null;
        }
    
        @Override
        public int onLoop() {
                Combat cb = new Combat(getClient());
                boolean auto = cb.isAutoRetaliateOn();
                if(auto){
                    cb.toggleAutoRetaliate(false);
                }
            state = getState();
            switch(state) {
                case WALK_TO_WILD:
                    walkToWild();
                case LOOT:
                    log("THIS SHOULD NOT BE EXECUTED");
    
            }
            return Calculations.random(200,300);
        }
    
    

     

    You should have breaks at the end of each case as @its me said, otherwise you could make your functions (walkToWild() etc..) return an integer value and then instead of just calling the methods in the switch case you can return them which wouldn't require a break to be added. 

    Posted

     

    You need to have a break statement at the end of every case in the switch block, otherwise all the cases below the first matching case will be executed!

     

    Like this:

    switch(state) {
        case WALK_TO_WILD:
            walkToWild();
            break;
        case LOOT:
            log("THIS SHOULD NOT BE EXECUTED");
            break;
    }
    

     

     

    You should have breaks at the end of each case as @its me said, otherwise you could make your functions (walkToWild() etc..) return an integer value and then instead of just calling the methods in the switch case you can return them which wouldn't require a break to be added. 

    Thanks guys!

    Posted

    To go ahead and clarify further, if you have something like:

     

    int x = 1;

    switch(x) {

      case 1:

        log("Here");

      case 2:

        log("Here");

    }

     

    it will print here twice if x is equal to 1.

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.