What Are They?
Essentially, functions are processes that you declare, enclosed within function tags, like so:
Function MyFunctionName(Parameters)
;code within function, this will be processed when you "call" the function
EndFunction
Event OnDeath(Actor akKiller); akKiller is pre-defined as the actor who killed the actor who died
If akKiller == Game.GetPlayer(); a common use of akKiller - using it to get the value of the actor who killed the actor the script is on
;other stuff
EndIf
EndEvent
Event OnInit()
MyFunction(10, Game.GetPlayer())
EndEvent
Function MyFunction(Int Param1, Actor Param2)
;do stuff
EndFunction
Basic Custom Functions
Function FunctionName(Type Parameter)
;function content
EndFunction
FunctionName(Parameter)
Scriptname MyFuncScript Extends Quest; or whatever extension
;calling the function, must be within an event or function
Event OnInit()
WhatFunction("MyString")
EndEvent
;declaring the function, may be declared anywhere in the script but not within events or other functions
Function WhatFunction(String Param1)
;do functiony stuff
EndFunction
Scriptname ThatScript Extends Quest
{This script is soo much longer than it should be.}
Int Count
Event OnActivate()
If Count == 0
Debug.Notification("Don't use debugs in actual releases, use messages and message properties!"
Count += 1
ElseIf Count == 1
Debug.Notification("Be descriptive with names! This script has an awful name!")
Count += 1
ElseIf Count == 2
Debug.Notification("Other useful stuff...")
Count += 1
EndIf
RegisterForSingleUpdate(1.0)
EndEvent
Event OnUpdate()
If Count == 0
Debug.Notification("Don't use debugs in actual releases, use messages and message properties!"
Count += 1
ElseIf Count == 1
Debug.Notification("Be descriptive with names! This script has an awful name!")
Count += 1
ElseIf Count == 2
Debug.Notification("Other useful stuff...")
Count += 1
EndIf
EndEvent
Scriptname ThatOtherScript Extends Quest
Int Count
Event OnActivate()
CountFunction()
RegisterForSingleUpdate(1.0)
EndEvent
Event OnUpdate()
CountFunction()
EndEvent
Function CountFunction(); no parameters necessary for this type of function
If Count == 0
Debug.Notification("Don't use debugs in actual releases, use messages and message properties!"
Count += 1
ElseIf Count == 1
Debug.Notification("Be descriptive with names! This script has an awful name!")
Count += 1
ElseIf Count == 2
Debug.Notification("Other useful stuff...")
Count += 1
EndIf
EndFunction
Scriptname ThatBetterScript Extends Quest
Int Count
Event OnActivate()
CountFunction(1)
RegisterForSingleUpdate(1.0)
EndEvent
Event OnUpdate()
CountFunction(2)
EndEvent
Function CountFunction(Int PrintWhat); parameters we can set so we can define what the function will do
If PrintWhat == 1
If Count == 0
Debug.Notification("PrintWhat is 1. Don't use debugs in actual releases, use messages and message properties!"
Count += 1
ElseIf Count == 1
Debug.Notification("Be descriptive with names! This script has an awful name!")
Count += 1
ElseIf Count == 2
Debug.Notification("Other useful stuff...")
Count += 1
EndIf
ElseIf PrintWhat == 2
If Count == 0
Debug.Notification("PrintWhat is 2. This means that we called CountFunction with its parameter as 2."
Count += 1
ElseIf Count == 1
Debug.Notification("See how this could be useful? While it still processes the same amount of code, it's much cleaner.")
Count += 1
ElseIf Count == 2
Debug.Notification("I recommend putting functions at the bottom of your code. Much cleaner.")
Count += 1
EndIf
EndFunction
Returning Functions
ReturnType Function FunctionName(Parameters)
;Function content, and return
EndFunction
Bool Function IsActorCool(Actor WhatActor);tell the function which actor you're calling the function on
If WhatActor.GetRelationShipRank(Game.GetPlayer()) == 4
;WhatActor likes the player
Return True
Else
Return False
EndFunction
Bool Function LightSwitch(ObjectReference[] kArray)
Int kIndex = kArray.Length
If GameHour.GetValueInt() >= 22 && GameHour.GetValueInt() < 6; if the time is bigger than or equal to 10 PM and less than 6 - 24 hour clock
While kIndex; while we haven't counted through the entire array
kArray[kIndex].Enable(true); enable the entry in the array, fading in
kIndex -= 1; move onto the next entry
EndWhile
Return False; it was nighttime
Else; time is bigger than or equal to 6, and less than 10 - 24 hour clock
While kIndex; while we haven't counted through the entire array
kArray[kIndex].Disable(true); disable the current entry in the array, fading out
kIndex -= 1; move onto the next entry
EndWhile
Return True; it was day time
EndIf
EndFunction
Global and Native Functions
ReturnType FunctionName(Parameters) native
;or, if it's a non-returning function
FunctionName(Parameters) native
Then, there is a global function. A global function is one that can be called from any script, without having to define a property or cast to the script the function is in. For example, EnablePlayerControls is a global function, as is RandomInt. However, you do have to prefix global functions with the name of the script they came from, and a dot when calling them, like so:
;how you call EnablePlayerControls
Game.EnablePlayerControls()
;how you call EnablePlayerControls, with Game imported
Import Game
EnablePlayerControls()
ReturnType FunctionName(Parameters) global; you can tack on a native flag after the global, but it wouldn't have a function body then
;function content
EndFunction
- Add GameHour as a parameter you have to define.
- Use GetFormFromFile within the function. We're not going to get into this. It's not a method I would recommend, given the fact that this is a pretty simple function.
So here's how we would achieve method one - just add a very simple parameter so the function declaration looks like this:
Bool Function LightSwitch(ObjectReference[] kArray, GlobalVariable kGameHour) global
GlobalVariable Property GameHour Auto; fill this property
ObjectReference[] Property MyObjects Auto; fill this array property
Event OnInit()
LightSwitch(MyObjects[], GameHour)
EndEvent
Bool Function LightSwitch(ObjectReference[] kArray, GlobalVariable kGameHour) global
Int kIndex = kArray.Length
If kGameHour.GetValueInt() >= 22 && kGameHour.GetValueInt() < 6; if the time is bigger than or equal to 10 PM and less than 6 - 24 hour clock
While kIndex; while we haven't counted through the entire array
kArray[kIndex].Enable(true); enable the entry in the array, fading in
kIndex -= 1; move onto the next entry
EndWhile
Return False; it was nighttime
Else; time is bigger than or equal to 6, and less than 10 - 24 hour clock
While kIndex; while we haven't counted through the entire array
kArray[kIndex].Disable(true); disable the current entry in the array, fading out
kIndex -= 1; move onto the next entry
EndWhile
Return True; it was day time
EndIf
EndFunction
Function Design Tips
Quest Property MyQuest Auto
Int Property StageToSet Auto
Event OnActivate()
MyQuest.SetStage(StageToSet)
EndEvent
Quest Property MyQuest Auto
Event OnActivate()
MyQuest.SetStage(10)
EndEvent
The same applies to functions, especially global ones that you intend to use in lots of places. So, for example, I'd probably change LightSwitch to a far more versatile version that looks like this:
Bool Function LightSwitch(ObjectReference[] kArray, GlobalVariable kGameTime, Int LowTime, Int HighTime) global
Int kIndex = kArray.Length
If kGameTime.GetValueInt() >= HighTime && kGameTime.GetValueInt() < LowTime; if the time is bigger than or equal to hightime and less than lowtime - 24 hour clock
While kIndex; while we haven't counted through the entire array
kArray[kIndex].Enable(true); enable the entry in the array, fading in
kIndex -= 1; move onto the next entry
EndWhile
Return False; it was nighttime
Else; time is bigger than or equal to lowtime, and less than hightime - 24 hour clock
While kIndex; while we haven't counted through the entire array
kArray[kIndex].Disable(true); disable the current entry in the array, fading out
kIndex -= 1; move onto the next entry
EndWhile
Return True; it was day time
EndIf
EndFunction
There's one last thing: You can have default parameters. For example, the Disable function has a default parameter:
Function Disable(bool abFadeOut = False) native
; the parameter checks if you want the object to fade out of existence rather than just blink out
MyObject.Disable()
MyObject.Disable(false)
ReturnType FunctionName(Parameter = DefaultValue); tack on the global and native flags here if you want
;if there is no return type
FunctionName(Parameter = DefaultValue)
Bool Function LightSwitch(ObjectReference[] kArray, GlobalVariable kGameTime, Int LowTime = 6, Int HighTime = 10)
Some Common Functions
This is a DisableArray function. It will disable all objects an array of your choice, and fade them out depending on what you input in place of kFadeObject, though by default it won't fade out. It's a global function, though you can delete the global flag, so whichever script you put this function in will be the prefix to the function (i.e. you need to call it like OwningScriptName.DisableArray(MyArray, true) to disable all the objects in MyArray, fading them out.) You could also replace all instances where I used Disable and instead replace it with Enable, if you want this to enable objects rather than disable them. | Function DisableArray(ObjectReference[] kArray, bool kFadeObject = false) global |
This is a SetHostile function. While you can usually use StartCombat, or SetRelationshipRank, sometimes individually they won't work. So SetHostile will set all of these to something that should make an actor target and attack another actor. This is also a global function, so whichever script you put this function in will be the prefix to the function (i.e. you need to call it like OwningScriptName.SetHostile(Actor1, Actor2) to make Actor1 hostile towards Actor2). | Function SetHostile(Actor kActor, Actor kTarget) global |