Jump to content

How to stop an action when a condition is met


dagod

Recommended Posts

1 hour ago, dagod said:

So i am new to coding in osbot and i have been writing scripts for the last 2 month or so. Now i am trying to stop a method on a condition.

So for example: 

bot will do woodcutter(), and if someone comes close to the bot i want to stop the woodcutter() and hop world. How could i do this? Thanks in advance.

You can use the getPlayers() to get players near you. Add some filter to your likings to get the right players ^^
You character is also a player btw, so you will always load yourself in there :)

  • Heart 1
Link to comment
Share on other sites

4 hours ago, Khaleesi said:

You can use the getPlayers() to get players near you. Add some filter to your likings to get the right players ^^
You character is also a player btw, so you will always load yourself in there :)

I guess i didnt explain the issue well. Yes i do know what is the condition i need to use, but the thing i couldnt do is how do i stop the action that the bot is currently doing.

2 hours ago, Gunman said:

You can do this many ways, just depends on how you want to do it. Probably be easiest for you to make a condition to check in your conditional sleep and then if it's true do whatever action

What if the other player comes close when the bot is not sleeping?

Like how do i make a conditional action which is like the conditional sleep but does the action until the condition is met?

Edited by dagod
Link to comment
Share on other sites

6 hours ago, dagod said:

I guess i didnt explain the issue well. Yes i do know what is the condition i need to use, but the thing i couldnt do is how do i stop the action that the bot is currently doing.

What if the other player comes close when the bot is not sleeping?

Like how do i make a conditional action which is like the conditional sleep but does the action until the condition is met?

You don't have too? if you are having issues with that your script logic is probably not correct.
You have an onLoop that loops all the time, if you have a check in there you have everything you need.

If you are using events, you can just check for it in the event and call setFinished();

Feel free to share some code ^^ Easier to tell what you are trying to do

Edited by Khaleesi
Link to comment
Share on other sites

This is pretty much what I do if I need to avoid people. It first checks to make sure no one but me is around, if so it does my task, otherwise it hops worlds. This HopRandomWorld() function will check if you are in F2P or P2P and hop to a random world accordingly, you can edit the world lists as you please.
 

@Override
    public int onLoop() throws InterruptedException {
        if (getPlayers().getAll().size() == 1) {
            if (!myPlayer().isAnimating() && !myPlayer().isMoving()) {
              
              //MAIN PART OF YOUR CODE GOES HERE
                
            }
        } else {
            HopRandomWorld();
        }
        return 602;
    }



void HopRandomWorld() {
        int P2PWorlds[] = {303, 304, 305, 306, 307, 309, 310, 311, 312, 313, 314, 315, 317, 318, 319, 320, 321, 322, 323, 324,
                325, 327, 328, 329, 330, 331, 332, 333, 334, 336, 337, 338, 339, 340, 341, 342, 343, 344, 346, 347, 348, 350,
                351, 352, 354, 355, 356, 357, 358, 359, 360, 362, 367, 368, 370, 374, 375, 376, 377, 378, 386, 387, 388, 389,
                390, 395, 421, 422, 424, 443, 444, 445, 446, 463, 464, 465, 466, 477, 478, 480, 481, 482, 484, 485, 486, 487,
                488, 489, 490, 491, 492, 493, 494, 495, 496, 505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517,
                518, 519, 520, 521, 522, 523, 524, 525, 531, 532, 534, 535};
        int F2PWorlds[] = {308, 316, 335, 371, 382, 383, 384, 394, 397, 398, 399,
                417, 418, 430, 431, 433, 434, 435, 436, 437, 451, 452, 453, 454, 455,
                456, 470, 471, 475, 476, 483, 497, 498, 499, 500, 501, 537, 542, 543,
                544, 545, 546, 547, 552, 553, 554, 556, 557, 562, 563, 571, 575};
        if (getWorlds().isMembersWorld()) {
            int TargetIndex = random(0, P2PWorlds.length - 1);
            if (P2PWorlds[TargetIndex] != getWorlds().getCurrentWorld()) {
                getWorlds().hop(P2PWorlds[TargetIndex]);
            } else {
                HopRandomWorld();
            }
        } else {
            int TargetIndex = random(0, F2PWorlds.length - 1);
            if (F2PWorlds[TargetIndex] != getWorlds().getCurrentWorld()) {
                getWorlds().hop(F2PWorlds[TargetIndex]);
            } else {
                HopRandomWorld();
            }
        }
    }


You can do a conditional sleep after you do whatever it is you're doing that requires you to sleep. This will stop sleeping when the condition is met or if another player is in ~minimap range.

new ConditionalSleep(10000) {
            @Override
            public boolean condition() {return /**Your main condition**/ || getPlayers().getAll().size() > 1;}
        }.sleep();

 

 

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