Squad drill
In Project 03, you built an individual bodyguard using manual offset following (FollowToOffsetFromEntity). That is the ideal solution for one companion because you control their exact position and stance.
When you want an entire squad of three or four bodyguards moving together in tactical formation, managing multiple manual offsets becomes cumbersome. GTA V includes a dedicated native squad engine: PedGroup.
This exercise is an optional deepening. It is self-contained, not a mandatory prerequisite for the rest of the course.
The six steps of a PedGroup
Managing a squad in ScriptHookVDotNet always follows these six steps:
- Obtain the leader's group: Access
player.PedGroup. - Spawn the follower: Create an allied ped and set
IsPersistent = true. - Lock membership: Set
ped.NeverLeavesGroup = trueso the AI does not abandon the player during chaos. - Add to group: Call
group.Add(ped, false)(falsemeans not as leader). - Configure formation: Choose a
Formationenum value (e.g.FollowInLineorDefault). - Set spacing: Call
group.SetFormationSpacing(2.5f).
The verified squad implementation
Here is the complete, self-contained implementation matching snippet S01:
using System;
using GTA;
public sealed class GroupFollowFormation : Script
{
private Ped _guard;
public GroupFollowFormation()
{
Tick += OnTick;
Aborted += OnAborted;
}
private void OnTick(object sender, EventArgs e)
{
Ped player = Game.Player.Character;
if (player == null || !player.Exists())
{
return;
}
if (_guard != null && _guard.Exists())
{
return;
}
Model model = new Model(PedHash.Security01SMM);
if (!model.IsValid || !model.IsInCdImage || !model.Request(2000))
{
model.MarkAsNoLongerNeeded();
return;
}
try
{
_guard = World.CreatePed(model, player.Position + player.RightVector * 2.5f, player.Heading);
if (_guard == null || !_guard.Exists())
{
return;
}
_guard.IsPersistent = true;
_guard.NeverLeavesGroup = true;
// Step 1 & 4: Add to player's squad group
PedGroup group = player.PedGroup;
group.Add(_guard, false);
// Step 5 & 6: Set tactical formation and spacing
group.Formation = Formation.FollowInLine;
group.SetFormationSpacing(2.5f);
}
finally
{
model.MarkAsNoLongerNeeded();
}
}
private void OnAborted(object sender, EventArgs e)
{
if (_guard != null && _guard.Exists())
{
_guard.Delete();
}
}
}
Formations available
The GTA.Formation enum provides four distinct squad shapes:
Default: Standard conversational following around the leader.FollowInLine: Single file line marching directly behind the leader.CircleAroundLeader: 360-degree perimeter protection.Pair: Paired side-by-side walk.
When you add two or three guards using this pattern, the native engine automatically assigns each guard their corresponding slot in the formation without any manual math.
In the next course lesson, we turn from pedestrians to vehicles: discovering road nodes and proper pavement placement.