Adding a timer inbetween an item use

04/12/2013 01:08 ajstyles316#1
Im basically trying to add a small timer in packethandler inbetween the "client.Send(Buf);" & the "Random Rand = new Random();" part so there is say a 5 second wait bewteen those 2 parts for the player when he uses the item.

Can anyone help with this?


Code:
 byte[] Buf = new byte[150 + 8]; 
                                 Writer.WriteUInt16(150, 0, Buf);
                                 Writer.WriteUInt16(10010, 2, Buf);
                                 Writer.WriteUInt32(client.Entity.UID, 4, Buf);
                                 Writer.WriteUInt32(0xD2, 8, Buf);
                                 Writer.WriteUInt32(164, 20, Buf);
                                 Writer.WriteUInt32(1, 22, Buf); 
                                 Writer.WriteUInt32(1, 32, Buf);
                                 Writer.WriteUInt32(1, 37, Buf);
                                 Writer.WriteUInt32(30, 38, Buf);
                                 Writer.WriteString("Planting Flower", 39, Buf);
                                 client.Send(Buf);

// add 5 second timer inbetween these 2 parts
                            
                        Random Rand = new Random();
                        int seed = Rand.Next(1, 5);
                        if (seed == 1) seed = 1;
                        if (seed == 2) seed = 2;
                        if (seed == 3) seed = 2;
                        if (seed == 4) seed = 1;
                        if (seed == 5) seed = 1;

                   if (seed == 2 && client.Entity.MapID == 1806)
                     {
04/12/2013 01:52 pro4never#2
You have two real options.

#1: Create a timer and execute that code inside the timer callback.

#2: Create some sort of timed events system for your server. This is the more scalable option in most cases but it's all personal preference really.
04/12/2013 03:07 ajstyles316#3
Could i use something like this inbetween there but instead of sending it to "MyTimer_Tick" once the timer is up make it just continue on from where it is to the next part, Is there a way to do that or not?

Code:
Timer MyTimer = new Timer();
        MyTimer.Interval = 3000;
        MyTimer.Tick += new EventHandler(MyTimer_Tick);
        MyTimer.Start();
04/12/2013 12:57 shadowman123#4
add this instance in Entity.cs

Time32 PlantTime;
then when u send the player the effect make sure u set PlantTime to Time32.Now;

then in TimerCall Back Or Thread make smthing like
if (Now >= client.Entity.PlantTime.AddSeconds(5)) { // ur cases }
04/12/2013 17:00 ajstyles316#5
Thanks Shadowman got it working with a few modifications, I notice a slight cpu hit with the way im doing it when when someone plants, Is it because of the "tryagain" loop im using to check if the time has run out? and if so is there another better way to do this check?

entity
Code:
public Time32 PlantStamp;
public byte PlantTime;
Timer callback
Code:
                       
 if (Now >= Owner.Entity.PlantStamp.AddSeconds(PlantTime))
     {
      Owner.Entity.PlantTime = 0;
     }
use code
Code:
#region Anemone Flower
  case 4000053:
        {
         Game.Features.LoadingBar.Loading.Bar1(client); 
         client.Entity.PlantStamp = Time32.Now;
         client.Entity.PlantTime = 5;
   tryagain:
         if (client.Entity.PlantTime == 0)
           {
            Game.Features.Plants.Anemone1.Anemone11(client);
          }
       else
            {
         goto tryagain;
          }
       }
   break;
#endregion
04/12/2013 18:31 shadowman123#6
Quote:
Originally Posted by ajstyles316 View Post

Code:
#region Anemone Flower
  case 4000053:
        {
         Game.Features.LoadingBar.Loading.Bar1(client); 
         client.Entity.PlantStamp = Time32.Now;
         client.Entity.PlantTime = 5;
   tryagain:
         if (client.Entity.PlantTime == 0)
           {
            Game.Features.Plants.Anemone1.Anemone11(client);
          }
       else
            {
         goto tryagain;
          }
       }
   break;
#endregion
Remove Plant time ...
Code:
                       
 if (Now >= Owner.Entity.PlantStamp.AddSeconds(5))
 {
    Owner.Entity.FinishedPlanting = true;
 }
as its obvious FinishedPlanting is Boolean instance inside Entity.cs
then in the Itemusing part

if (client.FinishedPlanting) { //ur cases }
else { send message to player telling him that he have to w8 few secs }