Difference between revisions of "Useful Scripts"
→GECK
imported>Chasethis |
imported>Nyteschayde (→GECK) |
||
Line 323: | Line 323: | ||
</pre> | </pre> | ||
==Simulating Global Functions in your Mod== | |||
Global functions, or behavior that acts as though it's a global function can be simulated through the use of a shared script (i.e. a Quest script) and setting properties on it as though it were a state machine. | |||
It must be understood that scripts execute all the way through, each tick, and | |||
many times the result from a function call won't be readable until the next tick has occurred. Some parts in this example use NVSE (FOSE works as well for Fallout3). | |||
The first script is called ExampleScript and is a Quest type script for the Quest called Example. Note that referencing variables in a script from outside of the script must be done by referencing the Quest name, not the Script name. (i.e. Example.Initialized instead of ExampleScript.Initialized). | |||
If you have any questions, email me at nyteshade.geck@gmail.com. | |||
<pre> | |||
ScriptName ExampleScript | |||
Short Initialized | |||
Short Msg_FnSum | |||
Short Msg_FnSum_Done | |||
Short Msg_FnProduct | |||
Short Msg_FnProduct_Done | |||
Short Param1 ; best to name these something better | |||
Short Param2 ; if you are not going to share them | |||
Short ReturnValue | |||
Begin GameMode | |||
If Initialized == 0 || GetGameLoaded || GetGameRestarted ; last two are from NVSE | |||
Set Initialized to 1 | |||
Set Param1 to 0 | |||
Set Param2 to 0 | |||
Set ReturnValue to 0 | |||
Set Msg_FnSum to 0 | |||
Set Msg_FnSum_Done to 0 | |||
Set Msg_FnProduct to 0 | |||
Set Msg_FnProduct_Done to 0 | |||
EndIf | |||
If Msg_FnSum != 0 | |||
Set ReturnValue to Param1 + Param2 | |||
Set Msg_FnSum to 0 | |||
Set Msg_FnSum_Done to 1 | |||
; Uncomment the next two lines and comment out the ElseIf, if you want to | |||
; allow multiple "functions" to execute per tick. This is not advised if | |||
; you reuse param and return values as shown in this example. | |||
;EndIf | |||
;If Msg_FnProdut != 0 | |||
ElseIf Msg_FnProduct != 0 | |||
Set ReturnValue to Param1 * Param2 | |||
Set Msg_FnProduct to 0 | |||
Set Msg_FnProduct_Done to 1 | |||
EndIf | |||
End | |||
</pre> | |||
The calling script can be anything really. You will notice in this script, the calling of the Msg_FnSum and Msg_FnProduct occur in separate time ticks than the Msg_FnSum_Done and Msg_FnProduct_Done code. The entire script is ready every tick but the conditions triggered by the code in the ExampleScript will not have taken place in the first pass. | |||
<pre> | |||
ScriptName Other | |||
Short SumValue | |||
Short ProductValue | |||
Begin GameMode | |||
If SomeCondition == 1 | |||
; Let's call function Sum | |||
Set Example.Param1 to 2 | |||
Set Example.Param2 to 5 | |||
Set Example.Msg_FnSum to 1 | |||
Set SomeCondition to 3 | |||
ElseIf SomeCondition == 2 | |||
; Let's call function Product | |||
Set Example.Param1 to 2 | |||
Set Example.Param2 to 5 | |||
Set Example.Msg_FnProduct to 1 | |||
Set SomeCondition to 4 | |||
EndIf | |||
; Occurs in a later 'tick' than the calling code | |||
If Example.Msg_FnSum_Done == 1 | |||
; Do something with the value | |||
PrintC "Sum is %.0f" Example.ReturnValue ; Requires NVSE | |||
Set Example.ReturnValue to 0 | |||
Set Example.Msg_FnSum_Done to 0 | |||
ElseIf Example.Msg_FnProduct_Done == 1 | |||
; Do something with the value | |||
PrintC "Product is %.0f" Example.ReturnValue ; Requires NVSE | |||
Set Example.ReturnValue to 0 | |||
Set Example.Msg_FnProduct_Done to 0 | |||
EndIf | |||
End | |||
</pre> | |||
= + [[Fallout Script Extender|FOSE]] = | = + [[Fallout Script Extender|FOSE]] = |