swiftdv 1 Posted April 15, 2017 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); }
its me 0 Posted April 15, 2017 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; }
Banker 175 Posted April 15, 2017 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.
swiftdv 1 Author Posted April 15, 2017 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!
Xephy 237 Posted April 15, 2017 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.
Recommended Posts
Archived
This topic is now archived and is closed to further replies.