Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Help with Asynchronous Event Listeners

Featured Replies

Hi guys,

I am looking for a bit of help with getting the syntax of my Async events correct. I have implemented them as follows.

In main exist this Event:

public Event Listen(){
        while (true)	{
            try {
                log("Listening for escape terms");
                if(rimmyPortal.contains(myPlayer()) && !LUMBRIDGE_BANK.contains(myPlayer()) && !getMap().isInHouse()){
                    log("Escaping as outside portal.");
                    doEscape();
                }
                Thread.sleep(250);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

And this is initated as so within onStart.

    @Override
    public void onStart() {
        log("Starting XXX XXXXX");
        execute(Listen()).setAsync();
    }

 

This works correctly as I imagined. However, this Listen event just runs constantly and doesn't let any of the onLoop run. Is there any reason for so? I assume that my implementation is incorrect somewhere (I mostly dev Python not Java).

 

Thank you for the help as always!!!

I believe you have to set the event to async first before you execute the event. Also, you may want to change the while(true) to something like while 

while (getBot().getScriptExecutor().isRunning())

 

Edited by IDontEB

  • Author
15 minutes ago, Czar said:

Well, with Async you will run events simultaneously (including onloop alongside) and it doesn't wait to finish events before moving onto the next event (like non-async)

That is what I am aiming to do. However with the Async listener it will block onLoop from running and just sit listening for all the terms in the listen event which work correctly. I will try the other suggestion and see if that works!

  • Author
9 hours ago, IDontEB said:

I believe you have to set the event to async first before you execute the event. Also, you may want to change the while(true) to something like while 


while (getBot().getScriptExecutor().isRunning())

 

When I change the code to this I require a return statement. What is this? I know its of type event but I dont see why I should have to return a Value as I just want the thread to run and listen for the terms and then activate the Escape function.

16 minutes ago, Impensus said:

When I change the code to this I require a return statement. What is this? I know its of type event but I dont see why I should have to return a Value as I just want the thread to run and listen for the terms and then activate the Escape function.

the isRunning() is so the eventually if you ever stop the script and start another one it's not running in the background. Also did setting the Event to Async before executing fix your problem?

  • Author
35 minutes ago, IDontEB said:

the isRunning() is so the eventually if you ever stop the script and start another one it's not running in the background. Also did setting the Event to Async before executing fix your problem?

No It didn't unfortunately. Token was helping me in Discord and it seemed I had a fair few errors with this. Can you potentially add my discord (Impensus#2428) to give me some assistance?

  • Author
1 hour ago, Czar said:

Well, with Async you will run events simultaneously (including onloop alongside) and it doesn't wait to finish events before moving onto the next event (like non-async)

 

54 minutes ago, IDontEB said:

the isRunning() is so the eventually if you ever stop the script and start another one it's not running in the background. Also did setting the Event to Async before executing fix your problem?

I changed my code to look like this:

https://gyazo.com/1a3d0091e8cb007ff954291595ec0ea7

 

The code now runs asynchronously but crashes due to a NPE which I am unsure of why.

https://gyazo.com/2b411b90ec904c9b08f3bc127815de86

 

Any idea?

When it comes to performing actions, you should probably leave that to the onLoop. Just have it set a flag and then have your onLoop react to it.

I feel like what you have is going to be entirely useless, and cause more issues than you would like.

If you are committed to making this async, I'd honestly just call the event as async in every onLoop, that way you don't have to worry about stopping the thread if the script stops.

If you aren't committed to async, I'd just have it call normally as its probably not going to slow your script down much at all, and then you don't have to worry about the bot trying to complete multiple actions at once. 

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.