Function Reference: Difference between revisions

Jump to navigation Jump to search
m
Convert source tags to syntaxhighlight
(Added a link to "List of Papyrus Functions" to the Function Reference page. Should have been here all along)
m (Convert source tags to syntaxhighlight)
 
Line 34: Line 34:
A simple example: imagine a function invoked on an object that takes a parameter that points to another object of the same class: '''car1.Ram(car2)''' (which should ram your car into another car). Obviously, the car cannot ram itself. By using the "Self" variable, we can ensure that car2 is not equal to Self and prevent damaging or unnecessary code from executing:
A simple example: imagine a function invoked on an object that takes a parameter that points to another object of the same class: '''car1.Ram(car2)''' (which should ram your car into another car). Obviously, the car cannot ram itself. By using the "Self" variable, we can ensure that car2 is not equal to Self and prevent damaging or unnecessary code from executing:
<br>
<br>
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
;The function is called on one car like this.
;The function is called on one car like this.
Car1.Ram(car2)
Car1.Ram(car2)
Line 49: Line 49:
  endIf
  endIf
endFunction
endFunction
</source>
</syntaxhighlight>
<br>
<br>
Another example is a script that is trying to release an object it no longer needs so that the reference no longer consumes memory. However due to the its complexities, the script may accidentally end up referencing the same object. A reference to an object that was expected to be released is called a dangling pointer, and it can cause crashes or other problems as the application progresses. By comparing newObject to Self (''if(newObject != Self)''), we can be sure that when the old object is released it will be properly replaced by newObject.
Another example is a script that is trying to release an object it no longer needs so that the reference no longer consumes memory. However due to the its complexities, the script may accidentally end up referencing the same object. A reference to an object that was expected to be released is called a dangling pointer, and it can cause crashes or other problems as the application progresses. By comparing newObject to Self (''if(newObject != Self)''), we can be sure that when the old object is released it will be properly replaced by newObject.
Line 68: Line 68:


=== Examples ===
=== Examples ===
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
; A simple function that adds the two values together and returns the result
; A simple function that adds the two values together and returns the result
; Global, because it doesn't need a self variable
; Global, because it doesn't need a self variable
Line 74: Line 74:
   return a + b
   return a + b
