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
  • How to calculate how long a script has been running


    Verify

    Recommended Posts

    Here's the class using setStartTime

    public class FirstScript extends TaskScript {
    
        private Anti t;
    
        @Override
        public void onStart() {
            t = new Anti(this);
            t.setStartTime();
        }
    }

    I want the script to set START_TIME as the current time when I start the script. I then try to use this variable in a method such as getStartTime in an other class like so: 

    public class Anti {
    
        public long START_TIME;
    
        public Anti(ScriptTest script) {
        }
    
        public double getStartTime() {
            double time = START_TIME;
            MethodProvider.log("[" + time + "]");
    
            return time;
        }
    
        public void setStartTime() {
            this.START_TIME = System.currentTimeMillis();
        }
    }

    The problem I am encountering is that the log from getStartTime display 0.0 instead of the current time, so I know something failed somewhere, but I can't figure out where. Any pointers would be appreciated. Thanks

    Link to comment
    Share on other sites

    You're not showing us the way and when you call Anti::getStartTime, you might be calling it before assigning START_TIME any value. The code below works, so perhaps you'll find it helpful. Also, the DreamBot API has a built in Timer you could also use. It has some cool methods in it.

    public class FirstScript extends TaskScript {
    
        private Anti t;
    
        @Override
        public void onStart() {
            t = new Anti(this);
        }
    
        @Override
        public void onExit() {
            log(String.format("Time at start: %d", t.START_TIME));
            log(String.format("Time now: %d", System.currentTimeMillis()));
            log(String.format("Elapsed time: %d", t.getElapsedTime()));
        }
    }

     

    public class Anti {
    
        public final long START_TIME;
    
        public Anti(FirstScript script) {
            START_TIME = System.currentTimeMillis();
        }
    
        public long getElapsedTime() {
            return System.currentTimeMillis() - START_TIME;
        }
    
    }
    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.