CBaseEntity Functions: Difference between revisions
Charlysotelo (talk | contribs) No edit summary |
Thunder4ik (talk | contribs) m (clean up, added orphan tag) |
||
(16 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
CBaseEntity derives from [[IServerEntity]]. | {{Orphan|date=January 2024}} | ||
[[CBaseEntity]] derives from [[IServerEntity]]. | |||
As its name suggests, all entity types derive from it. | 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() == | ||
Line 26: | Line 28: | ||
<code>GetAbsAngles()</code> returns the absolute (i.e. [[world]]-relative) [[angles]] of an entity as a [[QAngle]]. Its sister function '''<code>SetAbsAngles()</code>''' changes it. | <code>GetAbsAngles()</code> returns the absolute (i.e. [[world]]-relative) [[angles]] of an entity as a [[QAngle]]. Its sister function '''<code>SetAbsAngles()</code>''' changes it. | ||
Similar to <code>GetAbsOrigin()</code>, 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: | Example Usage: | ||
Line 43: | Line 47: | ||
:* <code>char const *decalName</code> is the name of the decal that is to be applied. | :* <code>char const *decalName</code> 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 <code>UTIL_DecalTrace()</code>. | This function is usually not called directly, but through <code>UTIL_DecalTrace()</code>. | ||
Example Usage: | Example Usage: | ||
<source lang=cpp> | <source lang=cpp> | ||
Line 56: | Line 62: | ||
</source> | </source> | ||
== EntityToWorldTransform() == | |||
'''Returns:''' | |||
:* <code>matrix3x4_t &</code> 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: | |||
<source lang=cpp> | |||
matrix3x4_t worldPos = EntityToWorldTransform(); | |||
Msg("This Entity's Position is %f %f %f/n",worldPos[0][3],worldPos[1][3],worldPos[2][3]); | |||
</source> | |||
== ShouldCollide() == | |||
'''Parameters:''' | |||
:* <code>int collisionGroup</code> The collision group we are testing [[this]] entity against. | |||
:* <code>int contentsMask</code> Similar to <code>UTIL_TraceLine</code>'s MASK_ parameter. It represents the MASK_ types to collide with. | |||
'''Returns:''' | |||
:* <code>bool</code> True if [[this]] entity should collide with the given <code>collisionGroup</code> with the given <code>contentsMask</code>. | |||
Example Usage: | |||
<source lang=cpp> | |||
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"); | |||
} | |||
</source> | |||
== SetModel() == | |||
''' Parameters ''' | |||
:* <code>const char *szModelName</code> Name of the Model (You need to add models in your path to your .mdl file) | |||
''' Returns ''' | |||
This function doesnt return anything | |||
Example Usage: | |||
<source lang=cpp> | |||
SetModel("models/player/player.mdl");</source> | |||
== GetTeamNumber() == | |||
''' Parameters ''' This function doesn't have any parameters | |||
''' Returns ''' | |||
:* <code>Returns the Team Number from m_iTeamNum</code> | |||
'''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: | |||
<source lang=cpp> | |||
void CHL2MP_Player::Spawn(void) | |||
{ | |||
if(GetTeamNumber() == TEAM_COMBINE) | |||
{ | |||
GiveAllItems(); // Gives combines all weapons on spawn | |||
} | |||
BaseClass::Spawn(); | |||
} | |||
</source> | |||
== SetHealth() == | |||
''' Parameters ''' | |||
:* <code>int amt</code> Amount to set to an entity health to. This sets m_iHealth to amt. | |||
''' Returns ''' This function doesn't return anything. | |||
Example Usage: | |||
<source lang=cpp> | |||
void CHL2MP_Player::Spawn(void) | |||
{ | |||
SetHealth(200); //Players now have 200 HP on spawn | |||
BaseClass::Spawn(); | |||
} | |||
</source> | |||
== GetHealth() == | |||
''' Parameters ''' This function doesn't have any parameters | |||
''' Returns ''' | |||
:* <code>Returns the current entity's health</code> It return's m_iHealth. | |||
'''NOTE:''' GetHealth returns the health as '''Integer''' not as float. | |||
== SetMaxHealth() == | |||
''' Parameters ''' | |||
:* <code>int amt</code> 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: | |||
<source lang=cpp> | |||
void CHL2MP_Player::Spawn(void) | |||
{ | |||
SetMaxHealth(200); //Players maximum HP is now 200 | |||
BaseClass::Spawn(); | |||
} | |||
</source> | |||
== GetMaxHealth() == | |||
''' Parameters ''' This function doesn't have any parameters. | |||
''' Returns ''' | |||
:* <code>Returns the current entity's max health</code> It return's m_iMaxHealth. | |||
'''NOTE:''' GetMaxHealth returns the health as '''Integer''' not as float. | |||
== ChangeTeam() == | |||
''' Parameters ''' | |||
:* <code>int iTeamNum</code> Team number to set for the entity See [https://developer.valvesoftware.com/w/index.php?title=CBaseEntity_Functions#GetTeamNumber.28.29 GetTeamNumber()] | |||
''' Returns ''' This function doesn't return anything. | |||
Example Usage: | |||
<source lang=cpp> | |||
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 | |||
} | |||
</source> | |||
== CollisionProp() == | |||
''' Returns ''' <code>CCollisionProperty *</code> this entity's collision property. ''Main Article: [[CCollisionProperty]]'' | |||
Example Usage: | |||
<source lang=cpp> | |||
void CAntlionGrub::Spawn( void ) | |||
{ | |||
... | |||
//Make my trigger bounds 1 unit bigger than my collision bounds. | |||
CollisionProp()->UseTriggerBounds(true,1); | |||
... | |||
} | |||
</source> | |||
== SetSolid() == | |||
''Main Article: [[SetSolid()]]'' | |||
''' Parameters ''' | |||
:* <code>SolidType_t</code> 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 [https://developer.valvesoftware.com/wiki/CCollisionProperty this for more...] | |||
Example Usage: | |||
<source lang=cpp> | |||
void CNPC_AntlionGuard::Spawn( void ) | |||
{ | |||
... | |||
SetSolid( SOLID_BBOX ); | |||
... | |||
} | |||
</source> | |||
== GetSolid() == | |||
''' Returns ''' <code>SolidType_t</code>. This is the type of solid this entity is. Look for <code>SolidType_t</code> in const.h to see the actual types. | |||
Example Usage: | |||
<source lang=cpp> | |||
bool IsSolid(CBaseEntity* pEntity) | |||
{ | |||
if (pEntity->GetSolid() == SOLID_NONE) | |||
return false; | |||
return true; | |||
} | |||
</source> | |||
== GetSolidFlags() == | |||
''' Returns ''' <code>SolidFlags_t</code>. This is the bitflags for the solid. Look for <code>SolidFlags_t</code> in const.h to see the actual flags. | |||
Example Usage: | |||
<source lang=cpp> | |||
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); | |||
} | |||
</source> | |||
== SetNavIgnore() == | |||
''' Parameters ''' | |||
:* <code>float duration</code> 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: | |||
<source lang=cpp> | |||
void CPropAirboat::EnterVehicle( CBaseCombatCharacter *pPlayer ) | |||
{ | |||
BaseClass::EnterVehicle( pPlayer ); | |||
//EnablePlayerBlocker( false ); | |||
// NPCs like manhacks should try to hit us | |||
SetNavIgnore(); | |||
... | |||
} | |||
</source> | |||
== edict() == | |||
''' Returns ''' <code>[[edict_t]]*</code>. see [https://developer.valvesoftware.com/wiki/Edict edict_t]... | |||
Returns this entity's edict. | |||
Example Usage: | |||
<source lang=cpp> | |||
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; | |||
} | |||
</source> | |||
[[Category:Programming]] |
Latest revision as of 22:37, 21 January 2024

You can help by

January 2024
CBaseEntity derives from IServerEntity.
As its name suggests, all entity types derive from it.

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
).

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 toUTIL_TraceLine
's MASK_ parameter. It represents the MASK_ types to collide with.
Returns:
bool
True if this entity should collide with the givencollisionGroup
with the givencontentsMask
.
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
int iTeamNum
Team number to set for the entity See GetTeamNumber()
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);
}
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;
}