Lucas_Larson Posted January 10, 2021 Share Posted January 10, 2021 I am learning how to script and have started with a mining script. I have some previous programming knowledge but I'm learning the api. The bot walks to the mining spot then banks when he is done and walks back. When he is getting attacked by a mugger he just keeps trying to mine. It shows in the logs when I am under attack. Not sure how to make him run. (I would appreciate any tips on how to make my code more efficient as I know it's pretty messy) LINK TO CODE: https://pastebin.com/1imvcf7D 1 Quote Link to comment Share on other sites More sharing options...
Simple79001 Posted January 10, 2021 Share Posted January 10, 2021 (edited) Your walking event doesn't seem to have the code to walk to your hiding spot? I'm not an experienced scripter so i may be wrong. But in my experience you may want something like this getWalking().webWalk Edited January 10, 2021 by Simple79001 Quote Link to comment Share on other sites More sharing options...
Jarl Posted January 10, 2021 Share Posted January 10, 2021 You should add the "if under attack" condition to getState() for the state to switch properly. You need to use webwalk to walk to your mining spot. You should use conditional sleeps to detect if your bot has done something, instead of sleeping every time because sometimes the bot doesn't manage to do some things and you have to keep waiting. I recommend you take a look at this guide and try to make use of conditional sleeps and if statements properly. https://osbot.org/forum/topic/115124-explvs-scripting-101/ Quote Link to comment Share on other sites More sharing options...
Nbacon Posted January 10, 2021 Share Posted January 10, 2021 (edited) My 2 cents: you can get better perfomance with a looping bot...and they are easier to write imo Quote Area miningspot = new Area(3181, 3380, 3185, 3374); if (inventory.isFull()) { if (Banks.VARROCK_WEST.contains(myPlayer())) { if (bank.isOpen()) { bank.depositAll(); } else { bank.open(); } } else { walking.webWalk(Banks.VARROCK_WEST); } } else { if (miningspot.contains(myPlayer())) { if (myPlayer().isUnderAttack()) { walking.webWalk(new Position(3169, 3399, 0)); // or walking.webWalk(new Area(3169, 3399, 3169, 3399)); Thread.sleep(10000);//so it stays there } else { if (!myPlayer().isAnimating()) { RS2Object vein = objects.closest(11361); if (vein != null) { vein.interact("Mine"); // sleep(random(2000, 3000)); } } } }else{ getWalking().walk(miningspot); } } comments on your code. Quote public class BasicMiner extends Script { private enum State { MINE, BANK, RUN } public State getState() { //add the under attack here // if ((myPlayer().isUnderAttack())){ // return State.RUN; // } if (inventory.isFull()) return State.BANK; return State.MINE; } // Area miningspot = new Area(3181, 3380, 3185, 3374);(1) @Override public void onStart() { //walks to mining area Area miningspot = new Area(3181, 3380, 3185, 3374);//make this a class var(1) WalkingEvent walkingEvent = new WalkingEvent(miningspot);//(0) walkingEvent.setMinDistanceThreshold(0);//(0) execute(walkingEvent);//(0) //just use walking.webWalk(miningspot); it will get you close (0) log("I can't believe script writing is this easy! I love learning!"); } public State running() { return State.RUN; } @Override public int onLoop() throws InterruptedException { switch (getState()) { case MINE: if (!myPlayer().isAnimating()) { RS2Object vein = objects.closest(11361); if (vein != null) { vein.interact("Mine"); sleep(random(2000, 3000)); } if ((myPlayer().isUnderAttack())){//geting attacked is an Animaion running();//return statement does nothing and no state changes... log("under attack"); } } break; case BANK: //FIX INVENTORY IS FULL if (inventory.isFull()) {//messy walking.webWalk(Banks.VARROCK_WEST); bank.open(); sleep(random(2500, 4000));//try not rely on sleeps and try using the state of the //world bank.depositAll(); sleep(random(2000, 2500)); bank.close(); sleep(random(3000, 5000)); getWalking().walk(new Area(3181, 3380, 3185, 3374));//walking.webWalk(miningspot) log("walking to mining spot"); } break; case RUN: if ((myPlayer().isUnderAttack() == true)){//can just be if (myPlayer().isUnderAttack()) Position hide = new Position(3169, 3399, 0);//make this a class var WalkingEvent walkingEvent = new WalkingEvent(hide); walkingEvent.setMinDistanceThreshold(0); execute(walkingEvent); log("running away"); } break; } return 123;//make this a random(0, 5000) } } p.s. uses getter... Edited January 10, 2021 by Nbacon 1 Quote Link to comment Share on other sites More sharing options...
Lucas_Larson Posted January 10, 2021 Author Share Posted January 10, 2021 3 hours ago, Nbacon said: When I tried out the first script for the looping bot he got stuck in the bank menu and wouldn't walk away. Do I have to use bank.close(); or should it just run away from the bank? Or did I just do something wrong Quote Link to comment Share on other sites More sharing options...
Nbacon Posted January 10, 2021 Share Posted January 10, 2021 Oh I think i see the problem tell me if this works? }else{ getWalking().walk(miningspot); } to }else{ if(getBank().isOpen()){ getBank().close() }else{ getWalking().walk(miningspot); } } Quote Link to comment Share on other sites More sharing options...
Jarl Posted January 11, 2021 Share Posted January 11, 2021 (edited) On 1/10/2021 at 4:47 AM, Lucas_Larson said: Based on the logic, once you deposit, your inventory isn't full anymore so you go into mining state and the bot will move to the location without closing the bank. (I think it is good practice to close the bank though, it would be a pattern if you always close the bank by clicking on minimap to walk. (But this is assuming your code works) The problem was with you not using webwalk for the mining spot. On 1/10/2021 at 5:03 AM, Nbacon said: }else{ if(getBank().isOpen()){ getBank().close() }else{ getWalking().walk(miningspot); } } This would not work. He needs getWalking().webWalk(miningspot) to generate a path to walk on. "walk()" only works if the location is close enough. Another solution is to manually make your path with a list of positions and use walkPath. Again, the guide I linked you to is really useful and could make your script better. Edited January 11, 2021 by Jarl 1 Quote Link to comment Share on other sites More sharing options...
Nbacon Posted January 11, 2021 Share Posted January 11, 2021 1 hour ago, Jarl said: . Oh, I just aways assume that I'm using webWalk. Did not see that it auto-carroted to something wrong. Thanks. But other than that I think it logicly works (in my head at least who knows if you run it).... Quote Link to comment Share on other sites More sharing options...