Forums
Games
Cyberpunk 2077 Thronebreaker: The Witcher Tales GWENT®: The Witcher Card Game The Witcher 3: Wild Hunt The Witcher 2: Assassins of Kings The Witcher The Witcher Adventure Game
Jobs Store Support Log in Register
Forums - CD PROJEKT RED
Menu
Forums - CD PROJEKT RED
  • Hot Topics
  • NEWS
  • GENERAL
    THE WITCHER ADVENTURE GAME
  • STORY
    THE WITCHER THE WITCHER 2 THE WITCHER 3 THE WITCHER TALES
  • GAMEPLAY
    THE WITCHER THE WITCHER 2 THE WITCHER 3 MODS (THE WITCHER) MODS (THE WITCHER 2) MODS (THE WITCHER 3)
  • TECHNICAL
    THE WITCHER THE WITCHER 2 (PC) THE WITCHER 2 (XBOX) THE WITCHER 3 (PC) THE WITCHER 3 (PLAYSTATION) THE WITCHER 3 (XBOX) THE WITCHER 3 (SWITCH)
  • COMMUNITY
    FAN ART (THE WITCHER UNIVERSE) FAN ART (CYBERPUNK UNIVERSE) OTHER GAMES
  • RED Tracker
    The Witcher Series Cyberpunk GWENT
THE WITCHER
THE WITCHER 2
THE WITCHER 3
MODS (THE WITCHER)
MODS (THE WITCHER 2)
MODS (THE WITCHER 3)
Menu

Register

Forums - CD PROJEKT RED

+
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:
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
}
}
Click to expand...
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.

However there may be cases where we actually want to remove a companion, here's a function which will deal with that:
{
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
}
Click to expand...
Limitations:
-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
  • English
    English Polski (Polish) Deutsch (German) Русский (Russian) Français (French) Português brasileiro (Brazilian Portuguese) Italiano (Italian) 日本語 (Japanese) Español (Spanish)

STAY CONNECTED

Facebook Twitter YouTube
CDProjekt RED Mature 17+
  • Contact administration
  • User agreement
  • Privacy policy
  • Cookie policy
  • Press Center
© 2018 CD PROJEKT S.A. ALL RIGHTS RESERVED

The Witcher® is a trademark of CD PROJEKT S. A. The Witcher game © CD PROJEKT S. A. All rights reserved. The Witcher game is based on the prose of Andrzej Sapkowski. All other copyrights and trademarks are the property of their respective owners.

Forum software by XenForo® © 2010-2020 XenForo Ltd.