DelayCommand

The   NWScript command causes the indicated command (or function) to be executed after the indicated delay. The delay is a fixed number of seconds; in order to have a variable delay based upon certain actions completing,  should be used. Delayed commands are lost if the calling object does not exist when the delay is finished. (This can be gotten around by using  to assign the delay to a different object, often the module as returned by  .)

Delayed commands cannot be canceled (other than by destroying the calling object), but they can sometimes be lost under extreme server conditions (such as having thousands of concurrently delayed commands). Delayed commands are sometimes used to create pseudo-heartbeats, which in its most basic form involves a function that calls itself after a delay.

Multiple delays of the same duration should not be relied upon if efficiency or the order of execution is important. Instead, those commands should be collected into a single function, and the function delayed.

Definition
The expression passed as the second parameter will be evaluated after the delay is complete. This can impact, for example, commands (used as parameters) that find existing objects. If an expression needs to be evaluated at the time a delay is scheduled, it should be assigned to a variable, and the variable used in the delayed command.

There is a memory leak associated with creating effects in the parameters to a delayed command, with this leak being most prominent in the case of pseudo-heartbeats. For this reason, any effects passed as parameters to the delayed command should first be assigned to a variable, rather than using a function or command that returns an effect (in particular, the "Effect" commands). That is,

effect eDamage = EffectDamage(nDamage, DAMAGE_TYPE_MAGICAL); DelayCommand(fDelay, ApplyEffectToObject(DURATION_TYPE_INSTANT, eDamage, oTarget);

rather than

DelayCommand(fDelay, ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectDamage(nDamage, DAMAGE_TYPE_MAGICAL), oTarget);