MSFD General Information: Scripts, Commands and Syntax
Types of scripts
Local scripts
Any script that is running on an object or Actor in the game (assigned in the script-dropdown field of the object or Actors object window) is a local script. Local scripts are only active if the cell is loaded – this is the current interior cell, or the current and all directly neighboring exterior cells. When the object is outside of this range the script is not running, but the local variables are saved. You cannot stop a local script using Stopscript.
Global scripts
Any script that is not attached to any object is a global script, and is by default not executed until you call it (see below). Note that there is no default object for a global script to work on, so objects must always be specified: while the following will work in a local script attached to an NPC:
AITravel 1150, 8899, 1110
You will have to specify the NPC in a global script:
"NPC_ID"->AITravel 1150, 8899, 1110 ;NPC_ID is the ID, the unique identifier for
;each object in the editor
Global scripts are active all the time once they have been activated and until they are specifically terminated. Thus, once activated, they will be processed every frame as described for locals scripts above. That is why they should be used with caution, as too many, or too complicated global scripts can easily slow the game down a lot.
The command to start a non-active script is:
StartScript, "Script ID"
With Tribunal and Bloodmoon you also have the option to make a script start automatically with the game. In the TESCS under the menu Gameplay/Edit starting Scripts/ you can add any script to the list of automatically started scripts.
The function to terminate a global script is:
StopScript, "Script ID"
Variables local to a global script will be saved temporarily when the script is stopped and restarted. In order to ensure that variables are always saved, the script must be run at least once every load session. If you need to be sure the variables are reset, you should reset them yourself: don't rely on it happening automatically.
A simple way to ensure variables are saved is to use a startscript (only available with expansions); so for example the startscript might look something like this:
begin KeepVarsScript
if ( ScriptRunning, MyScript == 0 )
set MyScript.KeepVars to 1
StartScript MyScript
endif
StopScript KeepVarsScript
end
…and the script "MyScript" might look like this:
begin MyScript
short KeepVars
if ( KeepVars == 1 )
set KeepVars to 0
StopScript MyScript
return
endif
;main body of script goes here
end
It is possible to use the StartScript function to run global scripts that are tied to an object or Actor. These are called "targeted scripts".
"Object_ID"->StartScript "Script_ ID"
These scripts resemble both local scripts (in that the functions called always default to the object or Actor the script targets) and global scripts (in that they are always running and can be terminated with StopScript).
Note: read more on the special case of "targeted scripts" in the Tips and Tricks section.