Jump to content

Addding AI packages to NPCs via script?


Deleted127869753User

Recommended Posts

I have to admit I'm confused by a lot of the documentation on AI packages and related script functions. Basically, I just want to run a one-time script on startup that adds AI packages to a variety of NPCs. This is just to ensure compatibility with mods that also make revisions to the affected NPCs.

 

This is such a simple question that I'm sure it must have been answered somewhere, so I apologize if I'm being a little (or massively) obtuse. Is it possible/effective to do this? And if so, what function would I use?

Link to comment
Share on other sites

You'd be far, far better off using an xEdit script. I made this on the fly for you, so it may need a bit of fixing. Hopefully the comments I made are enough to get you started.

unit userscript;

const
	strPatchFileName: 'My patch plugin.esp'

var
	tstrlistNPCsToEdit, tstrlistPackagesToAdd: TStringList;
	filePatch: IInterface;


function Initialize: Integer;
begin

	tstrlistPackagesToAdd := TStringList.Create;
	tstrlistNPCsToEdit := TStringList.Create;
	
	//Load the text file containing the Full FormID's of the AI packages to add
	//These are obtained from the 'Record Header' entry in the record's header
	//E.g: GetElementEditValues(ePackageRecord, 'Record Header\FormID');
	//Will return a string like: 'DefaultMasterPackage [PACK:0003EAB9]'
	//MAKE SURE THEY'RE ADDED IN DESCENDING ORDER, ACCORDING TO THEIR CONDITIONS
	tstrlistPackagesToAdd.LoadFromFile(ScriptsPath + 'MyFolder\TextFileWithMyPackageIDs.txt');
	
	//Load the text file containing the Full FormID's of the NPCs to be edited
	tstrlistNPCsToEdit.LoadFromFile(ScriptsPath + 'MyFolder\TextFileWithNPCIDs.txt');
	
	//Create our patch file
	filePatch := AddNewFileName(strPatchFileName, false);
	
	//Duplicate this line as many times as necessary, so all the required masters are added:
	AddMasterIfMissing(filePatch, 'Skyrim.esm');
	AddMasterIfMissing(filePatch, 'Example_MyModData.esp');
	
end;


function Process(e: IInterface): integer;
var
	eNPC, ePackages, ePackageAssigned: IInterface;
	iCounter: integer;
begin

	//Check if the currently selected record is an NPC
	if Signature(e) <> 'NPC_' then
		exit;
	
	//Now we get the last override, if the NPC is modified by any other mods
	eNPC := HighestOverrideOrSelf(e);
	
	//Check if the NPC uses a template, and if the NPC uses the template's packages
	//If it doesn't, the string returned will have no characters: ''
	if GetElementEditValues(eNPC, 'ACBS - Configuration\Template Flags\Use Ai Packages') = '1' then
		exit;
		
	//Check if the NPC's Full FormID is in our list of NPCs to edit	
	if tstrlistNPCsToEdit.IndexOf(GetElementEditValues(eNPC, 'Record Header\FormID')) < 0 then
		exit;
	
	//Everything is good, time to do the patching
	eNPC := wbCopyElementToFile(eNPC, filePatch, false, true);
	
	//If the NPC doesn't have any package data, we'll add it
	if ElementExists(eNPC, 'Packages') == false then
		Add(eNPC, 'Packages', false);
		
	//Now we get the package data!	
	ePackages := ElementByPath(eNPC, 'Packages');
	
	//Go through every line in the text file list of packages
	for iCounter := 0 to tstrlistPackagesToAdd.Count - 1 do begin
	
		//If we had to create the package data container, the first entry will have the default value
		//So we change that
		//If it was there already, we'll just insert a new entry at the top
		if GetEditValue(ElementByIndex(ePackages, 0)) = 'NULL - Null Reference [00000000]' then
			ePackageAssigned := ElementByIndex(ePackages, 0)
		else
			ePackageAssigned := ElementAssign(ePackages, 0, nil, false);
		
		//Now we add the package itself
		SetEditValue(ePackageAssigned, tstrlistPackagesToAdd[iCounter]);
	
	end;
	
end;


function Finalize: Integer;
begin

	//Now we must clean up our TStringList variables, as they will persist in xEdit's memory unless we get rid of them
	tstrlistNPCsToEdit.Free;
	tstrlistPackagesToAdd.Free;
	
end;

end.

 

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...