void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
The first parameter is CBaseEntity *pActivator
. When the Use function is called, this parameter identifies the entity which activated this particular Use function. It does not have to be the entity which actually called this entity's Use function. For example, if a player walks into a trigger_once, it calls its target's Use function and passes the player pointer as the activator.*pCaller
is the entity which called the particular Use
function. From the Use
function, you can modify the variables and call more functions to perform actions on those entities as you see fit.USE_ON
, USE_OFF
, and USE_TOGGLE
. You can check what useType
is and then perform different actions based on its value.Use
, Touch
, or Think
, you must put EXPORT
between the function type and function name.
void EXPORT UseFunction( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
If you forget the EXPORT
, the function will not be remembered between level changes or level saves. It is vitally important that you check to make sure all your functions have this.Use
is if you have multiple Use functions. The particular Use function that will be called is determined by using the SetUse( functionname );
command.
SetUse( UseFunction );
If you only have one Use function, just keep it named Use
and it will automatically be set, no SetUse
needed. To fire an entity's Use function, use the FireTargets
function in an entity; this will fire its pev->target
's Use function (assuming it has one).SetUse( NULL );
Bam! No more use function for it. And of course if you want it back, you can just call SetUse
with an actual function instead of NULL
.
DispatchTouch
function which in return calls the touched entity's Touch function with the pointer of the toucher.
void Touch( CBaseEntity *pOther );
CBaseEntity *pOther
is the entity which touched the entity with the Touch function. Again, you can modify it however you like. The Touch function shares many of the same properties as the Use function. You MUST have EXPORT
between the function type and name:
void TouchFunction( CBaseEntity *pOther );
If the name of the function is Touch, you do not need to use the SetTouch
function either. Otherwise, you must use the SetTouch
command in the same manner as the SetUse
function.
SetTouch( TouchFunction );
Again like use functions you can also remove a touch function from being set. One simple line:
SetTouch( NULL );
HuntThink
function. But, within the HuntThink
function is called the Flight
function, which controls the dynamic physics to tilt the chopper in the appropriate direction as it flies. Here is what a basic Think function looks like:
void Think( void );
Pretty plain. Now let's look at a Think function's actual body:
void CYourClass::Think( void )
{
ALERT(at_console, "I am thinking!\n");
pev->nextthink = gpGlobals->time + 0.1;
}
Ok, time to detail. The first line with the ALERT
is just a simple alert to tell us that our Think function is being called. Now the second line is extremely important in any Think function. pev->nextthink
is a variable that stores the time value for the engine to use. The engine uses this number to know when to trigger the think function again. In this case, we're taking the current global time (gpGlobals->time
) and adding 0.1 seconds to that. Right, so we'll get the message "I am thinking!" about 10 times a second. It's a simple Think function and serves no practical purpose, but it is indeed a Think function.SetThink
call within an entity's Spawn function, such as:
SetThink( ThinkFunction );
Followed by:
pev->nextthink = gpGlobals->time + X.X;
Again, that tells the engine at what time to call our Think function. If it is just named Think, there is no need for a SetThink, but you still must set the pev->nextthink
variable to a time value.SetThink( NULL );
But, you can also set nextthink to something before the current global time, for example:
pev->nextthink = 0;
If set before the global time, the engine will never call the function again because that time has already passed. This is useful if you want to keep the think function attached to the entity, but might not want to think in specific cases.
CBaseEntity::Create
and set its angles to whatever. I hope you've learned just how important the Think, Touch, and Use functions are in creating everything and anything in Half-Life.
You must log in to post a comment. You can login or register a new account.