Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Help with threads


    UberJason

    Recommended Posts

    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;
        }
    
    }

     

    Link to comment
    Share on other sites

    Your variable called api is never defined in TestThread, you're getting a NPE in your run method which dreambot probably isn't catching.  

    Link to comment
    Share on other sites

    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.

    Link to comment
    Share on other sites

    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 :)

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.