event
The event function is triggered by
entity events
when the corresponding emask flag is set.
Type:
Function pointer.
Remarks:
-
Event functions are actually executed during
the engine function that caused the event,
like a c_move, c_scan,
or c_trace instruction of another entity. The
event function itself should be simple. It normally should only
transfer information to the entities' main
function - it shouldn't
perform instructions that can trigger events itself, displace entities,
or change anything else in the level. Thus instructions like c_move, ent_create, ent_remove, c_trace etc.
must not be performed. Otherwise all sorts of
bad things can happen, like two entities endlessly triggering each
other's
event
(the
game would freeze in that case). If for some reason the event function
must perform such 'critical instructions', they must be preceded
by a wait(1)
for delaying them to the next frame. Then it's safe.
- After
start of an event function, all event-dependent
variables and pointers, like normal etc., keep
their values only until the next wait instruction. During
the wait pause
they can (and
certainly will) be changed by other functions. If you want to keep
them longer, copy them to local variables or entity skills. For clarity,
let the entities' main
action do most
of the work, and keep the event function as
short and simple as possible, without any wait instructions.
Example (lite-C):
// The following example shows how to use events for an object that flies ahead until it collides with a block or entity.
// The event function then plays a collision sound and lets the object ricochet from the surface.
function bounce_event()
{
switch (event_type)
{
case EVENT_BLOCK:
ent_playsound(my,whamm,50);
vec_to_angle(my.pan,bounce); // change direction
return;
case EVENT_ENTITY:
ent_playsound(my,boingg,50); // play a different sound
vec_to_angle(my.pan,bounce); // change direction
return;
}
}
action bounceball()
{
my.emask |= (ENABLE_BLOCK | ENABLE_ENTITY); // make entity sensitive for block and entity collision
my.event = bounce_event;
while(1)
{
c_move(me,vector(5*time_step,0,0),nullvector,0); // move ahead until obstacle is hit
wait(1);
}
}
Example (C-Script):
function bounce_event()
{
if (event_type == EVENT_BLOCK)
{
ent_playsound(my,whamm,50);
vec_to_angle (my.pan,bounce); // change direction
return;
}
if (event_type == EVENT_ENTITY)
{
ent_playsound(my,boingg,50); // play a different sound
vec_to_angle (my.pan,bounce); // change direction
return;
}
}
action bounceball()
{
my.ENABLE_BLOCK = ON; // make entity sensitive for block collision
my.ENABLE_ENTITY = ON; // make entity sensitive for entity collision
my.event = bounce_event;
while(1)
{
c_move(me,vector(5*time_step,0,0),nullvector,0); // move ahead until obstacle is hit
wait(1);
}
}
See also:
Events, event_type ► latest
version online