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!
== Notes on using Parent == A quirk of using the special '''parent''' variable that might not be immediately obvious is the behavior when functions are called when the same function is present in some scripts in the hierarchy, but not others, and the parent version is called within the function. Consider the following 4 scripts: <syntaxhighlight lang="papyrus"> ScriptName ScriptA extends ObjectReference function foo() debug.trace("Calling foo on ScriptA") endFunction ScriptName ScriptB extends ScriptA function foo() debug.trace("Calling foo on ScriptB") parent.foo() endFunction ScriptName ScriptC extends ScriptB ; ScriptC doesn't have function foo() declared! ScriptName ScriptD extends ScriptC function foo() debug.trace("Calling foo on ScriptD") parent.foo() endFunction </syntaxhighlight> Suppose we call '''ScriptD.foo()'''. We might expect the output to be, [01/01/2016 - 11:36:28AM] Calling foo on ScriptD [01/01/2016 - 11:36:28AM] Calling foo on ScriptB [01/01/2016 - 11:36:28AM] Calling foo on ScriptA However, this is <b>not</b> what will happen. Instead, we see the following: [01/01/2016 - 11:36:28AM] Calling foo on ScriptD [01/01/2016 - 11:36:28AM] Calling foo on ScriptB [01/01/2016 - 11:36:28AM] Calling foo on ScriptB [01/01/2016 - 11:36:28AM] Calling foo on ScriptA It appears that '''foo()''' on ScriptB is called <b>twice!</b> The reason for this is because '''parent''' attempts to call the function "as" a script <b>exactly one level higher</b> in the inheritance hierarchy, and does not "skip" a level in the hierarchy when the function isn't found. As we know, if a function isn't found on a child script, parents in the hierarchy are checked for that function instead. If the function is found on another script higher in the hierarchy, it is called, but "as" the '''direct hierarchical parent''', not as the script on which the function is found! In this case, since ScriptC doesn't define '''foo()''', it looks at its parent in the hierarchy and calls ScriptB's version of '''foo()''', but "as" ScriptC. Since ScriptB's '''foo()''' calls '''parent.foo()''', it moves up the hierarchy one level higher and calls the same function again, only "as" ScriptB this time. This leads to the same function being called twice. So, the execution order of the above is actually: <syntaxhighlight lang="papyrus"> ScriptD.foo() parent.foo() ScriptC.foo() ; ScriptC couldn't find foo(), so it calls ScriptB.foo()'s version instead. parent.foo() ScriptB.foo() ; Now we actually are calling foo() as ScriptB parent.foo() ScriptA.foo() </syntaxhighlight> As a final example, if you had scripts in the following hierarchy... <syntaxhighlight lang="papyrus"> ScriptName Script1 extends ObjectReference function foo() debug.trace("Calling foo on Script1") parent.foo() endFunction ; The following intermediary scripts don't define foo(). ScriptName Script2 extends Script1 ScriptName Script3 extends Script2 ScriptName Script4 extends Script3 ScriptName Script5 extends Script4 ScriptName Script6 extends Script5 ScriptName Script7 extends Script6 ScriptName Script8 extends Script7 ScriptName Script9 extends Script8 ScriptName Script10 extends Script9 function foo() debug.trace("Calling foo on Script10") parent.foo() endFunction </syntaxhighlight> ...'''foo()''' would be called 10 times; Script10's version once, and Script1's version 9 times.
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