Thread: Delayed Events

Results 1 to 5 of 5
  1. #1 Delayed Events 
    Programmer, Contributor, RM and Veteran




    Join Date
    Mar 2007
    Posts
    5,074
    Thanks given
    2,625
    Thanks received
    3,579
    Discord
    View profile
    Rep Power
    5000
    The AbstractDelayedEvent class, and the corresponding DelayedEventHandler class can be used to execute a task after a certain amount of time.

    An example is turning the character into an NPC for 30 seconds.

    Usually you would use some sort of timer, create a new variable for a timer (using up valuable memory) and add checks to a majorUpdate.

    You could also use the AbstractDelayedEvent class.

    An example is shown here:

    Code:
    // import classes we use
    import net.varek.rs2d.model.Player;
    import net.varek.rs2d.event.AbstractDelayedEvent;
    // the class
    public class MyEvent extends AbstractDelayedEvent {
    
    	// the constructor, call the constructor of the AbstractDelayedEvent class using super
    	public MyEvent(Player owner, int delay) {
    		super(owner,delay);
    	}
    
    	// this is the method that is called after however long you specify
    	public void run() {
    		// getOwner() returns the owner of the class (ie the player)
    		// getActionSender() returns the player's action sender
    		// sendMessage() sends a message
    		getOwner().getActionSender().sendMessage("This happened after 10 seconds after you clicked the object.");
    		// now terminate the event, so it will not happen again
    		terminate();
    		// note: you could also restart the event like so:
    		// setInterval(10000); - this would restart it in 10 seconds
    	}
    
    }
    To use this example, you could add some code to your object handling like so:

    Code:
    World.getWorld().getDelayedEventHandler().add(new MyEvent(player,10000));
    World.getWorld() will return the singleton of the world class, getDelayedEventHandler() returns the class that handles the various events, add() will add your event.

    The first argument in the constructor is the player, this can be set to null (I believe) if it is not related to any player. The second argument is the delay in milliseconds (1 millisecond = 1 second).

    In this example it would print the message 10 seconds after the object was clicked.

    NOTE: There is a bug in the AbstractDelayedEvent file that you have probably got.

    The constructor will look like this:

    Code:
    	public AbstractDelayedEvent(Player owner, int delay) {
    		this.owner = owner;
    		setInterval(delay);
    		this.running = true;
    	}
    It should look like this:

    Code:
    	public AbstractDelayedEvent(Player owner, int delay) {
    		this.owner = owner;
    		this.running = true;
    		setInterval(delay);
    	}
    If you do not make this change, all of your events will run instantly - not after the delay.

    If you wonder why it happens, it is because the setInterval checks if it is running. Running is set to false by default. If it is set to false, setInterval will always make the interval 0. Therefore, it will happen immediately.

    I hope this tutorial has taught you something useful, and spared you from the various actionTimer's of Winterlove.

    However, one thing to note, is that if you click the object or whatever a lot, it will queue up a lot of events. You will still need to do various checks for mass clicking/waiting for an existing event to complete yourself.
    Reply With Quote  
     

  2. #2  
    Jim
    Jim is offline
    I dunno, its fine I guess

    Join Date
    Feb 2007
    Posts
    147
    Thanks given
    12
    Thanks received
    33
    Rep Power
    265
    Very nice, I noticed this in the SVN commit you made. I didn't know what those runnable events could be used for .
    Reply With Quote  
     

  3. #3  
    Registered Member

    Join Date
    Aug 2007
    Posts
    2,395
    Thanks given
    2
    Thanks received
    63
    Rep Power
    558
    Interesting
    Thanks for it

    Jonas
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  4. #4  
    Programmer, Contributor, RM and Veteran




    Join Date
    Mar 2007
    Posts
    5,074
    Thanks given
    2,625
    Thanks received
    3,579
    Discord
    View profile
    Rep Power
    5000
    Quote Originally Posted by Jim View Post
    Very nice, I noticed this in the SVN commit you made. I didn't know what those runnable events could be used for .
    Yeah, they are useful. We use a somewhat similar system in RuneEmu.

    I guess the next step is to make a more unified way of doing this and agree on a system in which the player can only do one thing at once (like at the moment they can press mine 20 times and 5 seconds later they will get 20 ores).
    Reply With Quote  
     

  5. #5  
    Registered Member
    Join Date
    Aug 2006
    Posts
    50
    Thanks given
    0
    Thanks received
    0
    Rep Power
    4
    i used to have something alot like this in my wL server, i used it for firemaking and sheep sheering xD
    [Only registered and activated users can see links. ]
    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

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •