Author Topic: Avoiding the DelayCommand  (Read 342 times)

Legacy_The Mad Poet

  • Hero Member
  • *****
  • Posts: 715
  • Karma: +0/-0
Avoiding the DelayCommand
« on: March 17, 2014, 07:38:36 am »


               

I've got a rather long delay to setup. 15 minutes to be exact.


 


I know it's good advice to keep DelayCommands to a minimal, and short times. 


 


What's my other options? And would someone be kind enough to show me an example?



               
               

               
            

Legacy_Proleric

  • Hero Member
  • *****
  • Posts: 1750
  • Karma: +0/-0
Avoiding the DelayCommand
« Reply #1 on: March 17, 2014, 03:52:35 pm »


               The simplest way for short time intervals is to count heartbeats.

When the clock starts, set a counter (local int) to zero.

Increase by 1 on heartbeat. There are 10 heartbeats per minute, so when the count reaches 150, do the command.

You'll also need to set a flag (local int) to tell the heartbeat script to start / stop counting.

For 15 minutes in game time (as opposed to real time), the number of heartbeats is

FloatToInt(15. * HoursToSeconds(1) / (6. * 60.)); // 6 seconds per heartbeat, 60 seconds per hour
               
               

               
            

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
Avoiding the DelayCommand
« Reply #2 on: March 17, 2014, 04:49:21 pm »


               

If you run a pseudoheartbeat to do this, the load will be much worse than for a simple DelayCommand. The best way to do this, if it can be made to work for your situtation, is a variant of the above suggestion by Proleric. Set up a timestamping system in the module heartbeat - basically, an 'uptime' counter that increments every module heartbeat. You can add 6 to the count if you want it roughly in seconds, otherwise you can just increment by 1 for number of rounds.


 


When you have something you want to timestamp - that is, mark for expiration of a time period - just:


1 ) get the current uptime on the module, stored in that variable


2) store either the start of the expiry period time, or the end time you want it to expire, in terms of uptime


3) find an event that will fire before that expiry time needs to be checked, and check it there


 


This is a lot simpler than it sounds. Take, for example, a despawn script that clears the area, and respawns it on a delay. Let's say we don't like those delays, and want to eliminate them (they're about an hour long, and there's one for each placeable). This is a perfect sitation for a timestamp.


 


Why? Because we don't care about exactly when the items respawn - we just care that they respawn the next time a player enters the area, if an hour has passed. So, we use the timestamp system above, and check the timestamp on area entry. If the required amount of time has passed, we respawn right then - no delays involved - and wipe the variable indicating a respawn is due after x time.


 


If you can't use a timestamping setup, because there's no event to fire, just stick with simple delaycommands - they're far less intensive than using heartbeats for each delayed event.


 


Funky



               
               

               
            

Legacy_The Mad Poet

  • Hero Member
  • *****
  • Posts: 715
  • Karma: +0/-0
Avoiding the DelayCommand
« Reply #3 on: March 17, 2014, 08:27:46 pm »


               

So, just to make sure I understand...


 


On your heartbeat you can create a single variable on the module that increments each heartbeat. Such as like 'nRounds++' or something.


 


Then when I need to check one length of time against another I can, at the first interval, get the current value of nRounds, and later check in an event check that value against itself, + how many rounds have passed until I want it to happen.


 


Pretty elegant solution.



               
               

               
            

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
Avoiding the DelayCommand
« Reply #4 on: March 18, 2014, 09:33:20 pm »


               

That's what I'm suggesting, yup. The way we do it is a bit more complicated - we check uptime against database time, for increased accuracy. It works out basically the same in terms of checking durations, though.


 


Take a look at this snippet for how we do this. It's the uptime variable we're dealing with here (we use the others for more esoteric purposes, like a cross-server player listing, and tracking of boot times of different instances we use to confirm who was on what server when it crashed):


 


hg_mod_heartbeat snippet (full code is much longer)


 


Funky



               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Avoiding the DelayCommand
« Reply #5 on: March 19, 2014, 04:23:00 am »


               I would suggest that you just simply use a delay command