Jump to content

Script termination in the middle of a while loop


DylanSRT

Recommended Posts

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!

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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:

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 by DylanSRT
Link to comment
Share on other sites

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

  • Heart 1
Link to comment
Share on other sites

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 ^^ :D

  • Like 1
Link to comment
Share on other sites

  • 4 years later...

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 by sl0wkey
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...