Jump to content

Help with Asynchronous Event Listeners


Recommended Posts

Posted

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

Posted
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!

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

Posted
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?

Posted
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?

Posted
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?

Posted

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

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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