Lesson 3: Using engine functions

Engine functions can be used in external languages just as in the script language. All engine functions are available, and also most engine functions except for script-specific functions like wait. Under C/C++ you normally don't use multitasking and thus won't and can't use the script multitasking functions. Instead, use the Windows Sleep() function for waiting a certain time, or the EVENT_FRAME for triggering an entity function every frame cycle.

This is an example for an entity AI function that uses vector and collision functions for scanning the environment of an entity:

// returns free distance in front of MY entity until next obstacle
DLLFUNC var DistAhead(ENTITY *ent)
{
	if (!ent) return 0;
	VECTOR vtarget = { _VAR(1000.0),_VAR(0),_VAR(0) };	// trace target vector

// rotate vector by entity engles, just as in lite-C
	vec_rotate(&vtarget,(ANGLE*)&(ent->pan));

// add entity position to target
	vtarget.x += ent->x;
	vtarget.y += ent->y;
	vtarget.z += ent->z;

// set trace_mode, then trace a line between entity and target,
// and return the result
	v(trace_mode) = _VAR(IGNORE_ME|IGNORE_PASSABLE|USE_BOX);
	return trace((VECTOR*)&(ent->x),&vtarget);
}
The difference to lite-C is that you have to take more care to use proper types for the arguments. Some special lite-C functions, like inkey() for keyboard entry, can not be called directly from a DLL because they use the lite-C multitasking system. However they can be executed indirectly by calling a script that executes that function. User defined scripts can be called from a DLL through the following functions:

long engine_getscript(char *name);

This function returns an addresss of the user-defined script function with the given name. It can be used to call user defined lite-C actions or functions from inside a DLL plug-in. If the function is not found, NULL is returned and an error message will pop up. Example for a DLL function that calls a function that must be defined in the lite-C code:

//prototype
typedef void (*PF)();
PF beeptwice = NULL; 

DLLFUNC void litecBeep(void)
{
// get the function
  if (!beeptwice)
  	beeptwice = (PF)engine_getscript("beeptwice");
// call it
  beeptwice();
}
This DLL function expects the following function in the lite-C script which is then called:
function beeptwice() { beep(); beep(); } // in the script
Now that we have learned to access every part of lite-C by a DLL and vice versa, let's continue with some special applications for DLL functions. ► latest version online