Project 03: Bodyguard
In Project 02, you spawned a static pedestrian. In Project 03, you transform a spawned pedestrian into an intelligent, active ally: an individual bodyguard who walks alongside you, detects when you are under attack, targets the specific hostile threat, and returns to your side once the danger has passed.
The mission
Build an individual bodyguard script satisfying six core operational criteria:
- Spawn an armed companion pedestrian who walks in an offset follow posture beside the player.
- Establish a mutual companion relationship group to prevent friendly fire and enforce faction loyalty.
- Detect when the player is engaged in combat and direct the bodyguard specifically at a known aggressor.
- Allow native combat AI to manage tactical shooting without interrupting or resetting tasks every frame.
- Automatically resume peaceful offset following once the hostile threat is eliminated.
- Cleanly delete both bodyguard and test actors when reloading the script.
Specifications, constraints
- Individual companion focus: Keep the architecture focused on a single bodyguard rather than multi-npc squad formations.
- Explicit target requirement: Recognize that
player.IsInCombatis a binary boolean that provides no entity handle; you must supply a concrete target entity toTask.Combat. - Task priority hierarchy: Organize
OnTickso that ongoing combat, new engagement, and peaceful following never conflict or overwrite each other on the same frame. - Zero orphan entities: Guarantee that all spawned actors are checked and deleted inside
OnAborted.
Implementation steps
- Declare entity handles: Define private fields
private Ped _guard;andprivate Ped _testAggressor;. - Configure diplomatic relations: In the constructor, create a companion relationship group (
COMPANION_GUARD) and establish mutualRelationship.Companionwith the player's group. - Implement bodyguard spawning: In a dedicated helper, stream
PedHash.Security01SMM, spawn the ped 2 meters to the right, equip them with a weapon, assignBlockPermanentEvents = true, and associate the companion relationship group. - Build the decision hierarchy in
OnTick:- If the player is invalid, abort early.
- If the guard is missing or dead, trigger spawning.
- If the guard is already fighting (
_guard.IsInCombat), return immediately to give the game's tactical AI full control. - If both the player and the aggressor are engaged in combat, issue
_guard.Task.Combat(_testAggressor, ...). - Otherwise, maintain peaceful movement via
_guard.Task.FollowToOffsetFromEntity(player, ...).
- Enforce clean teardown: In
OnAborted, verify that both_guardand_testAggressorexist, and delete them cleanly.
APIs, tools to explore
GTA.RelationshipGroup: Struct representing faction loyalty and diplomatic rules between entities.RelationshipGroup.SetRelationshipBetweenGroups(RelationshipGroup other, Relationship relation, bool bidirectional): Configures mutual alignment between two groups.Ped.Task.FollowToOffsetFromEntity(Entity target, Vector3 offset, float speed, int timeout, float stoppingRange, bool persist): Commands an entity to match the movement of a target while maintaining a local coordinate offset.Ped.Task.Combat(Ped target, TaskCombatFlags combatFlags, TaskThreatResponseFlags threatResponseFlags): Commands an entity to engage in an armed or unarmed assault against an explicit target.Ped.IsInCombat: Native engine boolean indicating whether a pedestrian is actively fighting.Ped.BlockPermanentEvents: Disables default ambient panic, stopping NPCs from fleeing gunshots so your script retains full behavioral control.
Validation checklist
Your mod is validated when:
- The bodyguard spawns reliably and walks in formation on your left flank as you navigate through Los Santos.
- When an aggressor attacks the player, the bodyguard draws their weapon and engages that specific target.
- During the firefight, the bodyguard uses native tactical cover and shooting without stuttering or task resetting.
- Once the hostile target dies, the bodyguard holsters their weapon and returns to follow formation.
- Reloading the script with
Insertcleanly deletes both bodyguard and aggressor.
Solution, explanations
Partner
Verified solution and code explanations
The mission, specifications, and guided steps remain 100% free and open for everyone. The complete verified reference code and production explanations are reserved for Partner members.