UberJason 41 Posted July 14, 2022 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; } }
SubCZ 284 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.
Pandemic 2821 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 41 Author Posted July 15, 2022 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
Recommended Posts
Archived
This topic is now archived and is closed to further replies.