This article's documentation is for anything that uses the Source engine. Click here for more information.


From Valve Developer Community
Jump to: navigation, search
Ai relationship.png
TypePoint entity
EngineSource Source
AvailabilityAll games except:
Half-Life: Source Half-Life: Source
Left 4 Dead series Left 4 Dead series
Day of Defeat: Source Counter-Strike: Global Offensive Counter-Strike: Source

ai_relationship is a point entity available in all Source Source games.(not in Half-Life: Source Half-Life Deathmatch: Source Counter-Strike: Source Day of Defeat: Source Left 4 Dead series Counter-Strike: Global Offensive)

It sets a relationship type between two parties. These parties can be an NPC, an NPC class, or the player(s). Any object descending from CBaseCombatCharacter can have relationships, most notably NPCs and players.

Note.pngNote:While ai_relationship does not exist in Half-Life: Source Half-Life: Source, the SetRelationship input does work.


Name Description
Hate (D_HT) Considers the target an enemy. Most NPCs will attack if possible, although NPCs that cannot attack may flee instead.
Fear (D_FR) Tries to avoid/flee from the target. Behavior varies across NPCs. Most NPCs will just run to cover out of the target's sight while NPCs with ranged weapons may run away while shooting at the feared enemy.
Icon-Bug.pngBug:The trace code used by melee damage doesn't detect D_FR entities and therefore NPCs can't use melee attacks against them. (they may try, but they won't do any damage)
Code Fix: This can be fixed by going to CTraceFilterMelee::ShouldHitEntity() and replacing pBCC->IRelationType( pEntity ) == D_HT with pBCC->IRelationType( pEntity ) <= D_FR, which correctly accounts for both D_HT and D_FR.
Like (D_LI) Considers the target a friend. NPCs will never intentionally attack these targets and NPCs with bits_CAP_FRIENDLY_DMG_IMMUNE (e.g. citizens) will not take any damage from NPCs or players that have this disposition towards them.
Note.pngNote:Only the disposition of the NPC that deals the damage is considered by bits_CAP_FRIENDLY_DMG_IMMUNE. If a citizen friendly to the player (citizen D_LI to the player) is attacked by a player that doesn't consider them friendly (player not D_LI to the citizen), they will take damage and may not retaliate without additional programming.
Code: bits_CAP_FRIENDLY_DMG_IMMUNE can be added to a NPC with AddCapabilities and is usually appended in an NPC's Spawn() function, although it can be toggled at any time.
Neutral (D_NU) Considers a target neutral. NPCs usually won't consider these targets at all, or at least not care about them as enemies or as allies. This is used for fauna like crows.
Tip.pngTip:Making a npc_citizen neutral to the player (but not the other way around) is used to prevent them from joining the player's squad.
Error (D_ER) This "error" disposition is used internally to determine if something went wrong in a relationship. Should not be used. NPC behavior mostly equivalent to neutral.[confirm]

In code, it is represented by theCAI_Relationshipclass, defined in theai_relationship.cppfile.


  • 1: Notify subject of target's location
  • 2: Notify target of subject's location


Subject(s) (subject) (???) <target_name_or_class>
The targetname or classname of the NPCs whose disposition will change.
Subject Class (subjectclass) (???) <classname> (in all games since Alien Swarm)
As above, for classnames only.
Target(s) (target) (???) <target_name_or_class>
The targetname or class name of the NPCs about whom the subject(s) will change their disposition towards.
Tip.pngTip:Use !player for the player.
Target Class (targetclass) (???) <classname> (in all games since Alien Swarm)
As above, for classnames only.
Disposition (disposition) (???) <choices>
The way the subject(s) should feel about the target(s)
  1. Hate
  2. Fear
  3. Like
  4. Neutral
Radius for subject (radius) (???) <float>
Only NPCs within this distance of the entity will have their dispositions changed.
Disposition Priority (rank) (???) <integer>
How much the subject(s) should Like/Hate/Fear the target(s). Higher priority = stronger feeling.
Start Active (StartActive) (???) <boolean>
Apply this relationship as soon as the entity spawns?
Reciprocal (Reciprocal) (???) <boolean>
Make the new disposition apply to the targets as well as the subjects.

Name (targetname) <string>
The name that other entities use to refer to this entity.
Parent (parentname) <targetname>
Maintain the same initial offset to this entity. An attachment point can also be used if separated by a comma at the end. (parentname [targetname],[attachment])
Tip.pngTip:Entities transition to the next map with their parents
Tip.pngTip:phys_constraint can be used as a workaround if parenting fails.
Origin (X Y Z) (origin) <coordinates>
The position of this entity's center in the world. Rotating entities typically rotate around their origin.
Note.pngNote:Hammer does not move the entities accordingly only in the editor.
Pitch Yaw Roll (X Y Z) (angles) <angle>
This entity's orientation in the world. Pitch is rotation around the Y axis, yaw is the rotation around the Z axis, roll is the rotation around the X axis.
Note.pngNote:This works on brush entities, although Hammer doesn't show the new angles.
Classname (classname) <string> !FGD
Determines the characteristics of the entity before it spawns.
Tip.pngTip:Changing this on runtime still has use, like making matching an entry in S_PreserveEnts will persist the entity on new rounds!
Spawnflags (spawnflags) <flags> !FGD
Toggles exclusive features of an entity, its specific number is determined by the combination of flags added.
Effects (effects) <flags> !FGD
Combination of effect flags to use.
Entity Scripts (vscripts) <scriptlist> (in all games since Left 4 Dead 2) (also in Team Fortress 2)
Space delimited list of VScript files (without file extension) that are executed after all entities have spawned. The scripts are all executed in the same script scope, later ones overwriting any identical variables and functions. Scripts executed on the worldspawn entity will be placed in root scope.
Think function (thinkfunction) <string> (in all games since Left 4 Dead 2) (also in Team Fortress 2)
Name of the function within this entity's script that'll be called automatically every 100 milliseconds, or a user-defined interval if the function returns a number. Avoid expensive operations in this function, as it may cause performance problems.
Lag Compensation (LagCompensate) <boolean> (in all games since Left 4 Dead 2) !FGD
Set to Yes to lag compensate this entity. Should be used very sparingly!
Is Automatic-Aim Target (is_autoaim_target) <boolean> (in all games since Counter-Strike: Global Offensive) !FGD
If set to 1, this entity will slow down aiming movement for consoles and joystick controllers when the entity is under the crosshairs.


Applies relationship changes. This will change all subject entities' relationships to all target entities.
Note.pngNote:Once you fire ApplyRelationship, the entity is on until you send a RevertRelationship or RevertToDefaultRelationship input. During the time any entities that spawn who match the subject or target names will be affected.
Warning.pngWarning:Unpredictable results may occur when two ai_relationship entities refer to the same set or subset of target or subject entities. This situation should be avoided.
Reverts relationship changes to what the relationship of the first one was at the time the ApplyRelationship input was called (or when this ai_relationship was spawned if Start Active is set).
Reverts relationship changes to the entities' default relationships, completely returning control of affected relationships to the code.

AddContext <string>
Adds to the entity's list of response contexts. See Context.
AddOutput <string>
Assigns a new keyvalue/output on this entity. For keyvalues, some rely on extra necessary code to be ran and won't work if its simply just changed through this input. There is a strict format that must be followed:
// Format of changing KeyValues: "AddOutput [key] [value]"
//// Raw text:
"OnUser1" "!self,AddOutput,targetname new_name"

// Format of adding an Output: "AddOutput {targetname}:{inputname}:{parameter}:{delay}:{max times to fire, -1 means infinite}"
//// Raw text:
"OnUser1" "!self,AddOutput,OnUser1:SetParent:!activator:0.0:-1"
// Arguments can be left blank, but the empty blank should still be contained.
//// Raw text:
"OnUser1" "!self,AddOutput,OnUser1:ClearParent::0.0:-1"
Removes all contexts from this entity's list.
Removes this entity from the the movement hierarchy, leaving it free to move independently.
FireUser1 to FireUser4
Fires the respectiveOnUseroutputs; see User Inputs and Outputs.
Removes this entity and any entities parented to it from the world.
Functions the same as Kill, although this entity and any entities parented to it are killed on the same frame, being marginally faster thanKillinput.
RemoveContext <string>
Remove a context from this entity's list. The name should match the key of an existing context.
SetParent <string>
Move with this entity. See Entity Hierarchy (parenting).
SetParentAttachment <string>
Change this entity to attach to a specific attachment point on its parent. The entity will teleport so that the position of its root bone matches that of the attachment. Entities must be parented before being sent this input.
SetParentAttachmentMaintainOffset <string>
As above, but without teleporting. The entity retains its position relative to the attachment at the time of the input being received.
Use  !FGD
Same as a player invoking +use; no effect in most cases.
DispatchResponse <string> !FGD
Dispatches a response to the entity. See Response and Concept.
DispatchEffect <string> (removed since Left 4 Dead) !FGD
Dispatches a special effect from the entity's origin; See also List of Client Effects. Replaced by the particle system since Left 4 Dead.
RunScriptFile <script> (in all games since Left 4 Dead 2) (also in Team Fortress 2)
Execute a VScript file from disk, without file extension. The script contents are merged with the script scope of the receiving entity.
RunScriptCode <string> (in all games since Left 4 Dead 2) (also in Team Fortress 2)
Execute a string of VScript source code in the scope of the entity receiving the input. String quotation may be needed when fired via console.
Icon-Bug.pngBug:In Hammer, using string arguments will corrupt the VMF file's structure, making the file unviewable for the next Hammer session.
Note.pngFix:Remove the string argument manually with a text editor.
Note.pngNote:Team Fortress 2 Backtick characters ` are replaced with quotation marks at runtime, allowing quotation marks to be used when normally not possible.
CallScriptFunction <string> (in all games since Left 4 Dead 2) (also in Team Fortress 2) !FGD
Calls a VScript function defined in the scope of the receiving entity.
TerminateScriptScope  (only in Team Fortress 2) !FGD
Destroys the script scope of the receving entity.
SetLocalOrigin <coordinates> (in all games since Alien Swarm) !FGD
Send this entity to a spot in the map. If the entity is parented to something, it will be offset from the parent by this amount.
SetLocalAngles <angles> (in all games since Alien Swarm) !FGD
Set this entity's angles.



OnUser1 to OnUser4
These outputs each fire in response to the firing of the like-numbered FireUser1 to FireUser4 Input; see User Inputs and Outputs.

See also