Difference between revisions of "User Defined Function"

From the Fallout3 GECK Wiki
Jump to navigation Jump to search
imported>Odessa
(stub overview)
 
imported>Odessa
(tweaked)
Line 1: Line 1:
User defined functions (UDF) are added by NVSE V4. They must be saved as 'object' type scripts that must use the "Function" block type only. Although 'object' type, they must not be attached to any object and are instead called directly by script name from within other scripts, using the [[Call]] command.  
User defined functions (UDF) are added by NVSE V4. They must be saved as 'object' type scripts that must use the "Function" block type only. Although 'object' type, they must not be attached to any object and are instead called directly by script name from within other scripts, using the [[Call]] command.  


UDFs may optionally return a single value of any type, using the [[SetFunctionValue]] command within them. They may optionally accept one or more arguments of any type, which are specified within curly braces:
UDFs may optionally return a single value of any type, using the [[SetFunctionValue]] command within them. They may optionally accept one or more arguments of any type, which are defined within curly braces:
<pre>
<pre>
Begin Function { arg1, arg2..}
int arg1 ; * args are optional
ref arg2
float arg3 ....
 
Begin Function { arg1, arg2, arg3... }
     ; * body
     ; * body
End  
End  
</pre>
and called in a similar way to regular functions:
<pre>
Call SomeUDF arg1, arg2, arg3...
let SomeVariable := Call SomeOtherUDF arg1
if eval (call SomeSuitableUDF)
    ; the UDF return evaluates to true
endif
</pre>
</pre>


Line 15: Line 29:
; ***
; ***


; * vars
; * local vars
array_var aNew
array_var aNew
int iKey
int iKey
Line 45: Line 59:
array_var aBeatles
array_var aBeatles


Begin GameMode ; * some block type
Begin GameMode ; * or some other block type


     let aBeatles := Ar_List JohnREF, PaulREF, GeorgeREF, RingoREF
     let aBeatles := Ar_List JohnREF, PaulREF, GeorgeREF, RingoREF

Revision as of 00:18, 28 May 2014

User defined functions (UDF) are added by NVSE V4. They must be saved as 'object' type scripts that must use the "Function" block type only. Although 'object' type, they must not be attached to any object and are instead called directly by script name from within other scripts, using the Call command.

UDFs may optionally return a single value of any type, using the SetFunctionValue command within them. They may optionally accept one or more arguments of any type, which are defined within curly braces:

int arg1 ; * args are optional
ref arg2
float arg3 ....

Begin Function { arg1, arg2, arg3... }
    ; * body
End 

and called in a similar way to regular functions:

Call SomeUDF arg1, arg2, arg3...

let SomeVariable := Call SomeOtherUDF arg1

if eval (call SomeSuitableUDF)
    ; the UDF return evaluates to true
endif

Example

scn fnShuffle

; *** This script accepts a list array and returns it in a shuffled order
; ***

; * local vars
array_var aNew
int iKey
int iLast

; * args
array_var aList

Begin Function { aList }

    let aNew := Ar_Construct "array" ; * get a new empty list array

    while (Ar_Size aList) > 0        ; * while input array contains an element
        let iLast := Ar_Size aList   ; * get size of input array
        let iKey := Rand 0, iLast    ; * get random valid key for input array
        Ar_Append aNew, aList[iKey]  ; * append the random choice element to output array
        Ar_Erase aList, iKey         ; * erase that element from input array
    loop

    SetFunctionValue aNew            ; * return the shuffled array
    return

End

The above example shows a user defined function that accepts a list array and returns it in a shuffled order. This might be called in another script, like:

scn SomeOtherScript

array_var aBeatles

Begin GameMode ; * or some other block type

    let aBeatles := Ar_List JohnREF, PaulREF, GeorgeREF, RingoREF

    let aBeatles := Call fnShuffle aBeatles

    ; * The Beatles array is now in a shuffled order

End

Notes

  • UDFs may be used recursively by saving the script once first without the sub call.
  • This article is a simple overview, seek external documentation for further information.

See Also