TimerEvent

From OPU Wiki

The most basic of all events. Simular to the OP2 exported TimeTrigger.

Inherited from OP2Event


Description

Just like with the TimeTrigger, there are 2 versions: a single-time event, which fires at exactly time ticks (100 ticks = 1 timemark) after the event was created. The other one fires the event at a random time between the 2 supplied params (starting the count again from the moment the constructor was called).


On a sidenote: 1 game time mark consists of 100 gameticks, but AIProc gets called every 4th gametick. This means a TimerEvent for 100 and for 102 get called at the same time.


The size of the TimerEvent class is 31 bytes and uses 2 memoryblocks.


Constructors

Do NOT call the constructors directly, use the CreateTimerEvent functions instead:

TimerEvent *CreateTimerEvent(bool enable,bool norepeat,int time1,int time2,_callbackfunc callback,int id=0);
TimerEvent *CreateTimerEvent(bool enable,bool norepeat,int time,_callbackfunc callback,int id=0);

An id can be supplied. This can be an integer or any other variable type:

void DoTimer(OP2Event *timer)
{
  // Your code goes here
}
char *name="Timer 3";
TimerEvent *timer1,*timer2,*timer3;

timer1=CreateTimerEvent(1,0,200,DoTimer);
timer2=CreateTimerEvent(1,1,500,DoTimer,12);
timer3=CreateTimerEvent(0,1,500,800,DoTimer,(int)name);

timer3->Enable();

timer1 will fire every 2 timemarks, and its ID is set to 0 (by default).

timer2 will fire only once, 5 timemarks from now, and its ID is set to 12.

timer3 will fire only once, but has not yet been enabled. After it has been enabled, it will fire within 5 to 8 timemarks (random). Its ID is set to point to the "Timer 3" string. You need to use the pointer indication -> instead of the usual period, because CreateTimerEvent returns a pointer.

Members

type name description
int GetTime1( ) timeticks after which it should fire
int GetTime2( ) if Time2 > Time1 it creates a random time between the 2 time params
int GetFireTick( ) the absolute timetick when this event fires