GoTo
A command for Oblivion Script Extender
Syntax:
GoTo [labelID]
An alias for RestoreIP. This command causes script processing to jump to a label defined within the same script. Use Label or SaveIP to define the instruction point to which to jump.
Basic Example:
short count set Count to GetActiveEffectCount Label if ( count > 0 ) if GetNthActiveEffectCaster count == player messagebox "Player put a Hex on me!" endif set count to count - 1 Goto endif
Optional label IDs allow for multiple labels or nested loops within a script.
Example:
begin onActivate ; declare a variable to keep track of the number of times we've gone through the loop long var set var to 0 ; save the current instruction pointer ; this records that the beginning of the loop is right before the PrintToConsole command SaveIP ; could also use Label ; print the current loop count PrintToConsole "loop %f" var ; update the loop counter set var to var + 1 ; we only want to go through the loop three times, so check the loop counter if var < 3 ; jump back to the saved location RestoreIP ; could also use Goto endif ; if we get here, we're done with the loop end
When activated, this script will print:
loop 0.0000 loop 1.0000 loop 2.0000
To support nested loops, the SaveIP and RestoreIP commands take an optional integer parameter specifying which 'slot' to save the instruction location in. If you don't specify a number, it defaults to 0. There are 256 slots in the internal list.
A more familiar For-loop in a normal programming language would look like this:
for (i = 0; i < 5; i++) { for (j = 0; j < 3 ; j++) { //Do Something } }
To create the same loop using OBSE functions:
short i short j Label 0 ; top of outer loop, equivalent to SaveIP 0 set j to 0 Label 1 ; top of inner loop PrintToConsole "i = %.0f, j = %.0f" i, j set j to ( j + 1 ) if ( j < 3 ) GoTo 1 ; equivalent to RestoreIP 1 endif set i to ( i + 1 ) if ( i < 5 ) GoTo 0 endif
Note that unless the above loop is wrapped inside a conditional statement or a block like OnActivate, it will restart the very next frame and every frame thereafter.
NotesEdit
- Be careful to make sure your loop has an exit condition, otherwise you run the risk of creating an infinite loop, which can freeze the game and make it impossible for the player to exit.
- For the most part, scripts will run one at a time and different scripts can use the same label IDs without any problems. However, result scripts and scripts with onActivate blocks will run instantly when called via script (SetStage and ObjectRef.Activate player, 1, respectively), before the next line of code proceeds. For those two script types, make sure the IDs are unique.
- If you use a label ID in the calling script, and then the same ID in the called script, the ID will be set to the location in the called script. If the calling script uses Goto ID, when that ID is set to another script, it will act as a Return.