Agents
TaleWorlds.MountAndBlade.Agent Class Reference
Agent - moving entity in the Mission: NPC, Player, horse, etc.
- Player agent: Agent.Main
- All Mission agents: Mission.Current.Agents
- Active enemy agents: Mission.Current.PlayerEnemyTeam.ActiveAgents
hunharibo:
Agent and Character(Object) both exist and mean different things.
Agent is a "pawn" that moves in a mission (Mission = any scene that is different from the world map, when you walk around town in civilian mode, field battle, siege, arenas, bandit hideouts etc.)
Whereas character is an object container describing an NPC (civilian or troop) and their characteristics and equipment. Agents get spawned partially based on data from a character.
Agent's Hero
if (agent.IsHuman && agent.Character != null && agent.Character.IsHero) {
Hero? hero = (agent.Character as CharacterObject)?.HeroObject;
}
Public methods
.IsMainAgent
.IsPlayerTroop
.IsHero
.IsActive()
.IsRetreating
.IsFadingOut
.IsSliding
.IsSitting
.IsReleasingChainAttack
.IsOnLand
.IsEnemyOf
.IsFriendOf
.IsHuman
.IsAIControlled
.IsPlayerControlled
.IsRunningAway
.IsFemale
.HasWeapon
.HasMount
.IsMount
.RiderAgent // if agent is a horse - gives the rider
.GetSteppedEntity // returns the game entity agent is standing on
.IsDoingPassiveAttack // whether the agent is in a couched lance or braced spear state
.SpawnEquipment - the equipment the agent spawned with
.Equipment - agent's current equipment
AgentState
agent.State
Various actions
DropItem(EquipmentIndex itemIndex, WeaponClass pickedUpItemType = WeaponClass.Undefined)
EquipItemsFromSpawnEquipment(bool neededBatchedItems)
EquipWeaponToExtraSlotAndWield(ref MissionWeapon weapon)
RemoveEquippedWeapon(EquipmentIndex slotIndex)
Die(Blow b, Agent.KillInfo overrideKillInfo = Agent.KillInfo.Invalid)
MakeDead(bool isKilled, ActionIndexValueCache actionIndex)
Mount(Agent mountAgent)
SetCrouchMode(bool set)
SetAlwaysAttackInMelee(bool attack)
bool CheckSkillForMounting(Agent mountAgent) - can mount that mountAgent?
agent.SetMaximumSpeedLimit - change speed
Dropping a weapon
DropItem calls MBAPI.IMBAgent.DropItem, except when the weapon has both WeaponFlags.AffectsArea and WeaponFlags.Burning. That pair calls Mission.OnAgentShootMissile and RemoveEquippedWeapon instead. The native path comes back through Mission.SpawnWeaponAsDropFromAgent, which reads that agent's skeleton and the weapon entity in the slot. Agent.OnWeaponDrop clears the slot, and Mission.OnItemDrop fires.
Skip an empty slot. MissionWeapon.IsEmpty is CurrentUsageItem == null, and DropItem reads CurrentUsageItem.WeaponFlags with no null check.
DropItem from OnAgentRemoved or OnAgentPanicked
On 1.5.x that call is reported as an AccessViolationException. There is often no crash report. The fault shows up on the mission tick. The same call from those callbacks did not crash this way on 1.4.8.
Mission.OnAgentRemoved sets agent.State before any behavior runs, so IsActive() is already false (IsActive is State == AgentState.Active). It calls Team.DeactivateAgent, which only removes the agent from Team.ActiveAgents, and then calls OnEarlyAgentRemoved and OnAgentRemoved. The behavior method is that second loop. Mission.Agents drops the agent, and Agent.OnRemove() runs, only after every behavior returns. The only DropItem in that method is later still: a banner in EquipmentIndex.ExtraWeaponSlot, and only when that slot is the wielded offhand. It is skipped for AgentState.Routed, on a client or replay, and when the agent cannot wield. That call belongs to the engine. A behavior copies the weapon instead.
Mission.OnAgentPanicked only notifies behaviors. MissionAgentPanicHandler stores the agent and, on the next OnPreMissionTick, calls CommonAIComponent.Retreat() and Mission.OnAgentFleeing. It does not drop a weapon in the panic callback.
Spawn a copy
MissionWeapon is a struct. Copy the slot and spawn a new entity. SpawnWeaponWithNewEntity does not call IMBAgent.DropItem, OnWeaponDrop, or Mission.OnItemDrop, and it leaves the equipment slot filled, so the agent mesh can still show the weapon. The public method passes hasLifeTime: false, so the copy is not removed after 5 seconds (ItemFlags.QuickFadeOut) or 180 seconds. SpawnWeaponAsDropFromAgent passes hasLifeTime: true, and that timer removes a non-banner. A banner is initialized with lifetime off on both paths. WeaponSpawnFlags.WithPhysics is the flag WeaponSpawner uses for a pickup, and it starts the 10 second physics timer on SpawnedItemEntity.
LookFrame still reads IMBAgent.GetLookDirection. Read it in the callback, while this method is running. OnRemove has not run yet.
public override void OnAgentRemoved(Agent affectedAgent, Agent affectorAgent, AgentState agentState, KillingBlow blow)
{
for (EquipmentIndex slot = EquipmentIndex.WeaponItemBeginSlot; slot < EquipmentIndex.NumAllWeaponSlots; slot++)
{
MissionWeapon weapon = affectedAgent.Equipment[slot];
if (weapon.IsEmpty)
{
continue;
}
MatrixFrame frame = affectedAgent.LookFrame;
Mission.Current.SpawnWeaponWithNewEntity(ref weapon, Mission.WeaponSpawnFlags.WithPhysics, frame);
}
}
Weapon slots are Weapon0 through ExtraWeaponSlot. NumAllWeaponSlots is the end, not a slot. Every slot in that loop uses the same LookFrame, so the copies start on top of each other.
Drop from a living agent
When the agent is still in the mission and has to lose the mesh, record the agent and the slot in the callback. Call DropItem from OnMissionTick while IsActive() is true and the slot is not empty. MissionAgentPanicHandler has the same shape: record in OnAgentPanicked, act on the next tick. An agent who dies before that tick is no longer active, so this path skips them. Use the copy above for a death drop.
private readonly List<Agent> _dropAgents = new List<Agent>();
private readonly List<EquipmentIndex> _dropSlots = new List<EquipmentIndex>();
public override void OnAgentPanicked(Agent affectedAgent)
{
for (EquipmentIndex slot = EquipmentIndex.WeaponItemBeginSlot; slot < EquipmentIndex.NumAllWeaponSlots; slot++)
{
if (affectedAgent.Equipment[slot].IsEmpty)
{
continue;
}
_dropAgents.Add(affectedAgent);
_dropSlots.Add(slot);
}
}
public override void OnMissionTick(float dt)
{
for (int i = _dropAgents.Count - 1; i >= 0; i--)
{
Agent agent = _dropAgents[i];
EquipmentIndex slot = _dropSlots[i];
_dropAgents.RemoveAt(i);
_dropSlots.RemoveAt(i);
if (!agent.IsActive() || agent.Equipment[slot].IsEmpty)
{
continue;
}
agent.DropItem(slot);
}
}
Kill Agent
Blow blow = new Blow(agent.Index);
blow.WeaponRecord.FillAsMeleeBlow(null, null, -1, 0);
blow.DamageType = DamageTypes.Blunt;
blow.BaseMagnitude = 10000f;
blow.WeaponRecord.WeaponClass = WeaponClass.Undefined;
blow.GlobalPosition = agent.Position;
blow.DamagedPercentage = 1f;
if (agent.IsActive()) agent.Die(blow, Agent.KillInfo.Invalid);
Animations
Use SetActionChannel
MissionEquipment
public MissionEquipment Equipment { get; private set; }
Agent.DropItem(EquipmentIndex itemIndex, WeaponClass pickedUpItemType = WeaponClass.Undefined)
EquipItemsFromSpawnEquipment(bool neededBatchedItems)
EquipWeaponToExtraSlotAndWield(ref MissionWeapon weapon)
RemoveEquippedWeapon(EquipmentIndex slotIndex)
Position
agent.Position - position on the map in Vec3
agent.Position.AsVec2 - position on the map in Vec2
Target
agent.GetTargetAgent()
agent.GetTargetPosition()
Various
How to get the position of a bone on an agent skeleton?
boneFrame = TargetAgent.AgentVisuals.GetGlobalFrame().TransformToParent(TargetAgent.AgentVisuals.GetBoneEntitialFrame(i, false));
Weapons
What weapon agent is wielding?
Get nearby enemy agents
MBList<Agent> nearbyEnemyAgents = Mission.GetNearbyEnemyAgents(Agent.Main.Position.AsVec2, 20f, Mission.PlayerTeam, new MBList<Agent>());
Outline agent
uint focusedContourColor = new TaleWorlds.Library.Color(1f, 0.84f, 0.35f, 1f).ToUnsignedInteger();
agent.AgentVisuals?.SetContourColor(focusedContourColor, true);
agent.AgentVisuals?.SetContourColor(null); to remove it
Looks like this:

BoneBodyPartType
public enum BoneBodyPartType : sbyte
{
None = -1,
Head,
Neck,
Chest,
Abdomen,
ShoulderLeft,
ShoulderRight,
ArmLeft,
ArmRight,
Legs,
NumOfBodyPartTypes,
CriticalBodyPartsBegin = 0,
CriticalBodyPartsEnd = 6
}