Difference between revisions of "TES5Edit Scripting Functions"

Jump to navigation Jump to search
3,577 bytes added ,  11:48, 6 January 2018
Added missing types function AddNewFile, as well as instructions for using Delphi.
imported>Robboten
m
imported>J.C.
(Added missing types function AddNewFile, as well as instructions for using Delphi.)
Line 6: Line 6:
Work in progress: To become the future home of scripting functions for TES5Edit. If you make scripts for TES5Edit please contribute to this page.
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.  
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.  


The information in the table is not complete so please contribute by explaining these functions, their uses and by fixing mistakes in this information.
The information in the table is not complete so please contribute by explaining these functions, their uses and by fixing mistakes in this information.
=== Using the Delphi IDE to Edit TES5Edit Scripts ===
As of TES5Edit version 3.2.1, there is a file in the <code>Edit Scripts</code> directory called [https://github.com/TES5Edit/TES5Edit/blob/dev/Edit%20Scripts/xEditAPI.pas xEditAPI.pas].
The contents of that file match what is published here. The xEditAPI.pas includes xEdit API
declarations to be used for editing scripts using Delphi.
With this file you can now edit xEdit scripts using Delphi.
This gives you full IDE benefits like checking for warnings and errors before
executing your script, as well as intellisense and code insights.
For example purposes in this documentation, we assume a user created xEdit script named <code>GreatScript.pas</code>.
However, you can name your script almost anything. There are some limitations though,
because the name of the script must match the unit declaration of the script, and
unit names have limitations. See [http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Fundamental_Syntactic_Elements_(Delphi)#Identifiers Delphi Identifiers]
Instructions to get started editing xEdit scripts with Delphi:
<ol>
<li>Download the free Delphi Starter edition, called RAD Studio (current version is 10.2) from [https://www.embarcadero.com/products/delphi/starter Embarcadero Delphi Starter]</li>
<li>Create new console application project named <code>GreatScriptApp</code></li>
<li>Add the files <code>xEditAPI.pas</code> and <codE>GreatScript.pas</code> to the project (menu Project -> Add to project)</li>
<li>At the top of the script, modify or add the <code>unit</code> clause. The name of the unit must match exactly the filename of your script without the .pas extension.</li>
<li>Then add/update the <code>interface</code>, <code>implementation</code>, and <code>uses</code> clauses to your script below. Afterwards, the top of your script file should look like this:</li>
<pre>
  unit GreatScript;
 
  interface
  implementation
  uses xEditAPI, Classes, SysUtils, StrUtils, Windows;
</pre>
<li>You can add additional Delphi units to the <code>uses</code> clause needed, for example <code>IniFiles</code> if script works with them.</li>
<li>Check that project can be compiled <code>Ctrl+F9</code> and the same script can be applied in xEdit without errors.</li>
<li>While editing your script, before running it in xEdit, press <code>Ctrl+F9</code> in Delphi to compile and ensure that the code is valid.</li>
<li>You can also modify the GreatScriptApp file and have it run your xEdit script from Delphi by pressing <code>F9</code>. Make GreatScriptApp look something like this:</li>
<pre>
  program GreatScriptApp;
 
  {$APPTYPE CONSOLE}
 
  {$R *.res}
 
  uses
    Windows,
    Winapi.ShellApi,
    System.SysUtils,
    xEditAPI in 'C:\Games\FO4Edit 3.2\Edit Scripts\xEditAPI.pas';
    GreatScript in 'C:\Games\FO4Edit 3.2\Edit Scripts\GreatScript.pas',
 
  begin
    try
      ShellExecute(0, Nil, 'C:\Games\FO4Edit 3.2\FO4Edit.exe', '-script:"GreatScript.pas" -nobuildrefs', 'C:\Games\FO4Edit 3.2', SW_SHOWNORMAL);
    except
      on E: Exception do
        Writeln(E.ClassName, ': ', E.Message);
    end;
  end.
</pre>
</ol>


=== TES5Edit Global Variables ===
=== TES5Edit Global Variables ===
Line 40: Line 97:


     [[#IwbElement|IwbElement]]
     [[#IwbElement|IwbElement]]
     IwbContainerBase
     + IwbContainerBase
       [[#IwbContainer|IwbContainer]]
       + [[#IwbContainer|IwbContainer]]
       │ ├ IwbDataContainer
       ¦ + IwbDataContainer
       │ │ IwbFileHeader
       ¦ ¦ + IwbFileHeader
       │ │ IwbRecord
       ¦ ¦ + IwbRecord
       │ │     [[#IwbGroupRecord|IwbGroupRecord]]
       ¦ ¦     + [[#IwbGroupRecord|IwbGroupRecord]]
       │ │     [[#IwbMainRecord|IwbMainRecord]]
       ¦ ¦     + [[#IwbMainRecord|IwbMainRecord]]
       │ │     [[#IwbSubRecord|IwbSubRecord]]
       ¦ ¦     + [[#IwbSubRecord|IwbSubRecord]]
       │ │
       ¦ ¦
       │ └ [[#IwbFile|IwbFile]]
       ¦ + [[#IwbFile|IwbFile]]
      
       ¦
       IwbContainerElementRef
       + IwbContainerElementRef
      
      
     [[#IwbResource|IwbResource]]
     [[#IwbResource|IwbResource]]
     IwbResourceContainer
     IwbResourceContainer
     IwbBA2File
     + IwbBA2File
     IwbBSAFile
     + IwbBSAFile
     IwbFolder
     + IwbFolder
   
    Other Types Used but Unknown
    + TwbDefType
    + TwbElementState
    + TwbElementType
    + TwbGridCell
    + TwbVector
    + TwbFastStringList
    + TGameResourceType
      
      
     [[#Misc functions|Misc functions]]
     [[#Misc functions|Misc functions]]
     [[#DDS functions|DDS]]
     + [[#DDS functions|DDS]]
     [[#NIF functions|NIF]]
     + [[#NIF functions|NIF]]
 


==== Global functions ====
==== Global functions ====
Line 73: Line 140:
! scope="col" style="background-color: #DDDDDD" | Description
! scope="col" style="background-color: #DDDDDD" | Description
{{TES5Edit function row|even
{{TES5Edit function row|even
  |AddMessage||asMessage: string
  |Pushes a line to TES5Edit's Information tab.}}
{{TES5Edit function row|odd
   |Assigned|boolean|aeElement: IwbElement
   |Assigned|boolean|aeElement: IwbElement
   |An extension to Delphi's native [http://www.delphibasics.co.uk/RTL.asp?Name{{=}}Assigned Assigned] function: returns true if ''aeElement'' is not ''Nil'', and returns false otherwise.}}
   |An extension to Delphi's native [http://www.delphibasics.co.uk/RTL.asp?Name{{=}}Assigned Assigned] function: returns true if ''aeElement'' is not ''Nil'', and returns false otherwise.}}
{{TES5Edit function row|odd
{{TES5Edit function row|even
   |ObjectToElement|IInterface|akObject
   |ObjectToElement|IInterface|akObject
   |If you have stored an IInterface inside of a TList or TStringList, you must call this function when retrieving the object from the list, i.e. <code>ObjectToElement(myList.Items[0])</code>.}}
   |If you have stored an IInterface inside of a TList or TStringList, you must call this function when retrieving the object from the list, i.e. <code>ObjectToElement(myList.Items[0])</code>.}}
{{TES5Edit function row|even
{{TES5Edit function row|odd
   |FileByIndex|IwbFile|aiFile: integer
   |FileByIndex|IwbFile|aiFile: integer
   |See also: ''FileCount''.}}
   |See also: ''FileCount''.}}
{{TES5Edit function row|odd
{{TES5Edit function row|even
   |FullPathToFilename|string|asFilename: string
   |FullPathToFilename|string|asFilename: string
   |Returns the full path to the filename ''asFilename''.}}
   |Returns the full path to the filename ''asFilename''.}}
{{TES5Edit function row|even
{{TES5Edit function row|odd
   |EnableSkyrimSaveFormat||
   |EnableSkyrimSaveFormat||
   |As of xEdit 3.1.2, calling this function will corrupt saved plugins until xEdit is restarted.}}
   |As of xEdit 3.1.2, calling this function will corrupt saved plugins until xEdit is restarted.}}
{{TES5Edit function row|odd
{{TES5Edit function row|even
   |GetRecordDefNames||akList: TStrings
   |GetRecordDefNames||akList: TStrings
   |Unverified: Modifies ''akList'' by adding entries based on the contents of the global ''wbRecordDefs''.}}
   |Unverified: Modifies ''akList'' by adding entries based on the contents of the global ''wbRecordDefs''.}}
{{TES5Edit function row|even
{{TES5Edit function row|odd
   |wbFilterStrings||akListIn: TStrings;<br/> akListOut: TStrings;<br/> asFilter: String
   |wbFilterStrings||akListIn: TStrings;<br/> akListOut: TStrings;<br/> asFilter: String
   |Modifies ''akListOut'', adding every entry in ''akListIn'' that contains the substring ''asFilter''.}}
   |Modifies ''akListOut'', adding every entry in ''akListIn'' that contains the substring ''asFilter''.}}
{{TES5Edit function row|odd
{{TES5Edit function row|even
   |wbRemoveDuplicateStrings||akList: TStringList
   |wbRemoveDuplicateStrings||akList: TStringList
   |Modifies ''akList'', removing any duplicate entries that it contains.}}
   |Modifies ''akList'', removing any duplicate entries that it contains.}}
Line 328: Line 398:
   |Adds the specified file as a master for ''aeFile'', if it isn't already a master.}}
   |Adds the specified file as a master for ''aeFile'', if it isn't already a master.}}
{{TES5Edit function row|odd
{{TES5Edit function row|odd
  |AddNewFileName||aeFile: IwbFile<br/>
  |Creates a new, empty plugin in the game's plugin folder (Data) and adds it to the end of the plugins list.}}
{{TES5Edit function row|even
   |CleanMasters||aeFile: IwbFile
   |CleanMasters||aeFile: IwbFile
   |Appears to find unnecessary files in ''aeFile''<nowiki/>'s master list and remove them, updating all form indices accordingly. Don't confuse this within "cleaning master files" as in "removing ITMs and UDRs from official DLCs." This function is used in "Skyrim - Book Covers Patch.pas".}}
   |Appears to find unnecessary files in ''aeFile''<nowiki/>'s master list and remove them, updating all form indices accordingly. Don't confuse this within "cleaning master files" as in "removing ITMs and UDRs from official DLCs." This function is used in "Skyrim - Book Covers Patch.pas".}}
{{TES5Edit function row|even
{{TES5Edit function row|odd
   |FileFormIDtoLoadOrderFormID|cardinal|aeFile: IwbFile; aiFormID: cardinal
   |FileFormIDtoLoadOrderFormID|cardinal|aeFile: IwbFile; aiFormID: cardinal
   |Converts ''aiFormID'' from a form ID relative to ''aeFile''<nowiki/>'s master list (like that returned by ''FixedFormID'') to a load-order-relative form ID (like that returned by ''FormID''). See also: ''LoadOrderFormIDtoFileFormID''.}}
   |Converts ''aiFormID'' from a form ID relative to ''aeFile''<nowiki/>'s master list (like that returned by ''FixedFormID'') to a load-order-relative form ID (like that returned by ''FormID''). See also: ''LoadOrderFormIDtoFileFormID''.}}
{{TES5Edit function row|odd
{{TES5Edit function row|even
   |FileWriteToStream||aeFile: IwbFile;<br/> akOutStream: TStream
   |FileWriteToStream||aeFile: IwbFile;<br/> akOutStream: TStream
   |Writes the contents of ''aeFile'' to ''akOutStream''. Used in "SaveAs.pas" to allow a user to save a loaded file under a new name; that script creates a TFileStream object and uses this function to write to it.}}
   |Writes the contents of ''aeFile'' to ''akOutStream''. Used in "SaveAs.pas" to allow a user to save a loaded file under a new name; that script creates a TFileStream object and uses this function to write to it.}}
{{TES5Edit function row|even
{{TES5Edit function row|odd
   |GetFileName|string|aeFile: IwbFile
   |GetFileName|string|aeFile: IwbFile
   |Returns ''aeFile''<nowiki/>'s filename.}}
   |Returns ''aeFile''<nowiki/>'s filename.}}
{{TES5Edit function row|odd
{{TES5Edit function row|even
   |GetIsESM|boolean|aeFile: IwbFile
   |GetIsESM|boolean|aeFile: IwbFile
   |Returns ''True'' if ''aeFile'' is flagged as an ESM. See also: ''SetIsESM''.}}
   |Returns ''True'' if ''aeFile'' is flagged as an ESM. See also: ''SetIsESM''.}}
{{TES5Edit function row|even
{{TES5Edit function row|odd
   |GetLoadOrder|string|aeFile: IwbFile
   |GetLoadOrder|string|aeFile: IwbFile
   |Returns ''aeFile''<nowiki/>'s index in the load order, or -1 if called on something that is not an IwbFile.}}
   |Returns ''aeFile''<nowiki/>'s index in the load order, or -1 if called on something that is not an IwbFile.}}
{{TES5Edit function row|odd
{{TES5Edit function row|even
   |GetNewFormID|cardinal|aeFile: IwbFile
   |GetNewFormID|cardinal|aeFile: IwbFile
   |Returns a new form ID, the same way that <code>Add(..., ..., True)</code> does.}}
   |Returns a new form ID, the same way that <code>Add(..., ..., True)</code> does.}}
{{TES5Edit function row|even
{{TES5Edit function row|odd
   |GroupBySignature|IwbGroupRecord|aeFile: IwbFile;<br/> asSignature: string
   |GroupBySignature|IwbGroupRecord|aeFile: IwbFile;<br/> asSignature: string
   |If ''aeFile'' has a top-level group with the specified signature, that group is returned.}}
   |If ''aeFile'' has a top-level group with the specified signature, that group is returned.}}
{{TES5Edit function row|odd
{{TES5Edit function row|even
   |HasGroup|boolean|aeFile: IwbFile;<br/> asSignature: string
   |HasGroup|boolean|aeFile: IwbFile;<br/> asSignature: string
   |Returns ''True'' if ''aeFile'' contains a top-level group with the specified signature.}}
   |Returns ''True'' if ''aeFile'' contains a top-level group with the specified signature.}}
{{TES5Edit function row|even
{{TES5Edit function row|odd
   |HasMaster|boolean|aeFile: IwbFile;<br/> asMasterFilename: string
   |HasMaster|boolean|aeFile: IwbFile;<br/> asMasterFilename: string
   |Returns ''True'' if ''aeFile'' has a file with the name ''asMasterFilename'' as a master.}}
   |Returns ''True'' if ''aeFile'' has a file with the name ''asMasterFilename'' as a master.}}
{{TES5Edit function row|odd
{{TES5Edit function row|even
   |LoadOrderFormIDtoFileFormID|cardinal|aeFile: IwbFile; aiFormID: cardinal
   |LoadOrderFormIDtoFileFormID|cardinal|aeFile: IwbFile; aiFormID: cardinal
   |Converts ''aiFormID'' from a load-order-relative form ID (like that returned by ''FormID'') to a form ID relative to ''aeFile''<nowiki/>'s master list (like that returned by ''FixedFormID''). See also: ''FileFormIDtoLoadOrderFormID''.}}
   |Converts ''aiFormID'' from a load-order-relative form ID (like that returned by ''FormID'') to a form ID relative to ''aeFile''<nowiki/>'s master list (like that returned by ''FixedFormID''). See also: ''FileFormIDtoLoadOrderFormID''.}}
{{TES5Edit function row|even
{{TES5Edit function row|odd
   |MasterByIndex|IwbFile|aeFile: IwbFile;<br/> aiIndex: integer
   |MasterByIndex|IwbFile|aeFile: IwbFile;<br/> aiIndex: integer
   |Returns the ''aiIndex''-th master file for ''aeFile''.}}
   |Returns the ''aiIndex''-th master file for ''aeFile''.}}
{{TES5Edit function row|odd
{{TES5Edit function row|even
   |MasterCount|cardinal|aeFile: IwbFile
   |MasterCount|cardinal|aeFile: IwbFile
   |Returns the number of master files that ''aeFile'' has.}}
   |Returns the number of master files that ''aeFile'' has.}}
{{TES5Edit function row|even
{{TES5Edit function row|odd
   |RecordByEditorID|IwbMainRecord|aeFile: IwbFile;<br/> asEditorID: string
   |RecordByEditorID|IwbMainRecord|aeFile: IwbFile;<br/> asEditorID: string
   |Returns the [[Magic Effect|MGEF]] or [[Setting|GMST]] record in ''aeFile'' that has the specified editor ID. For all other record types, use something like <code>MainRecordByEditorID(GroupBySignature(f, 'ECZN'), 'RandomEncounterZone1')</code>.}}
   |Returns the [[Magic Effect|MGEF]] or [[Setting|GMST]] record in ''aeFile'' that has the specified editor ID. For all other record types, use something like <code>MainRecordByEditorID(GroupBySignature(f, 'ECZN'), 'RandomEncounterZone1')</code>.}}
{{TES5Edit function row|odd
{{TES5Edit function row|even
   |RecordByFormID|IwbMainRecord|aeFile: IwbFile;<br/> aiFormID: integer;<br/> abAllowInjected: boolean
   |RecordByFormID|IwbMainRecord|aeFile: IwbFile;<br/> aiFormID: integer;<br/> abAllowInjected: boolean
   |Returns the main record in ''aeFile'' that has the specified form ID, or ''Nil'' if no records match. The form ID must be local to the file (see ''FixedFormID'').}}
   |Returns the main record in ''aeFile'' that has the specified form ID, or ''Nil'' if no records match. The form ID must be local to the file (see ''FixedFormID'').}}
{{TES5Edit function row|even
{{TES5Edit function row|odd
   |RecordByIndex|IwbMainRecord|aeFile: IwbFile;<br/> aiIndex: integer
   |RecordByIndex|IwbMainRecord|aeFile: IwbFile;<br/> aiIndex: integer
   |Returns the ''aiIndex''-th record in ''aeFile''.}}
   |Returns the ''aiIndex''-th record in ''aeFile''.}}
{{TES5Edit function row|odd
{{TES5Edit function row|even
   |RecordCount|cardinal|aeFile: IwbFile
   |RecordCount|cardinal|aeFile: IwbFile
   |Returns the number of records that ''aeFile'' has.}}
   |Returns the number of records that ''aeFile'' has.}}
{{TES5Edit function row|even
{{TES5Edit function row|odd
   |SetIsESM||aeFile: IwbFile;<br/> abFlag: boolean
   |SetIsESM||aeFile: IwbFile;<br/> abFlag: boolean
   |Modifies the ESM flag for ''aeFile''. See also: ''GetIsESM''.}}
   |Modifies the ESM flag for ''aeFile''. See also: ''GetIsESM''.}}
{{TES5Edit function row|odd
{{TES5Edit function row|even
   |SortMasters||aeFile: IwbFile
   |SortMasters||aeFile: IwbFile
   |Attempts to sort the masters for ''aeFile'' by their place in the current load order.}}
   |Attempts to sort the masters for ''aeFile'' by their place in the current load order.}}
Anonymous user

Navigation menu