CBaseEntity Functions

From Valve Developer Community
Jump to: navigation, search

CBaseEntity derives from IServerEntity.

As its name suggests, all entity types derive from it.

Note: This article is under-construction. As with any wiki, please feel free to add to it, or edit it. My goal here is to document every single function in the 2013 sdk.

GetAbsOrigin()

GetAbsOrigin() returns the absolute (i.e. world-relative) origin of an entity as a vector. Its sister function SetAbsOrigin() changes it.

The origin is actually stored in local space (i.e parent-relative), meaning that the Abs functions perform extra calculations to get their return value. Use the more direct GetLocalOrigin() and SetLocalOrigin() if possible; they work directly with the stored value (m_vecOrigin).

Note:In fact, GetAbsOrigin() seems to recalculate its return value only if it was changed since the last time it was used, otherwise it just returns cached value stored in a member variable.

Example Usage:

void CMyEntity::Think()
{
	BaseClass::Think(); // Always do this if you override Think()

	Msg( "My World Position Is %f %f %f.\n",GetAbsOrigin().x,GetAbsOrigin().y,GetAbsOrigin().z);
}

GetAbsAngles()

GetAbsAngles() returns the absolute (i.e. world-relative) angles of an entity as a QAngle. Its sister function SetAbsAngles() changes it.

Similar to GetAbsOrigin(), the actual orientation information about this entity is stored relative to its parent - and thus, extra calculations are needed to fetch its Absolute Orientation.

Example Usage:

void CMyEntity::Think()
{
	BaseClass::Think(); // Always do this if you override Think()

	Msg( "My World Angle Is %f %f %f.\n",GetAbsAngles().x,GetAbsAngles().y,GetAbsAngles().z);
}

DecalTrace()

Parameters:

  • trace_t *pTrace is the traceline along which the decal will be applied.
  • char const *decalName is the name of the decal that is to be applied.

Applies a decal to this entity, in the direction of pTrace. This function is usually not called directly, but through UTIL_DecalTrace().

Example Usage:

trace_t	tr;
UTIL_TraceLine( GetAbsOrigin(), GetAbsOrigin() - Vector( 0, 0, 128 ), MASK_SOLID_BRUSHONLY, this, COLLISION_GROUP_NONE, &tr );

if(tr.fraction == 1)
{
   CBaseEntity *pEntity = tr.m_pEnt;
   pEntity->DecalTrace(&tr,"Scorch");
}

EntityToWorldTransform()

Returns:

  • matrix3x4_t & is a 3x4 matrix denoting the linear transformation from the entity's coordinate frame to the world coordinate frame. The first three columns of the matrix are the world-relative positions of the x,y,and z-axes respectively. The last column is the position of the entity's origin relative to the world.

Example Usage:

matrix3x4_t worldPos = EntityToWorldTransform();
Msg("This Entity's Position is %f %f %f/n",worldPos[0][3],worldPos[1][3],worldPos[2][3]);

ShouldCollide()

Parameters:

  • int collisionGroup The collision group we are testing this entity against.
  • int contentsMask Similar to UTIL_TraceLine's MASK_ parameter. It represents the MASK_ types to collide with.

Returns:

  • bool True if this entity should collide with the given collisionGroup with the given contentsMask.

Example Usage:

bool ShouldHitEntity( IHandleEntity *pHandleEntity, int contentsMask )
{
   CBaseEntity *pEntity = EntityFromEntityHandle( pHandleEntity );
		
   if ( pEntity )
   {
      if ( !pEntity->ShouldCollide( m_collisionGroup, contentsMask ) )
         return false;
   }
   Msg("We should hit this guy!\n");
}

SetModel()

Parameters

  • const char *szModelName Name of the Model (You need to add models in your path to your .mdl file)

Returns This function doesnt return anything

Example Usage:

SetModel("models/player/player.mdl");

GetTeamNumber()

Parameters This function doesn't have any parameters

Returns

  • Returns the Team Number from m_iTeamNum

NOTE: TEAM_COMBINE (2) and TEAM_REBELS (3) are defined in hl2mp_gamerules.h however 0 means no team selected yet and 1 means they are spectator

