Open main menu

UESPWiki β

StartScript

                StartScript, ScriptName

       Where:   ScriptName = Name of the script to start

        Type:   Script

     Returns:   none

     Example:   StartScript, CharGen

     Scripts:   Main
                MoonAndStar

This function starts a script running as a global script. It is not attached to any object, so functions like moving, rotating, checking distances and such have no bearing in a global script (which means you must specify object IDs explicitly). Note this isn't exactly true as if you start a global script it will use the calling script object by default (i.e., if you started a global script from within the NPC Bob's script, the global script would use Bob as the default object). Global scripts are good for running complex quests, checking variables, or setting timers. Each global script is run every frame so take care not to run too many at one or the game's speed will be reduced.

You can target a global script using either ObjectID->StartScript, or calling StartScript from the dialogue results box. While both are true, they don't quite work as expected when used together. From the results box, ObjectID->StartScript seems to attach the script to the NPC calling the dialogue and not the referenced object. When you use a targeted global script any function call in the script will use the target object (you don't need to explicitly specify the object unless you need/want to).

A script started with StartScript runs as a global script, even if it is targeted to an object. You can only have one instance of the script running, even if you target different objects. If you use StartScript when a script is already running, it will stop the first instance running before starting, even on a different object.

For Example, a script to empty an NPCs inventory of certain items (which contains Stopscript to stop itself after running once) may be called by an object in the same cell as the NPC. If there are two NPCs in the same cell targeted by the same script, only one will have their inventory emptied.

To get around this problem, insert a way of ensuring only one version of the script is running, such as using a global variable or using ScriptRunning to check if the script is already running before using StartScript.


if ( ScriptRunning Scriptname )
        Return
Endif

"ObjectID"->StartScript, Scriptname

OR

if ( GlobalVariable == 0 )
        Set GlobalVariable to 1
        "ObjectID"->StartScript, Scriptname
endif

if ( ScriptRunning Scriptname )
        Return
Endif

Set GlobalVariable to 0

See Also: ScriptRunning, StopScript