Difference between revisions of "TES5Edit Scripting Functions"

From the CreationKit Wiki
Jump to navigation Jump to search
imported>Mator
 
(120 intermediate revisions by 12 users not shown)
Line 1: Line 1:
[[Category:Tools]]
Do not add to this page please. All documentation has been moved to GitHub:
[[Category:Data File Editors]]


== TES5Edit Scripting ==
* [https://tes5edit.github.io/docs/13-Scripting-Functions.html Scripting Functions]
=== Description ===
Work in progress: To become the future home of scripting functions for TES5Edit. If you make scripts for TES5Edit please contribute to this page.


TES5Edit implements a script engine based on pascal syntax. The following table enumerates the functions exported by TES5Edit to the script engine that allows interacting with the editor's elements to perform various tasks such as finding records, fixing record conflicts, etc.
If you would like to contribute feel free to edit the files and submit your changes:


The information in the table is not complete so please contribute by explaining these functions, their uses and by fixing mistakes in this information.
https://github.com/TES5Edit/docs


=== TES5Edit Scripting Functions ===
= Additional TES5Edit Resources =
The types described by this table such as IwbElement, IwbFile, etc.. are internal and not directly accessible but instead the script engine receives a generic type named <b>IInterface</b> which is common to every object type returned by TES5Edit.
The reason the internal types are shown in this table is because some functions expect references to specific types even if the object reference is being hold by the generic IInterface variable.


{| class="wikitable" style="background-color: #AAAAAA; cellpadding: 0"
* [[TES5Edit|TES5Edit]]
|- align="left"
* [[TES5Edit_Cleaning_Guide_-_TES5Edit|TES5Edit Cleaning Guide]]
! scope="col" style="background-color: #DDDDDD" | Function
* [[TES5Edit_Mod_Cleaning_Tutorial|TES5Edit Mod Cleaning Tutorial]]
! scope="col" style="background-color: #DDDDDD" | Returns
* [[Fixing_Navmesh_Deletion_Tutorial|Fixing Navmesh Deletion Tutorial]]
! scope="col" style="background-color: #DDDDDD" | Arguments
* [[Skyrim_Dirty_Plugins_List|Skyrim Dirty Plugins List]]
! scope="col" style="background-color: #DDDDDD" | Description
* [[TES5Edit_Scripting_Functions|TES5Edit Scripting Functions]]
|- align="left" style="background-color: #F8F8F8"
* [https://bethesda.net/community/topic/57570/relz-sseedit/ Official Elderscrolls TES5Edit forum topic] (offline)
| scope="row" | <b>Add</b> || IwbElement || container : IwbContainer, name : string, silent = true : boolean || Implemented for main records etc.
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>AddElement</b> ||  || container : IwbContainer , element : IwbElement  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>AdditionalElementCount</b> || integer || container : IwbContainer ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>AddMasterIfMissing</b> ||  || file : IwbFile , mastername : string  || Adds a master to a file by its filename if it isn't already a master for that file
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>AddMessage</b> ||  || message : string  || Adds a message line into the TES5Edit output panel
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>AddNewFile</b> || IwbFile ||  || Adds a new file and returns its reference
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>AddRequiredElementMasters</b> || IwbFile || aSourceElement : IwbElement , aTargetFile : IwbFile , aAsNew : boolean  || Add the element''s master into the target file.
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>Assigned</b> || boolean || element : IwbElement  || Returns true if the element is not nil.  Else returns false.
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>CanContainFormIDs</b> || boolean || element : IwbElement  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>CanMoveDown</b> || boolean || element : IwbElement  || Checks if an element can by moved down a list to a higher index
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>CanMoveUp</b> || boolean || element : IwbElement  || Checks if an element can be moved up a list to a lower index
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>ChangeFormSignature</b> ||  || record : IwbMainRecord , signature : TwbSignature  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>Check</b> || boolean || element : IwbElement  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>ChildGroup</b> || IwbGroupRecord || record : IwbMainRecord  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>ChildrenOf</b> || IwbMainRecord || group : IwbGroupRecord  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>CleanMasters</b> ||  || file : IwbFile  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>CompareExchangeFormID</b> || boolean || mainrecord : IwbMainRecord , oldFormID : Cardinal , aNewFormID : Cardinal  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>ConflictAllForMainRecord</b> || TConflictThis || record : IwbMainRecord  || Gets the ConflictThis argument of the record by calling ConflictLevelForMainRecord (see ctXxxxx enums)
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>ConflictAllForNode</b> || TConflictThis || node : IwbElement  || Gets the ConflictAll argument of the record by calling ConflictLevelForMainRecord (see ctXxxxx enums)
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>ConflictThisForMainRecord</b> || TConflictThis || record : IwbMainRecord  || Gets the ConflictThis argument of the record by calling ConflictLevelForMainRecord (see ctXxxxx enums)
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>ConflictThisForNode</b> || TConflictThis || node : IwbElement  || Gets the ConflictAll argument of the record by calling ConflictLevelForMainRecord (see ctXxxxx enums)
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>ContainingMainRecord</b> || IwbMainRecord || element : IwbElement  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>DefType</b> || TwbDefType || element : IInterface  || returns the IwbElement::DefType (see dtXxxx enums)
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>ElementAssign</b> || IwbElement || container : IwbContainer , aInder : integer, [element : IwbElement], aOnlySK : boolean  || Adds a new element to the container
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>ElementByIndex</b> || IwbElement || container : IwbContainer , index : integer  || Gets an element in the container by index
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>ElementByName</b> || IwbElement || container : IwbContainer , name : string  || Gets an element in the container by name
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>ElementByPath</b> || IwbElement || container : IwbContainer , path : string  || Gets an element in the container by path
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>ElementBySignature</b> || IwbElement || container : IwbContainer , signature : string  || Gets an element in the container by its signature
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>ElementCount</b> || integer || container : IwbContainer  || Returns the number of elements in a container
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>ElementExists</b> || boolean || container : IwbContainer , name : string  || Checks if the name of the element already exist in the container
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>ElementType</b> || TwbElementType || element : IwbElement  || Returns the ElementType of the element
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>Equals</b> || boolean || element1 : IwbElement , element2 : IwbElement  || Compares both elements by their ElementID
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>FileByIndex</b> || IwbFile || index : integer  || Gets the file at the specified index
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>FileByLoadOrder</b> || IwbFile || loadorder : integer  || Gets the file at the specified load order
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>FileFormIDtoLoadOrderFormID</b> || cardinal || file : IwbFile , formid : string/cardinal  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>FindChildGroup</b> || IwbGroupRecord || group : IwbGroupRecord , aType : integer , aMainRecord : IwbMainRecord  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>FixedFormID</b> || cardinal || aMainRecord : IwbMainRecord  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>FormID</b> || cardinal || aMainRecord : IwbMainRecord  || Obtains the FormID of the record
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>FullPath</b> || string || element : IwbElement  || Gives the full path, all the way down to file, of the input element
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>GetContainer</b> || IwbContainer || element : IwbElement  || Gets the container of the element
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>GetEditValue</b> || string || element : IwbElement  || Gets the element's value represented as text
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>GetElementEditValues</b> || string || element : IwbElement , name : string  || Gets the element's value represented as text
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>GetElementNativeValues</b> || variant || element : IwbElement , name : string  || Gets the element's native value
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>GetFile</b> || IwbFile || element : IwbElement  || Gets the file that defines the element
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>GetFileName</b> || string || file : IwbFile  || Obtains the filename of the plugin file
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>GetFormVersion</b> || cardinal || mainrecord : IwbMainRecord  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>GetIsDeleted</b> || boolean || mainrecord : IwbMainRecord  || Indicates if the record has been deleted
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>GetIsESM</b> || boolean || file : IwbFile  || Indicates if the plugin is an ESM file
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>GetIsInitiallyDisabled</b> || boolean || mainrecord : IwbMainRecord  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>GetIsPersistent</b> || boolean || mainrecord : IwbMainRecord  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>GetIsVisibleWhenDistant</b> || boolean || mainrecord : IwbMainRecord  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>GetLoadOrder</b> || cardinal || file : IwbFile  || Gets the global load order of the file
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>GetLoadOrderFormID</b> || cardinal || mainrecord : IwbMainRecord  || Obtains the FormID with the current load order applied
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>GetNativeValue</b> || variant || element : IwbElement  || Gets the element's native value
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>GroupBySignature</b> ||  || file : IwbFile , signature : string  || Selects a group in a file by its signature
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>GroupLabel</b> || cardinal || group : IwbGroupRecord  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>GroupType</b> || integer || group : IwbGroupRecord  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>HasGroup</b> || boolean || file : IwbFile , signature : string  || Checks if a file has a group by the group's signature
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>HasMaster</b> || boolean || file : IwbFile , signature : string  || Returns true if the file has a master file defined
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>IndexOf</b> || integer || container : IwbContainer , element : IwbElement  || Gets the index of the element inside the collection
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>InsertElement</b> ||  || container : IwbContainer , position : integer , element : IwbElement  || Inserts an existing element inside a collection by position
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>IsEditable</b> || boolean || element : IwbElement  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>IsInjected</b> || boolean || element : IwbElement  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>IsMaster</b> || boolean || record : IwbMainRecord  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>IsWinningOverride</b> || boolean || record : IwbMainRecord  || Checks whether or not the record is the highest override loaded
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>JumpTo</b> || TConflictThis || record : IwbMainRecord , backward : boolean  || Selects the specified record
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>LastElement</b> || IwbElement || container : IwbContainer  || Obtains the last element in the collection
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>LinksTo</b> || IwbElement || element : IwbElement  || Obtains the referenced element
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>LoadOrderFormIDtoFileFormID</b> || cardinal || file : IwbFile , aFormID : cardinal  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>MainRecordByEditorID</b> || IwbMainRecord || group : IwbGroupRecord  || Does not work for every case because it's inefficient
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>Master</b> || IInterface || record : IwbMainRecord  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>MasterByIndex</b> || IInterface || file : IwbFile , index : integer  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>MasterCount</b> || cardinal || file : IwbFile  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>MasterOrSelf</b> || IwbMainRecord || record : IwbMainRecord  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>MoveDown</b> ||  || element : IwbElement  || Moves the element down
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>MoveUp</b> ||  || element : IwbElement  || Moves the element up
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>Name</b> ||  || element : IwbElement  || Obtains the name of the element
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>OverrideByIndex</b> || IwbMainRecord || record : IwbMainRecord , index : integer  || Gives the overriding record associated with the provided index
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>OverrideCount</b> || cardinal || record : IwbMainRecord  || Provides a value for the number of overrides there are for a given record
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>Path</b> || string || element : IwbElement  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>RecordByEditorID</b> || IwbMainRecord || file : IwbFile , editorid : string  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>RecordByFormID</b> || IwbMainRecord || file : IwbFile , formid : integer , aAllowInjected : boolean  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>RecordByIndex</b> || IwbMainRecord || file : IwbFile , index : integer  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>RecordCount</b> || cardinal || file : IwbFile  || Returns a value corresponding to the number of records in a file
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>ReferencedByCount</b> || cardinal || record : IwbMainRecord  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>ReferencedByIndex</b> || IwbMainRecord || record : IwbMainRecord , index : integer  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>Remove</b> ||  || element : IwbElement  || Removes an element
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>RemoveByIndex</b> || IwbElement || container : IwbContainer , index : integer , aMarkModified : boolean  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>RemoveElement</b> || IwbElement || container : IwbContainer , element : IwbElement  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>RemoveNode</b> || boolean || node : IwbElement  || Removes the node from the file
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>ResourceCopy</b> ||  || container : IwbContainer , fileName : string , pathOut : string , containerIndex : integer  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>ResourceCount</b> || cardinal || container : IwbContainer , aFileName : string , containers : TStrings  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>ResourceExists</b> || boolean || container : IwbContainer , aFileName : string  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>ResourceList</b> ||  || container : IwbContainer , aContainerName : string , containers : TStrings  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>ReverseElements</b> ||  || container : IwbContainer  || Reverses the order of a list of elements in a container
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>SetEditValue</b> ||  || element : IwbElement , value : string  || Sets the value of the element as string
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>SetElementEditValues</b> ||  || container : IwbContainer , path : string , value : string  || Sets the value as a string of the element by its path
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>SetElementNativeValues</b> ||  || container : IwbContainer , path : string , value : variant  || Sets the native value of the element by its path
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>SetFormVersion</b> ||  || record : IwbMainRecord , version : integer  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>SetIsDeleted</b> ||  || record : IwbMainRecord , value : boolean  || Enables/disables deleted flag for a record
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>SetIsESM</b> ||  || file : IwbFile , value : boolean  || Enables/disbles ESM flag for a file
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>SetIsInitiallyDisabled</b> ||  || record : IwbMainRecord , value : boolean  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>SetIsPersistent</b> ||  || record : IwbMainRecord , value : boolean  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>SetIsVisibleWhenDistant</b> ||  || record : IwbMainRecord , value : boolean  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>SetLoadOrderFormID</b> ||  || record : IwbMainRecord , loadOrderFormId : cardinal  || Changes the 8-digit hexadecimal Form ID for a record
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>SetNativeValue</b> ||  || element : IwbElement , value : variant  || Sets the native value of the element
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>ShortName</b> || string || element : IwbElement  || Gets the short name of the element
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>Signature</b> || string || record : IwbMainRecord  || Gets the signature of a record
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>SortKey</b> || string || element : IwbElement, aExtended: boolean  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>SortMasters</b> ||  || file : IwbFile  || Attempts to sort the masters for a file by their load order
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>wbCopyElementToFile</b> || IwbElement || element : IwbElement , file : IwbFile , aAsNew : boolean , aDeepCopy : boolean  ||
|- align="left" style="background-color: #EEEEEE"
| scope="row" | <b>wbCopyElementToRecord</b> || IwbElement || element : IwbElement , aMainRecord : IwbMainRecord , aAsNew : boolean , aDeepCopy : boolean  ||
|- align="left" style="background-color: #F8F8F8"
| scope="row" | <b>WinningOverride</b> || IwbMainRecord || record : IwbMainRecord  || Will return the winning override record
|}
 
=== Script Structure ===
==== Base Script Functions ====
The are three special functions that TES5Edit will call when a script is run:
* '''Initialize''': This function is called when the script starts. It's useful to initialize variables.
* '''Process''': This function is called for every record selected in the TES5Edit tree. If a plugin is selected then it will be called for each record defined in the plugin. The same happens if a record type is selected in the tree.
* '''Finalize''': This function is called when the script has finished processing every record. Generally useful for saving files and freeing the allocated resources.
 
All these functions are optional, so if they are not needed they can be omitted.
 
==== Hotkeys ====
 
TES5Edit can assign ''hotkeys'' to scripts. The script hotkey is defined in the description like this:
 
<pre>
{
Script description.
------------------------
Hotkey: Ctrl+Alt+Shift+E
}
</pre>
 
==== Script References ====
 
Scripts can use functions defined in other scripts. That allows creating ''toolkits'' to avoid duplicating code. To make use of this feature the following instruction is used: (use below the unit name)
 
<pre>
uses 'MyTools';
</pre>
 
With that command we instruct the script to load another script named "MyTools.pas" and the functions in that script will be available. Note that any conflict in names can be resolved by the unit name. So it's suggested to change the ''toolkit'' script unit name appropriately.
 
==== Script User Interface ====
 
TO DO: Explain user interface and provide some examples.
 
=== Simple Script Sample ===
 
This is a sample script which will only export every selected NPC to a CSV file:
 
<pre>
{
Script description: Exports the FormID and EditorID of the selected NPCs
}
 
// This is the unit name that will contain all the script functions
unit ExportScripts;
 
// Global variables
var NPCList : TStringList;
 
// Called when the script starts
function Initialize : integer;
begin
NPCList := TStringList.Create;
NPCList.Add('FormID;EditorID');
end;
 
// Called for each selected record in the TES5Edit tree
// If an entire plugin is selected then all records in the plugin will be processed
function Process(e : IInterface) : integer;
begin
if Signature(e) <> 'NPC_' then exit;
NPCList.Add(IntToHex(FixedFormID(e), 8) + ';' + GetElementEditValues(e, 'EDID'));
end;
 
// Called after the script has finished processing every record
function Finalize : integer;
var filename : string;
begin
filename := ProgramPath + 'Edit Scripts\NPCs.csv';
AddMessage('Saving NPC list to ' + filename);
NPCList.SaveToFile(filename);
NPCList.Free;
end;
 
end.
</pre>

Latest revision as of 04:25, 10 April 2024

Do not add to this page please. All documentation has been moved to GitHub:

If you would like to contribute feel free to edit the files and submit your changes:

https://github.com/TES5Edit/docs

Additional TES5Edit Resources[edit | edit source]