A pseudo-heartbeat is a scripting technique that mimics an OnHeartbeat event, although usually with a different duration or frequency. Essentially, this technique — a type of recursion — involves a function calling itself on a delay, which is then repeated until some goal is accomplished. (When the delay is six seconds and the repetition is as long as the caller exists, then this precisely mimics an OnHeartbeat event.) This is particularly useful when the delay needs to be less than six seconds (as heartbeats would be insufficient), or when the pseudo-heartbeat would end long before (or start well after) its caller does (as heartbeats would be overkill).


void PseudoHeartbeat()
    // Check for the pseudo-heartbeat no longer being needed.
    if ( Done() )

    // Do stuff here.

    // Recurse.
    DelayCommand(2.0, PseudoHeartbeat());

The above shows a typical structure for a pseudo-heartbeat. It begins by seeing if the pseudo-heartbeat is no longer needed, and ending if that is the case. After doing whatever tasks are needed on each pseudo-heartbeat, it schedules another execution of itself after some suitable delay.

Discretion should be exercised when using pseudo-heartbeats, as they do entail more overhead for the game than an OnHeartbeat event. That is, in comparing the overhead of using a pseudo-heartbeat that runs every six seconds for as long as the caller exists, to using the caller's OnHeartbeat event to accomplish the same task, the pseudo-heartbeat adds a larger load to the game. However, in this context, overhead accumulates only for each call (to the heartbeat or pseudo-heartbeat); if a pseudo-heartbeat has a highly limited lifespan or a long delay between calls, it can net less overhead than using an OnHeartbeat event to accomplish the same result. Poorly written pseudo-heartbeats can add a huge burden to the game, while well-written pseudo-heartbeats that are used appropriately can be the most efficient way to add a certain functionality to a module.