endFunction
endFunction
</source>
</syntaxhighlight>
<br>
<br>
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
; A function that increments a value on this script by the specified amount.
; A function that increments a value on this script by the specified amount.
; The amount has a default value of 1 (so the caller doesn't have to pass it)
; The amount has a default value of 1 (so the caller doesn't have to pass it)
Line 82: Line 82:
   myValue += howMuch
   myValue += howMuch
endFunction
endFunction
</source>
</syntaxhighlight>


== Special Variables ==
== Special Variables ==
Line 90: Line 90:


=== Examples ===
=== Examples ===
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
; Pass our self off to another function
; Pass our self off to another function
SomeObject.OtherFunction(self)
SomeObject.OtherFunction(self)
</source>
</syntaxhighlight>
<br>
<br>
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
; Call the parent's version of DoStuff, ignoring our local definition
; Call the parent's version of DoStuff, ignoring our local definition
Parent.DoStuff()
Parent.DoStuff()
</source>
</syntaxhighlight>


== Calling Functions ==
== Calling Functions ==
Line 119: Line 119:


=== Examples ===
=== Examples ===
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
; Call the function: MyFunction(int a, int b) and get the result and put it in x
; Call the function: MyFunction(int a, int b) and get the result and put it in x
x = MyFunction(1, 2)
x = MyFunction(1, 2)
</source>
</syntaxhighlight>
<br>
<br>
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
; Call the function DefaultFunction(float a, float b, float c = 0.0, float d = 1.0) on MyObject,
; Call the function DefaultFunction(float a, float b, float c = 0.0, float d = 1.0) on MyObject,
; but only pass in the first three parameters
; but only pass in the first three parameters
MyObject.DefaultFunction(4.0, 2.0, 1.0)
MyObject.DefaultFunction(4.0, 2.0, 1.0)
</source>
</syntaxhighlight>
<br>
<br>
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
; Call the function DefaultFunction(float a, float b, float c = 0.0, float d = 1.0), but specify
; Call the function DefaultFunction(float a, float b, float c = 0.0, float d = 1.0), but specify
; argument d out of order because we want c to keep the default it has
; argument d out of order because we want c to keep the default it has
DefaultFunction(5.0, 2.4, d = 2.0)
DefaultFunction(5.0, 2.4, d = 2.0)
</source>
</syntaxhighlight>
<br>
<br>
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
; Call the global function MyGlobal() in the Utility script
; Call the global function MyGlobal() in the Utility script
Utility.MyGlobal()
Utility.MyGlobal()
</source>
</syntaxhighlight>


<br>
<br>
Line 152: Line 152:


<br>'''Example:'''
<br>'''Example:'''
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
myRefAliasScript property refScript auto      ;fill this property with your quest in the CK editor
myRefAliasScript property refScript auto      ;fill this property with your quest in the CK editor


Line 158: Line 158:
refScript.myRefAliasScriptFunction01()
refScript.myRefAliasScriptFunction01()
refScript.myRefAliasScriptFunction02()        ;access two functions that are in the reference alias script
refScript.myRefAliasScriptFunction02()        ;access two functions that are in the reference alias script
</source>
</syntaxhighlight>




Line 169: Line 169:
<br>'''Example One:'''<br>
<br>'''Example One:'''<br>
''Access the script functions on a created ObjectReference:''
''Access the script functions on a created ObjectReference:''
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
ObjectReference Pony1 = PlayerRef.PlaceAtMe(myCustomPony)  ;create two ponies. Let us assume that myCustomPony is a base Actor that has 'PonyScript.psc' attached to it.
ObjectReference Pony1 = PlayerRef.PlaceAtMe(myCustomPony)  ;create two ponies. Let us assume that myCustomPony is a base Actor that has 'PonyScript.psc' attached to it.
ObjectReference Pony2 = PlayerRef.PlaceAtMe(myCustomPony)
ObjectReference Pony2 = PlayerRef.PlaceAtMe(myCustomPony)
Line 176: Line 176:
Pony1Script.groomPony()                                    ;groom pony one, using the groomPony() function in (its instance of) PonyScript
Pony1Script.groomPony()                                    ;groom pony one, using the groomPony() function in (its instance of) PonyScript
Pony2Script.killPony()                                      ;kill pony two, using the killPony() function in (its instance of) PonyScript
Pony2Script.killPony()                                      ;kill pony two, using the killPony() function in (its instance of) PonyScript
</source>
</syntaxhighlight>


<br>Note: sometimes you will have to cast the object with ScriptB attached to it before declaring the variable will work. Let us say that Pony1 was an Actor instead of an ObjectReference, for instance. Since PonyScript extends ObjectReference, we first need to cast Pony1 ''as an ObjectReference'' before we can cast it ''as a PonyScript'', otherwise the compiler will throw out errors:
<br>Note: sometimes you will have to cast the object with ScriptB attached to it before declaring the variable will work. Let us say that Pony1 was an Actor instead of an ObjectReference, for instance. Since PonyScript extends ObjectReference, we first need to cast Pony1 ''as an ObjectReference'' before we can cast it ''as a PonyScript'', otherwise the compiler will throw out errors:


<source lang="papyrus">
<syntaxhighlight lang="papyrus">
Actor Pony1 = somePony
Actor Pony1 = somePony
PonyScript Pony1Script = (Pony1 as ObjectReference) as PonyScript
PonyScript Pony1Script = (Pony1 as ObjectReference) as PonyScript
</source><br><br>
</syntaxhighlight><br><br>


'''Example Two:'''<br>
'''Example Two:'''<br>
''Find an Actor's linked reference, and conditionally access the script functions on that reference:''
''Find an Actor's linked reference, and conditionally access the script functions on that reference:''
<source lang="papyrus">
<syntaxhighlight lang="papyrus">
ObjectReference possibleTable1 = Actor1.GetLinkedRef()
ObjectReference possibleTable1 = Actor1.GetLinkedRef()
if (possibleTable1 as Furniture) == magicTable                        ;is the actor linked to a magicTable?
if (possibleTable1 as Furniture) == magicTable                        ;is the actor linked to a magicTable?
Line 193: Line 193:
   TableScript1.explode()                                              ;if so, use the magicTable's script function to make the magicTable explode!
   TableScript1.explode()                                              ;if so, use the magicTable's script function to make the magicTable explode!
endif
endif
</source>
</syntaxhighlight>
<br><br>
<br><br>
== Notes ==
== Notes ==
Line 207: Line 207:
Consider the following 4 scripts:
Consider the following 4 scripts:


<source lang="papyrus">
<syntaxhighlight lang="papyrus">
ScriptName ScriptA extends ObjectReference
ScriptName ScriptA extends ObjectReference


Line 235: Line 235:
endFunction
endFunction


</source>
</syntaxhighlight>


Suppose we call '''ScriptD.foo()'''. We might expect the output to be,
Suppose we call '''ScriptD.foo()'''. We might expect the output to be,
Line 256: Line 256:
So, the execution order of the above is actually:
So, the execution order of the above is actually:


<source lang="papyrus">
<syntaxhighlight lang="papyrus">
ScriptD.foo()
ScriptD.foo()
     parent.foo()
     parent.foo()
Line 264: Line 264:
     parent.foo()
     parent.foo()
ScriptA.foo()
ScriptA.foo()
</source>
</syntaxhighlight>




As a final example, if you had scripts in the following hierarchy...
As a final example, if you had scripts in the following hierarchy...


<source lang="papyrus">
<syntaxhighlight lang="papyrus">
ScriptName Script1 extends ObjectReference
ScriptName Script1 extends ObjectReference
function foo()
function foo()
Line 291: Line 291:
     parent.foo()
     parent.foo()
endFunction
endFunction
</source>
</syntaxhighlight>


...'''foo()''' would be called 10 times; Script10's version once, and Script1's version 9 times.
...'''foo()''' would be called 10 times; Script10's version once, and Script1's version 9 times.

Navigation menu