Editing
Function Reference
(section)
Jump to navigation
Jump to search
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.
Anti-spam check. Do
not
fill this in!
== Function Definition == <function> ::= <function header> [<function block> 'endFunction'] Function headers must always be followed by a block and an "EndFunction" [[Keyword Reference|keyword]], unless they are native functions (which are exposed by the game). === Function Header === <function header> ::= [<type>] 'Function' <identifier> '(' [<parameters>] ')' ('global' | 'native')* <flags>* A function header starts (optionally) with the return type of the function, and is then followed by the name of the function, its parameters (if any), and any modifiers and flags. The [[Identifier Reference|identifier]] used to name the function cannot conflict with any other function in the current script. If the identifier matches a function in the parent script, then the return type and parameters must match the parent script's version of the function - and the function will override the parent's function. The "Global" flag indicates a function that does not actually run on an in-game object, and has no "Self" variable. The "Native" flag indicates a function that does not have a function body, because the function is implemented by the game itself. If you add the native flag to a function the game does not expose, the compiler won't complain, but the game will error at you. The same flag cannot be specified more than once. <br><br> ::{|style="border-collapse: separate; border-spacing: 0; border-width: 1px; border-style: solid; border-color: #000; padding: 0" |- |style="border-style: solid; border-width: 0"|[[Image:InDepth.jpg|48px]] |style="border-style: solid; border-width: 0"| <br> The "self" variable is a natural consequence of object oriented programming. Object oriented programming relies on the concept of an abstract ''class'' and an ''instance'' of that class (An instance may also be called an ''object''.) The relationship between a class and an instance is similar to the difference between a blueprint of a car and the actual car that is made from that blueprint. A single blueprint may be the basis for many cars. The blueprint is an abstraction. You cannot interact act with it. For example, you can't turn on the radio of the car in the blueprint. It's a piece of paper. In order to turn on the radio, you need to interact with the actual car. The blueprint is the class, and the car is the object. The way you find out which object is being acted upon by a function is to refer to the self. For example, ''self.TurnOn(theRadio)'' will turn on the radio of the car that calls that function, and only that one car. All functions are actually resolved by the compiler to use the '''this/self''' pointer. If a script changes an integer variable '''someInt''' to 5, what it looks like to us is: '''someInt = 5'''. Internally, that variable is labeled with, and considered a component of, the object that calls it, which could be written as '''self.someInt = 5''' 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> <syntaxhighlight lang="papyrus"> ;The function is called on one car like this. Car1.Ram(car2) ;Then we write the function: function Ram(object someVehicle) ;if someVehicle is the same as the car that the function was initially called on, ;kill the function and do not proceed to ram it. if(someVehicle == self) return else self.Moveto(someVehicle) endIf endFunction </syntaxhighlight> <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. |} === Parameters === <parameters> ::= <parameter> (',' <parameter>)* <parameter> ::= <type> <identifier> ['=' <constant>] The parameter list is a comma-separated list of types and identifiers that indicate the various parameters that a function takes. Each parameter may be optionally followed by an equals sign and a constant, which indicates that the parameter has a default value. If a parameter has a default value, every parameter after it must also have a default value. Parameters are essentially [[Variable Reference|variables]] the function has access to that the caller gives initial values to. === Function Block === <function block> ::= <statement>* The function block contains zero or more [[Statement Reference|statements]]. This performs the actual work of the function. === Examples === <syntaxhighlight lang="papyrus"> ; A simple function that adds the two values together and returns the result ; Global, because it doesn't need a self variable int Function AddTwo(int a, int b) global return a + b endFunction </syntaxhighlight> <br> <syntaxhighlight lang="papyrus"> ; 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) Function IncrementValue(int howMuch = 1) myValue += howMuch endFunction </syntaxhighlight>
Summary:
Please note that all contributions to the CreationKit Wiki are considered to be released under the Creative Commons Attribution-ShareAlike (see
CreationKit: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)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
Edit source
View history
More
Search
Navigation
Landing page
Tutorials
Game Systems
Editor Reference
Scripting Reference
Recent changes
Help
Tools
What links here
Related changes
Special pages
Page information
Page values