Difference between revisions of "User Defined Function"

856 bytes added ,  13:31, 7 May 2017
no edit summary
imported>Odessa
(note about let and eval)
imported>Pintocat
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Template:Community_Wiki}}
==Overview==
==Overview==
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 defined within curly braces:
UDFs may optionally return a single value of any type, using the [[SetFunctionValue]] command within them. They may optionally accept up to 15 arguments of any type, which are defined within curly braces:
<pre>
<pre>
scn SomeUDF
scn SomeUDF
Line 30: Line 33:
</pre>
</pre>


==Example==
==Examples==
<pre>
scn GetRandomForm
 
; *** This script accepts a form list and returns one random form from it
 
; local variables
int iCount
int iChoice
ref RandomForm
 
; arguments
ref rFormList
 
Begin Function { rFormList }
 
    let iCount := ListGetCount rFormList
    let iChoice := Rand 0, iCount
    let RandomForm := ListGetNthForm rFormList, iChoice
   
    SetFunctionValue RandomForm
 
End
</pre>
 
<pre>
<pre>
scn fnShuffle
scn fnShuffle
Line 77: Line 104:
End
End
</pre>
</pre>
==Accepting any type arguments (advanced)==
In some contexts, it is useful to allow arguments of any type for a UDF. Since [[Array Variable|array]] elements can be any type, this can be conveniently achieved using the [[NVSE Expressions]], <b>*</b> ('box') and <b>&</b> ('unbox'), and the [[TypeOf]] / [[GetType]] functions.
<pre>
scn MyUDF
array_var AnyTypeArgument
Begin Function { AnyTypeArgument }
    if eval (TypeOf *AnyTypeArgument) == "form"
        ...
End
</pre>
<pre>
ref MyRef
int MyInt
call MyUDF &MyRef
call MyUDF &MyInt
</pre>
==Notes==
==Notes==
*UDFs may be used recursively by saving the script once first without the sub call.
*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==
==See Also==
Line 88: Line 135:
*[[Script Compiler Override]]
*[[Script Compiler Override]]
==External Links==
==External Links==
*[http://fallout.bethsoft.com/eng/links/privacyredirect.php?site=http://www.loverslab.com/topic/26802-tutorial-nvse4-part-2-user-defined-functions-udfs/ Tutorial on User Defined Functions in NVSE 4]
*[http://www.loverslab.com/topic/26802-tutorial-nvse4-part-2-user-defined-functions-udfs/ Tutorial on User Defined Functions in NVSE 4]
[[Category:NVSE]]
[[Category:NVSE]]
[[Category:User Defined Functions]]
Anonymous user