Пређи на главни садржај

Timers module

· 2 минута читања
Y_Less

This sneak-peek showcases improvements to the timers module in open.mp, introducing a more flexible and efficient API while maintaining backward compatibility.

warning

The content of the following post is out of date and has no reference to the current state of open.mp. The post is published here for archival purposes.

This is a sneak-peek at one of the improved modules we've done, for timers in open.mp:

native SetTimer(const func[], msInterval, bool:repeat) = SetTimerEx;
native SetTimerEx(const func[], msInterval, bool:repeat, const params[], GLOBAL_TAG_TYPES:...);
native KillTimer(timer) = Timer_Kill;

// CreateTimer
native Timer:Timer_Create(const func[], usDelay, usInterval, repeatCount, const params[] = "", GLOBAL_TAG_TYPES:...);

// KillTimer
native bool:Timer_Kill(Timer:timer);

// Return time till next call.
native Timer_GetTimeRemaining(Timer:timer);

// Get number of calls left to make (0 for unlimited).
native Timer_GetCallsRemaining(Timer:timer);

// Get `repeatCount` parameter.
native Timer_GetTotalCalls(Timer:timer);

// Get `usInterval` parameter.
native Timer_GetInterval(Timer:timer);

// Reset time remaining till next call to `usInterval`.
native bool:Timer_Restart(Timer:timer);

The first two are just for backwards-compatibility, the rest are the improved API:

native Timer:Timer_Create(const func[], usDelay, usInterval, repeatCount, const params[] = "", GLOBAL_TAG_TYPES:...);
  • func - Fairly obvious; what to call.
  • usDelay - Again obvious, the delay before the call (in microseconds).
  • usInterval - What to reset usDelay to after the first call. So if you wanted a timer on the hour every hour, but it was 8:47am right now, the call would be Timer_Create("OnTheHour", 780 SECONDS, 3600 SECONDS, 0);
  • repeatCount - Unlike the old functions, which are just "once" or "forever", this instead takes the number of times to call the function. "once" would be 1, 500 would stop after 500 calls, and (backwards from the old API) 0 means "forever".
  • GLOBAL_TAG_TYPES - Like {Float, ...}, but with more tags.