DylanSRT Posted May 2, 2019 Share Posted May 2, 2019 Hey guys, I've noticed that if I stop my script in the middle of a while loop, the next time I try to run the script it is still in the while loop. So it basically runs two instances of the same script at the same time which obviously screws everything up. I would expect that behavior if I paused the script during a while loop and played it, but why does it happen even after completely stopping the script? The only way I can get the script to run normally again is by restarting the client. I'm sure some of you more experienced scripters know a way around this. Thanks! Quote Link to comment Share on other sites More sharing options...
Chris Posted May 2, 2019 Share Posted May 2, 2019 break out of the loop when the client isnt running anything? Quote Link to comment Share on other sites More sharing options...
DylanSRT Posted May 2, 2019 Author Share Posted May 2, 2019 6 minutes ago, Chris said: break out of the loop when the client isnt running anything? How would I go about doing that? Do I put something in my On Exit? I don't know how to control the client, just the script. Thanks Quote Link to comment Share on other sites More sharing options...
Charlotte Posted May 2, 2019 Share Posted May 2, 2019 You can use a for loop if that makes it easier...I personally never liked using while loop Quote Link to comment Share on other sites More sharing options...
DylanSRT Posted May 2, 2019 Author Share Posted May 2, 2019 Just now, Charlotte said: You can use a for loop if that makes it easier...I personally never liked using while loop Yeah I agree, it seems good to stay away from while loops. I've also just been using intermediary booleans which basically act like a while loop, but it doesn't have the problem with the client. For example, for planting something with ultracompost I was using this, but it kept getting screwed up if I stopped it in the middle of a while loop: long ucAmount = inventory.getAmount(uc); while (inventory.getAmount(uc) == ucAmount) { if (inventory.interact("Use", uc)) { if (patch.interact("Use")) { Sleep.sleepUntil(() -> inventory.getAmount(uc) < ucAmount, 5000); } } } long seedAmount = inventory.getAmount(seed); while (inventory.getAmount(seed) == seedAmount) { if (inventory.interact("Use", seed)) { if (patch.interact("Use")) { Sleep.sleepUntil(() -> inventory.getAmount(seed) < seedAmount, 5000); } } } So instead I'm using this which works exactly the same but no issues with client: boolean compostUsed = false; if (!compostUsed) { long ucAmount = inventory.getAmount(uc); if (inventory.interact("Use", uc)) { if (patch.interact("Use")) { Sleep.sleepUntil(() -> inventory.getAmount(uc) < ucAmount, 5000); if (inventory.getAmount(uc) < ucAmount) { compostUsed = true; } } } } else { long seedAmount = inventory.getAmount(seed); if (inventory.interact("Use", seed)) { if (patch.interact("Use")) { Sleep.sleepUntil(() -> inventory.getAmount(seed) < seedAmount, 5000); if (inventory.getAmount(seed) < seedAmount) { compostUsed = false; } } } } There must be a way to completely terminate while loops when you exit though. Quote Link to comment Share on other sites More sharing options...
Charlotte Posted May 2, 2019 Share Posted May 2, 2019 You can make use of break; ? Like what chris has mentioned Quote Link to comment Share on other sites More sharing options...
Chris Posted May 2, 2019 Share Posted May 2, 2019 1 hour ago, DylanSRT said: How would I go about doing that? Do I put something in my On Exit? I don't know how to control the client, just the script. Thanks https://osbot.org/api/org/osbot/rs07/event/ScriptExecutor.html 1 Quote Link to comment Share on other sites More sharing options...
DylanSRT Posted May 2, 2019 Author Share Posted May 2, 2019 (edited) 18 minutes ago, Charlotte said: You can make use of break; ? Like what chris has mentioned But that would need to be for a specific scenario within the while loop right? I'm talking about stopping the script with the red button so it could be anywhere within the loop at that point. Like when I'm debugging the script and something goes wrong, I use the red button to stop the script and then I can't restart it properly. Stopping the script is meant to be a break for the main onLoop() right? So how is it possible for while loops within the main loop to still be running? 11 minutes ago, Chris said: https://osbot.org/api/org/osbot/rs07/event/ScriptExecutor.html My man! that is exactly what I was looking for I guess. So would you put the stop command in the onExit? If you could give me an example, I would be eternally grateful. Edited May 2, 2019 by DylanSRT Quote Link to comment Share on other sites More sharing options...
dreameo Posted May 2, 2019 Share Posted May 2, 2019 Are you running more than one thread? What you're saying shouldn't happen in a single thread. Quote Link to comment Share on other sites More sharing options...
Token Posted May 2, 2019 Share Posted May 2, 2019 @DylanSRT you could wrap your code in Event instances to replicate the looping behavior, they should be interrupted when a script stops/pauses. execute(new Event() { long ucAmount; long seedAmount; @Override public void onStart() { ucAmount = getInventory().getAmount(uc); seedAmount = getInventory().getAmount(seed); } @Override public int execute() { if (getInventory().isItemSelected()) { String selectedItem = getInventory().getSelectedItemName(); long selectedItemAmount = getInventory().getAmount(selectedItem); if (patch.interact("Use")) { Sleep.sleepUntil(() -> getInventory().getAmount(selectedItem) < selectedItemAmount, 3000 + 630 * getMap().realDistance(patch)); } } else if (ucAmount == getInventory().getAmount(uc)) { Item ucItem = getInventory().getItem(uc); if (Objects.isNull(ucItem)) { setFailed(); return -1; } else if (ucItem.interact("Use")) { Sleep.sleepUntil(getInventory()::isItemSelected, 2000); } } else if (seedAmount == getInventory().getAmount(seed)) { Item seedItem = getInventory().getItem(seed); if (Objects.isNull(seedItem)) { setFailed(); return -1; } else if (seedItem.interact("Use")) { Sleep.sleepUntil(getInventory()::isItemSelected, 2000); } } else { setFinished(); return -1; } return random(10, 100); } }); Check the API docs on Event, EventExecutor, ScriptExecutor for details PS: Make sure you change the code to take into account the base where the ultracompost/seed cannot be used on the patch, otherwise it will loop until script is interrupted. 1 Quote Link to comment Share on other sites More sharing options...
Czar Posted May 2, 2019 Share Posted May 2, 2019 If you really wanna use while loops and don't want to try the useful posts above or if you just need to have while loops in other areas of your code, then just make sure your while loops have a break condition checking that the script executor is paused or stopped getBot().getScriptExecutor().isPaused() or isRunning(), other than that there are some good posts by our scripters above ^^ 1 Quote Link to comment Share on other sites More sharing options...
DylanSRT Posted May 7, 2019 Author Share Posted May 7, 2019 @Token Thanks man this was very helpful Quote Link to comment Share on other sites More sharing options...
sl0wkey Posted December 10, 2023 Share Posted December 10, 2023 (edited) I found this topic recently and I've been having trouble pausing and resuming my script when I change states inside the script. I need the script to pause so I can have mouse input to select the nodes that must be gathered and resumed when entering gathering state. The script pauses, but I'm not being able to resume it so it starts gathering and running again. Spoiler @Override public int onLoop() { switch(currentState) { case SELECTION_STATE: if (getBot().getScriptExecutor().isRunning()) { try { getBot().getScriptExecutor().pause(); } catch (Exception e) {} break; } case GATHERING_STATE: if (getBot().getScriptExecutor().isPaused()) { getBot().getScriptExecutor().resume(); } try { gatherNode(); } catch (Exception e) {} break; case BANKING_STATE: break; } return random(500,1000); } Anyone knows why it does not resume? Edited December 10, 2023 by sl0wkey Quote Link to comment Share on other sites More sharing options...