Thread: [Pi] Using timers

Results 1 to 8 of 8
  1. #1 [Pi] Using timers 
    Donator

    Join Date
    Jul 2013
    Posts
    488
    Thanks given
    21
    Thanks received
    42
    Rep Power
    0
    I am unable to fully understand system.currenttimemillis() and how to use it properly.

    The way in which i would like to use it would be for example to make it pause when a player opens a casket. So the player would click open casket, a message would appear "you peer inside the casket..." and then another message (for example) 3 seconds later says "..and recieve 25m coins!".

    Please could somebody explain it to me? or show me how i would do that.
    Reply With Quote  
     

  2. #2  
    Donator

    Jason's Avatar
    Join Date
    Aug 2009
    Posts
    6,092
    Thanks given
    2,402
    Thanks received
    2,823
    Rep Power
    4550
    Think about it like this. You have a variable called lastAction;

    Code:
    long lastAction;
    Right now this variables hasn't be initialized, meaning it have no value. What i'm about to do is make it equal to the current time translated into milliseconds.

    Code:
    lastAction = System.currentTimeMillis();
    What you want to know is if enough time has passed so the player can continue doing an action. There is an easy way to do start doing this, subtract the time of the last action, with the current time.

    Code:
    long difference = System.currentTimeMillis() - lastAction
    At this point we know how long it has been, in milliseconds since the last action. We have a variable that is storing this information. Now compare the difference to some value like so;

    Code:
    if(difference > (1000 * 30))
        doAction();
    Hope this helps.
    Reply With Quote  
     

  3. Thankful users:


  4. #3  
    Donator

    Join Date
    Jul 2013
    Posts
    488
    Thanks given
    21
    Thanks received
    42
    Rep Power
    0
    Quote Originally Posted by Jason View Post
    Think about it like this. You have a variable called lastAction;

    Code:
    long lastAction;
    Right now this variables hasn't be initialized, meaning it have no value. What i'm about to do is make it equal to the current time translated into milliseconds.

    Code:
    lastAction = System.currentTimeMillis();
    What you want to know is if enough time has passed so the player can continue doing an action. There is an easy way to do start doing this, subtract the time of the last action, with the current time.

    Code:
    long difference = System.currentTimeMillis() - lastAction
    At this point we know how long it has been, in milliseconds since the last action. We have a variable that is storing this information. Now compare the difference to some value like so;

    Code:
    if(difference > (1000 * 30))
        doAction();
    Hope this helps.
    just for a template for the future how would you enforce the above onto:
    Code:
    if (playerCommand.startsWith("home")) {
    c.getPA().startTeleport(1000, 1000, 0, "modern");
    }
    To make the command say c.sendMessage("You are about to teleport"); before teleporting and then c.sendMessage("You have teleported home") after teleporting
    Reply With Quote  
     

  5. #4  
    Banned [Pi] Using timers Market Banned


    Join Date
    Jan 2011
    Age
    26
    Posts
    3,112
    Thanks given
    1,198
    Thanks received
    1,479
    Rep Power
    0
    lol if you want delayed actions use a task, if you want to be able to throttle actions use system timers


    example of using task: teleporting (you click the button and you are teleported after the animation and gfx are done)
    example of using system timers: eating food (you can only eat once every xx seconds)
    Reply With Quote  
     

  6. #5  
    🍥🍥🍥


    Join Date
    Dec 2008
    Posts
    1,702
    Thanks given
    664
    Thanks received
    293
    Rep Power
    621
    Quote Originally Posted by Jason View Post
    Think about it like this. You have a variable called lastAction;

    Code:
    long lastAction;
    Right now this variables hasn't be initialized, meaning it have no value. What i'm about to do is make it equal to the current time translated into milliseconds.

    Code:
    lastAction = System.currentTimeMillis();
    What you want to know is if enough time has passed so the player can continue doing an action. There is an easy way to do start doing this, subtract the time of the last action, with the current time.

    Code:
    long difference = System.currentTimeMillis() - lastAction
    At this point we know how long it has been, in milliseconds since the last action. We have a variable that is storing this information. Now compare the difference to some value like so;

    Code:
    if(difference > (1000 * 30))
        doAction();
    Hope this helps.
    god no, for something what OP is asking for: use the event/task system thats already in the source
    Reply With Quote  
     

  7. #6  
    Registered Member

    Join Date
    Dec 2010
    Posts
    1,982
    Thanks given
    174
    Thanks received
    256
    Rep Power
    223
    Quote Originally Posted by Global View Post
    god no, for something what OP is asking for: use the event/task system thats already in the source
    He asked how to use System.currentTimeMillis(); Jason answered that question.
    Reply With Quote  
     

  8. #7  
    🍥🍥🍥


    Join Date
    Dec 2008
    Posts
    1,702
    Thanks given
    664
    Thanks received
    293
    Rep Power
    621
    Quote Originally Posted by Someone View Post
    He asked how to use System.currentTimeMillis(); Jason answered that question.
    oh oops, but still op should use event/task system for what hes tryna do
    Reply With Quote  
     

  9. #8  
    Donator

    Jason's Avatar
    Join Date
    Aug 2009
    Posts
    6,092
    Thanks given
    2,402
    Thanks received
    2,823
    Rep Power
    4550
    Quote Originally Posted by Global View Post
    god no, for something what OP is asking for: use the event/task system thats already in the source
    He asked how to use the function currentTimeMillis(), he didn't ask for a better alternative.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 13
    Last Post: 07-19-2010, 11:32 PM
  2. how do i use timer and timertask??
    By sigmą in forum Help
    Replies: 16
    Last Post: 07-16-2010, 08:45 AM
  3. Using Timer class, API
    By Greyfield in forum Snippets
    Replies: 28
    Last Post: 03-18-2010, 06:29 PM
  4. [x-log fix useing timers]
    By Neekage in forum Tutorials
    Replies: 26
    Last Post: 11-28-2009, 11:57 PM
  5. Adding a Easy-To-Use Timer!
    By LesterKnome in forum Tutorials
    Replies: 11
    Last Post: 08-05-2009, 07:26 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •