Garry's Mod Wiki

Revision Difference

NPC:AddEntityRelationship#510674

<function name="AddEntityRelationship" parent="NPC" type="classfunc">⤶ <description>⤶ Makes the NPC like, hate, feel neutral towards, or fear the entity in question. If you want to setup relationship towards a certain entity `class`, use <page>NPC:AddRelationship</page>.⤶ ⤶ <note>NPCs do not see <page>NextBot</page>s by default. This can be fixed by adding the [FL_OBJECT](/gmod/Enums/FL) flag to the NextBot.</note>⤶ </description>⤶ <realm>Server</realm>⤶ <args>⤶ <arg name="target" type="Entity">The entity for the relationship to be applied to.</arg>⤶ <arg name="disposition" type="number">A &lt;page&gt;D&lt;/page&gt; representing the relationship type.</arg>⤶ <arg name="priority" type="number">How strong the relationship is.</arg>⤶ </args>⤶ </function>⤶ ⤶ <example>⤶ <description>Spawns a manhack and makes it fear player 1.</description>⤶ <code>⤶ local hack = ents.Create( "npc_manhack" )⤶ hack:Spawn()⤶ hack:AddEntityRelationship( player.GetByID(1), D_FR, 99 )⤶ </code>⤶ ⤶ </example>⤶ ⤶ ⤶ <example>⤶ <description>Find wanted NPC class name and make them hate the entity.</description>⤶ <code>⤶ function ENT:Think()⤶ local enemy = ents.FindByClass("npc_*") --Find any spawned entity in map with class beginning at npc⤶ for _, x in pairs(enemy) do --for every found entity do⤶ if !x:IsNPC() then return end -- if found entity is not NPC then do nothing ⤶ if x:GetClass() != self:GetClass() then -- if found entity is not self entity then continue⤶ x:AddEntityRelationship( self, D_HT, 99 ) -- found entity will hate self entity⤶ self:AddEntityRelationship( x, D_HT, 99 ) -- self entity will hate found entity ⤶ end⤶ end ⤶ end⤶ </code>⤶ ⤶ </example>