Example Usage:

void CHL2MP_Player::Spawn(void)
{

	if(GetTeamNumber() == TEAM_COMBINE)
	{
		GiveAllItems(); // Gives combines all weapons on spawn
	}
	BaseClass::Spawn();
}

SetHealth()

Parameters

  • int amt Amount to set to an entity health to. This sets m_iHealth to amt.

Returns This function doesn't return anything.


Example Usage:

void CHL2MP_Player::Spawn(void)
{

	SetHealth(200); //Players now have 200 HP on spawn
	BaseClass::Spawn();
}

GetHealth()

Parameters This function doesn't have any parameters

Returns

  • Returns the current entity's health It return's m_iHealth.

NOTE: GetHealth returns the health as Integer not as float.

SetMaxHealth()

Parameters

  • int amt Amount to set to an entity's max health to. This sets m_iMaxHealth to amt.

Returns This function doesn't return anything.


Example Usage:

void CHL2MP_Player::Spawn(void)
{

	SetMaxHealth(200); //Players maximum HP is now 200
	BaseClass::Spawn();
}

GetMaxHealth()

Parameters This function doesn't have any parameters.

Returns

  • Returns the current entity's max health It return's m_iMaxHealth.

NOTE: GetMaxHealth returns the health as Integer not as float.

ChangeTeam()

Parameters

Returns This function doesn't return anything.

Example Usage:

void CHL2MP_Player::Spawn(void)
{

	int team = random->RandomInt(2,3); // Random Value between 2 and 3
	ChangeTeam(team); // Set players team to the result from the above random int function
}

CollisionProp()

Returns CCollisionProperty * this entity's collision property. Main Article: CCollisionProperty

Example Usage:

void CAntlionGrub::Spawn( void )
{
   ...
   //Make my trigger bounds 1 unit bigger than my collision bounds.
   CollisionProp()->UseTriggerBounds(true,1);
   ...
}

SetSolid()

Main Article: SetSolid()

Parameters

  • SolidType_t What type of solid this entity will be set to. Depending on what this is set to, a different collision test primitive will be used. See this for more...

Example Usage:

void CNPC_AntlionGuard::Spawn( void )
{
   ...
   SetSolid( SOLID_BBOX );
   ...
}

GetSolid()

Returns SolidType_t. This is the type of solid this entity is. Look for SolidType_t in const.h to see the actual types.

Example Usage:

bool IsSolid(CBaseEntity* pEntity)
{
   if (pEntity->GetSolid() == SOLID_NONE)
      return false;

   return true;
}

GetSolidFlags()

Returns SolidFlags_t. This is the bitflags for the solid. Look for SolidFlags_t in const.h to see the actual flags.

Example Usage:

bool IsTrigger(CBaseEntity* pEntity)
{
   //returns true if this object will fire a touch function
   //even if the entity is not collideable
   return (pEntity->GetSolidFlags() == FSOLID_TRIGGER);
}

SetNavIgnore()

Parameters

  • float duration How long will the navigation ignore this entity for? Default value is FLT_MAX (infinity)

This function tells the navigation system to treat this entity as if it was not there whenever navigation is being determined. (i.e. an NPC trying to navigate through a path will not treat this entity as an obstacle).

Example Usage:

void CPropAirboat::EnterVehicle( CBaseCombatCharacter *pPlayer )
{
	BaseClass::EnterVehicle( pPlayer );

	//EnablePlayerBlocker( false );

	// NPCs like manhacks should try to hit us
	SetNavIgnore();
        ...
}

edict()

Returns edict_t*. see edict_t...

Returns this entity's edict.

Example Usage:

bool CItemSuit::MyTouch( CBasePlayer *pPlayer )
{
	if ( pPlayer->IsSuitEquipped() )
		return FALSE;

	if ( m_spawnflags & SF_SUIT_SHORTLOGON )
		UTIL_EmitSoundSuit(pPlayer->edict(), "!HEV_A0");		// short version of suit logon,
	else
		UTIL_EmitSoundSuit(pPlayer->edict(), "!HEV_AAx");	// long version of suit logon

	pPlayer->EquipSuit();
				
	return true;
}