UberJason 41 Share Posted July 14, 2022 (edited) Hi. I've been trying to get threads to work for a while now but just can't seem to figure them out. I know this isn't a client issue, but with other clients & trying different things, most of the time I just get an unhandled exception otherwise the thread just seems to do nothing. If anyone can help with the code below I'd appreciate it. import org.dreambot.api.script.AbstractScript; import org.dreambot.api.script.Category; import org.dreambot.api.script.ScriptManifest; @ScriptManifest(author = "Test", category = Category.MISC, name = "Thread Test", version = 1.0) public class Main extends AbstractScript { TestThread test = new TestThread(); @Override public void onStart () { Thread Test = new Thread(test); Test.start(); } @Override public int onLoop() { return 1000; } } class TestThread extends Thread { AbstractScript api; public volatile boolean run = true; @Override public void run () { while (run) { if (!api.getLocalPlayer().isInCombat()) { System.out.println("True"); } } } public void Stop () { run = false; } } Edited July 14, 2022 by UberJason Link to comment Share on other sites More sharing options...
SubCZ 282 Share Posted July 14, 2022 Your variable called api is never defined in TestThread, you're getting a NPE in your run method which dreambot probably isn't catching. UberJason and OG Botter 2 Link to comment Share on other sites More sharing options...
Pandemic 2723 Share Posted July 14, 2022 Our API is static so you don't need the API variable anyways, you can just use Players.localPlayer().isInCombat() directly from the new thread. However, be aware that the majority of our API isn't designed to be thread safe so it could behave strangely. We always recommend doing any game actions in the main script thread. UberJason 1 Link to comment Share on other sites More sharing options...
UberJason 41 Author Share Posted July 15, 2022 (edited) 2 hours ago, Pandemic said: Our API is static so you don't need the API variable anyways, you can just use Players.localPlayer().isInCombat() directly from the new thread. However, be aware that the majority of our API isn't designed to be thread safe so it could behave strangely. We always recommend doing any game actions in the main script thread. Gotcha, ty Edited July 15, 2022 by UberJason Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now