Editing User Defined Function

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 5: Line 5:
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 up to 15 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 one or more arguments of any type, which are defined within curly braces:
<pre>
<pre>
scn SomeUDF
scn SomeUDF
Line 33: Line 33:
</pre>
</pre>


==Examples==
==Example==
<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 104: Line 80:
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==

Please note that all contributions to the Fallout3 GECK Wiki are considered to be released under the Creative Commons Attribution-ShareAlike (see GECK:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)

Template used on this page: