Simple AI companion spawn mod tutorial
As a prerequisite, you're required to have console enabler installed (unless you wish to bind the function to button).
This tutorial mod will spawn an AI companion which will follow us around, attack our enemies and optionally appear on HUD as a companion with his health bar.
Since we want this function to be executable from console, best place for it is in scripts\game\temp.ws
Inside we're going to add a new function:
However there may be cases where we actually want to remove a companion, here's a function which will deal with that:
-Companion can only walk, literally walk. No running, no sprinting. By the looks of it "CAIFollowSideBySideAction" should work as "variable speed" following mode where NPC adjusts to our speed, however it's either broken or i can't use it properly Actually i think this functionality may be broken, because even official debug commands like MoveToPlayer do nothing.
-Certain NPCs can't die, even if killed with immortality override, some NPCs (like civilians or Yennefer) only get knocked out and will keep following you once they get back up, for now i see no way around this.
Enjoy!
//
P.S: You can get extended implementation of this script here
As a prerequisite, you're required to have console enabler installed (unless you wish to bind the function to button).
This tutorial mod will spawn an AI companion which will follow us around, attack our enemies and optionally appear on HUD as a companion with his health bar.
Since we want this function to be executable from console, best place for it is in scripts\game\temp.ws
Inside we're going to add a new function:
Usage is simple, open console and call SimpleComp(), this will spawn your companion without the HUD module as will SimpleComp(0). Using SimpleComp(1) will spawn companion with the companion module.exec function SimpleComp(optional wanthud : bool)
{
var hud : CR4ScriptedHud; //For the companion module
var template : CEntityTemplate; //For spawning functionality
var pos : Vector; //For spawning functionality
var rot : EulerAngles; //For spawning functionality
var ent : CEntity; //For spawning functionality
var l_aiTree : CAIFollowSideBySideAction; //For follower behavior
var ModuleComp : CR4HudModuleCompanion; //For the companion module
ModuleComp = (CR4HudModuleCompanion)hud.GetHudModule( "CompanionModule" ); //Find companion module
pos = thePlayer.GetWorldPosition() + VecRingRand(1.f,2.f); // Determine position for spawning the companion
rot = VecToRotation(thePlayer.GetWorldPosition() - pos); // Determine spawn rotation
template = (CEntityTemplate)LoadResource("sword1h_super_hard"); // Determine what we spawn, replace with any NPC template you wish
ent = theGame.CreateEntity(template, pos, rot ); // This directly handles the spawning
l_aiTree = new CAIFollowSideBySideAction in ent; // Initialize follower behavior
l_aiTree.OnCreated(); // Once we're done initializing behavior tree
((CActor)ent).ForceAIBehavior( l_aiTree, BTAP_AboveEmergency2); // Force the follower behavior tree onto the spawned NPC
((CActor)ent).SetTemporaryAttitudeGroup( 'player', AGP_Default ); // Put the NPC into same attitude group as player to share friendlies/enemies
((CActor)ent).SetAttitude( thePlayer, AIA_Friendly ); // Just to make sure it's always friendly to us
ent.AddTag( 'InPlayerParty' ); // add companion tag
if(!wanthud)
{
return; // Do nothing
}
else
{
hud = (CR4ScriptedHud)theGame.GetHud();
ModuleComp = (CR4HudModuleCompanion)hud.GetHudModule( "CompanionModule" );
ModuleComp.ShowCompanion(true, 'InPlayerParty');module
}
}
However there may be cases where we actually want to remove a companion, here's a function which will deal with that:
Limitations:{
var npcname : CNewNPC; // Declare NPC we're looking for
var hud : CR4ScriptedHud; // Declare HUD to find companion module
var ModuleComp : CR4HudModuleCompanion; // Declare companion module for direct access
npcname = theGame.GetNPCByTag( 'InPlayerParty' ); // Find our companion
npcname.RemoveTag( 'InPlayerParty' ); // Remove tag
npcname.Kill(1); // Force kill, 1 in bracket ignores immortality
hud = (CR4ScriptedHud)theGame.GetHud(); // find our HUD
ModuleComp = (CR4HudModuleCompanion)hud.GetHudModule( "CompanionModule" ); // find our module
ModuleComp.ShowElement(false, false); // No companion at the moment, kill the module
ModuleComp.SetEnabled(false); // same as above
hud.UpdateHudConfig('CompanionModule', true); // force hud refresh
}
-Companion can only walk, literally walk. No running, no sprinting. By the looks of it "CAIFollowSideBySideAction" should work as "variable speed" following mode where NPC adjusts to our speed, however it's either broken or i can't use it properly Actually i think this functionality may be broken, because even official debug commands like MoveToPlayer do nothing.
-Certain NPCs can't die, even if killed with immortality override, some NPCs (like civilians or Yennefer) only get knocked out and will keep following you once they get back up, for now i see no way around this.
Enjoy!
//
P.S: You can get extended implementation of this script here
Last edited by a moderator:


