Lewis Posted December 4, 2016 Posted December 4, 2016 basically looking to world hop a random amount of time(similar to sleep(random(300, 500)) ) just need help on being able to intitate the world hop i.e if time has been between 30 mins - 1 hour since last world hop { do stuff }
House Posted December 4, 2016 Posted December 4, 2016 (edited) store the last hop time in a long. Check if the current time - the last time you stored > time your want in between. If so hop. Edited December 4, 2016 by House
Lewis Posted December 4, 2016 Author Posted December 4, 2016 long lastHop = System.currentTimeMillis();if(lastHop >= (pastTime + 30*60000) { //multiply by 60000 to get minsdoSomething();} like this? is what i can find, but its after a set amount of time rather than random between two ints
House Posted December 4, 2016 Posted December 4, 2016 long lastHop = System.currentTimeMillis(); if(lastHop >= (pastTime + 30*60000) { //multiply by 60000 to get mins doSomething(); } like this? is what i can find, but its after a set amount of time rather than random between two ints so randomize it?
TzTok Jad Posted December 4, 2016 Posted December 4, 2016 long lastHop = System.currentTimeMillis(); if(lastHop >= (pastTime + 30*60000) { //multiply by 60000 to get mins doSomething(); } like this? is what i can find, but its after a set amount of time rather than random between two ints I'm not trying to berate you or anything but I really think it would benefit you greatly to learn the basics first, all of this trivial you should be able to come up with the solution on your own after just an hour of reading or even less. 2
Team Cape Posted December 4, 2016 Posted December 4, 2016 I'm not trying to berate you or anything but I really think it would benefit you greatly to learn the basics first, all of this trivial you should be able to come up with the solution on your own after just an hour of reading or even less.
Easy Posted December 4, 2016 Posted December 4, 2016 (edited) private Timer worldHoppingTimer = new Timer(); public int onLoop() { if (worldHoppingTimer.isActive()) { //do something here int hour = 3600000; int minHours = 1 * hour; int maxHours = 3 * hour; worldHoppingTimer.setTimeout(random(minHours, minHours)); //set the timer to go off in 1 - 3 hours (just an example) } return 100; } public class Timer { private long timeout; public Timer() { timeout = System.currentTimeMillis(); } public boolean isActive() { return timeout > System.currentTimeMillis(); } public void setTimeout(long timeout) { this.timeout = timeout + System.currentTimeMillis(); } public long getElapsedTime() { return System.currentTimeMillis() - timeout; } public String getElapsedToString() { return Format.msToString(getElapsedTime()); } public long getPerHour(long value) { return value * 3600000 / getElapsedTime(); } } Edited March 25, 2017 by Easy