the Sim Settlements forums!

Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Default Argument Values are Fake

kinggath

Well-Known Member
Staff member
Administrator
Moderator
Verified Builder
Messages
4,068
...well not exactly... but sort of.

TLDR: If you change a function, recompile all of your scripts that might be calling it.

Default arguments don't actually work how you'd expect. Instead of being available at runtime, they are filled in calling functions at compile time. This can end up causing all sorts of problems for scripts that are being publicly used, or even those you're using yourself.

Imagine you have this function:

Code:
Function SomeFunction(Int aiNumber = 1)
    if(aiNumber == 1)
         Debug.MessageBox("We got a 1!")
    else
         Debug.MessageBox("Womp, womp.... we got nothing good.")
    endif
EndFunction


Then you have another script that calls SomeFunction

Code:
Function MyOtherFunction()
      SomeFunction()
EndFunction

When you compile the second script it becomes:

Code:
Function MyOtherFunction()
       SomeFunction(1)
EndFunction

(well really it becomes a bunch of illegible hex, but this is what it would look like if you decompiled it again)

If you had compiled it before you added the default value to SomeFunction, then it would still look exactly like you wrote it - and can actually fire errors about not enough arguments. This can cause any call involving that function to be terminated. So it's pretty important you recompile everything after changing a function... even if you added a default value to your new argument.

This is something that took forever to click for me, and I still often forget it - much to our testing team's dismay who often have to wait for 3 or 4 alphas of a patch before I remember to recompile everything.
 
Last edited:

1000101

Lurking in the bushes
Verified Builder
Messages
108
Everything kinggath said is also true of every compiled scripting/programming language and is not limited to Papyrus; this is an addendum to what he has provided.

Default parameter values only have meaning at compile time to the code calling the function and never have meaning to the function with default parameter values itself at any time. That is, the default value of parameters are never encoded in the compiled function prototype, they only exist in the source code.

Using kinggaths examples above, "SomeFunction" function will be stripped of the default value and only the parameter types and names will exist in the compiled script. Without the source to the script with the default parameter value the caller must explicitly provide all parameters and values.

eg:
Code:
Function MyOtherFunction()
     SomeFunction() // Perfectly acceptable if the source to SomeFunction is available
     SomeFunction( 1 ) // Required if the source to SomeFunction is not available
EndFunction
 
Top