Owner

From Valve Developer Community
Jump to: navigation, search

The owner of an entity is a nebulous concept, entirely unrelated to parenting. It is generally whatever emitted or is currently in possession of the current entity: if you fire a projectile, pick up a physics object or have a viewmodel, you will be its owner.

Ownership is tested in various situations, the most notable of which is collision detection.

Usage

Any ownership of an entity is typically assigned when calling CBaseEntity::Create(), but can be altered later with SetOwner() too.

GetOwnerEntity() returns the owner as CBaseEntity*. Weapons and viewmodels also provide GetOwner(), which returns CBaseCombatCharacter*.

Collisions with owner

In Valve's code objects do not collide with their owner, which can be frustrating. To enable owner collision without breaking backwards compatibility:

  1. Use Class View (Ctrl+Shift+C) to find SolidFlags_t. Change the end to read:
    FSOLID_COLLIDE_WITH_OWNER   = 0X0400,	// Can hit our m_hOwner
    
    FSOLID_MAX_BITS	= 11
    }
    
  2. Browse to line 514 in CCollisionEvent::ShouldCollide(). Modify the conditionals there to read:
    if ( pEntity0->edict() && pEntity1->edict() )
    {
    	// don't collide with your owner
    	if ( (!pEntity0->IsSolidFlagSet(FSOLID_COLLIDE_WITH_OWNER) && pEntity0->GetOwnerEntity() == pEntity1)
    		|| (!pEntity1->IsSolidFlagSet(FSOLID_COLLIDE_WITH_OWNER) && pEntity1->GetOwnerEntity() == pEntity0) )
    		return 0;
    }
    
  3. To make TraceLines follow our new rules, edit the bottom of PassServerEntityFilter() to read:
    // don't clip against own missiles
    if ( !pEntTouch->IsSolidFlagSet(FSOLID_COLLIDE_WITH_OWNER) && pEntTouch->GetOwnerEntity() == pEntPass )
    	return false;
    
    // don't clip against owner
    if ( !pEntPass->IsSolidFlagSet(FSOLID_COLLIDE_WITH_OWNER) && pEntPass->GetOwnerEntity() == pEntTouch )
    	return false;
    
  4. Then, in your entity, call:
    AddSolidFlags( FSOLID_COLLIDE_WITH_OWNER );