Function Reference: Difference between revisions
Jump to navigation
Jump to search
Added "Notes on using Parent" section.
imported>SarthesArai m (→Function Header: typo) |
imported>Chesko (Added "Notes on using Parent" section.) |
||
| Line 199: | Line 199: | ||
: auiCount, for instance, is an unsigned integer argument while asFileName is a string argument. Note that 'p' and 'k', for Papyrus' purposes, are interchangeable and that Papyrus does not actually have pointers. | : auiCount, for instance, is an unsigned integer argument while asFileName is a string argument. Note that 'p' and 'k', for Papyrus' purposes, are interchangeable and that Papyrus does not actually have pointers. | ||
== 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. | |||
Consider the following 4 scripts: | |||
<source 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 | |||
</source> | |||
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 <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. | |||
So, the execution order of the above is actually: | |||
<source 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() | |||
</source> | |||
== Bugs == | == Bugs == | ||