Impensus Posted May 24, 2019 Share Posted May 24, 2019 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!!! Quote Link to comment Share on other sites More sharing options...
IDontEB Posted May 25, 2019 Share Posted May 25, 2019 (edited) 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 May 25, 2019 by IDontEB Quote Link to comment Share on other sites More sharing options...
Czar Posted May 25, 2019 Share Posted May 25, 2019 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) Quote Link to comment Share on other sites More sharing options...
Impensus Posted May 25, 2019 Author Share Posted May 25, 2019 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! Quote Link to comment Share on other sites More sharing options...
Impensus Posted May 25, 2019 Author Share Posted May 25, 2019 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. Quote Link to comment Share on other sites More sharing options...
IDontEB Posted May 25, 2019 Share Posted May 25, 2019 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? Quote Link to comment Share on other sites More sharing options...
Impensus Posted May 25, 2019 Author Share Posted May 25, 2019 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? Quote Link to comment Share on other sites More sharing options...
Impensus Posted May 25, 2019 Author Share Posted May 25, 2019 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? Quote Link to comment Share on other sites More sharing options...
liverare Posted May 25, 2019 Share Posted May 25, 2019 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. Quote Link to comment Share on other sites More sharing options...
Tom Posted May 25, 2019 Share Posted May 25, 2019 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. Quote Link to comment Share on other sites More sharing options...