Papyrus functions with optional arguments

Post » Thu Jul 04, 2013 11:22 am

I'm having trouble setting up a Papryus function with an optional argument. If I say something like this:

Function RentRoom(DialogueGenericScript pQuestScript, Int Days = 1)

The function won't fire unless the second variable is passed, even though it is supposed to have a default value. If I pass the second variable, everything works fine. Without it, nothing happens and the log says "incorrect number of arguments passed." I get the same result without the spaces, like so:

Function RentRoom(DialogueGenericScript pQuestScript, Int Days=1)

Is there anyone who can tell me what I'm missing? I'm sure it's some obvious (after-the-fact) thing.

User avatar
Steve Smith
 
Posts: 3540
Joined: Sat Jun 30, 2007 10:47 am

Post » Thu Jul 04, 2013 3:44 pm

Post your whole script, maybe something else is going on? I just tested this and it compiled no problem.

Scriptname _test_testscript extends ActiveMagicEffectObjectReference Property Gold Auto   Event OnEffectStart(Actor akTarget, Actor akCaster)    DoSomething(akTarget)    DoSomething(akTarget, 50)endEventFunction DoSomething(Actor akTarget, int aiValue = http://forums.bethsoft.com/topic/1465300-papyrus-functions-with-optional-arguments/1)    akTarget.AddItem(Gold, aiValue)endFunction
User avatar
Roisan Sweeney
 
Posts: 3462
Joined: Sun Aug 13, 2006 8:28 pm

Post » Thu Jul 04, 2013 8:26 am

It compiles fine, that's not the issue. Try actually calling the function in the game without aiValue. Nothing happens, and the Papyrus log complains that the incorrect number of arguments was passed.

Full Script:

Function MyFunction(Int Variable1, Int Variable2 = 1)

Debug.Notification("Hello World")

EndFunction

Works if Variable2 is passed. Doesn't work otherwise.

User avatar
Stacey Mason
 
Posts: 3350
Joined: Wed Nov 08, 2006 6:18 am

Post » Thu Jul 04, 2013 10:39 am

Try
Function MyFunction(Int Variable1, Int Variable2) Global	Debug.Notification("Hello World")EndFunction
...or...
Function MyFunction(Int Variable1 = 0, Int Variable2 = 1)	Debug.Notification("Hello World")EndFunction
?

Also, seeing the whole script would be potentially useful. What is pQuestScript, for instance?
User avatar
David John Hunter
 
Posts: 3376
Joined: Sun May 13, 2007 8:24 am

Post » Thu Jul 04, 2013 5:23 am

Hi, Justin.

With both options, I stil got this error in the Papryus log: "Error: Incorrect number of arguments passed. Expected 2, got 1."

The whole script is not really useful. I've tried optional arguments on a number of different functions, including the hello world function above. Right now, I'm just working with hello world to keep it simple. If I can get it working with the function I posted above, that would be enough. I can tell it is set up correctly aside from the optional argument, because if I pass only one argument I get the debug notification just fine.

Has anyone actually gotten optional arguments to work with Papyrus (other than optional parameters on the native functions that are called in Game, Utility, etc.)?

User avatar
Ross Zombie
 
Posts: 3328
Joined: Wed Jul 11, 2007 5:40 pm

Post » Thu Jul 04, 2013 10:01 am

I'm going to hazard a guess and you're trying to call the function from a separate script.

Optional arguments are baked into the caller (they're not really optional once compiled) so if you add optional arguments to a function called by another script the game will complain until you've re-compiled the other script so the proper number of arguments are baked in. (Note that this also means you need to recompile scripts when you change what the default value is, because the old one will still be compiled into the other scripts)

Please note that when reporting errors you should paste the entire error to the forums, not just the text as that will also include a full call stack detailing the problem. Simply paraphrasing the error message will result in fustration and wasted time as everyone tries to figure out what really happened :smile: (Same reason why you should always post the entire script rather then just pieces of it)
User avatar
Rudi Carter
 
Posts: 3365
Joined: Fri Sep 01, 2006 11:09 pm

Post » Thu Jul 04, 2013 7:33 am

"Optional arguments are baked into the caller (they're not really optional once compiled) so if you add optional arguments to a function called by another script the game will complain until you've re-compiled the other script so the proper number of arguments are baked in."

Could you elaborate on this a bit more? I don't fully understand. Does this mean it is not possible with Papyrus to have a function with optional arguments if the function is called from another script (such as a dialog fragment)? Or are you trying to say that there is some way to do it?

The situation is that there are two mods (one of them is mine) that have modified the rentroomscript. My script adds one function--FreeRoom()--to the bottom of the script to avoid messing with the vanilla rentroom function but piggyback on certain other things (like knowing which bed to use, and already being attached to all the innkeeper actors). The freeroom() function is called from a dialog fragment, just like the rentroom() function.

The other mod makes more extensive changes to the rentroom script, and to the dialog fragment that calls the rentroom() function in the vanilla game. An easy way to make the two compatible would be to add my standalone function to that other script, recompile, and offer that as a patch to use both mods--and that works fine. But I prefer to handle compatibility behind the scenes like I do with other mods, so the separate patch bugs me.

It's easy enough to use getformfromfile() to check if the other mod is loaded. Then, within the various functions, I can go in one direction (vanilla) if the mod is not loaded and another direction if it is. The ONLY problem is that I cannot get one of the functions, the rentroom() function, to work because one of the changes the other mod makes is adding another parameter to the rentroom() function. I gave the second one a default value of 0, but when I test it, if I have the other mod installed (both variables are passed) everything works fine. Without the other mod installed, I get the Incorrect number of arguments passed error.

Also, the hello world function above is the actual function I am using to test. I don't know how to post a full script with the formatting and such.

User avatar
Maeva
 
Posts: 3349
Joined: Mon Mar 26, 2007 11:27 pm

Post » Thu Jul 04, 2013 1:06 am

Basically Papyrus doesn't have optional function arguments - it's all done via compiler tricks. If you create a function Foo with a single optional parameter with default value 3, then you can call it like this:
Foo()
However, the compiler will turn it into this:
Foo(3)
This means that if you change the arguments for the function or the default values, after recompiling the script in which it is defined you will also need to recompile every script in which that function is called in order to ensure the correct arguments and default values are used.

If you press the switch icon in the upper left of the reply box, you can turn off fancy formatting and indentation will no longer be stripped from your scripts.

Cipscis
User avatar
Mario Alcantar
 
Posts: 3416
Joined: Sat Aug 18, 2007 8:26 am

Post » Thu Jul 04, 2013 11:37 am

A point that is stressed so often..

User avatar
Alisia Lisha
 
Posts: 3480
Joined: Tue Dec 05, 2006 8:52 pm

Post » Thu Jul 04, 2013 11:59 am

And has an entire http://www.gamesas.com/topic/1347469-how-to-ask-for-scripting-help/ dedicated to it.

Cipscis
User avatar
Soraya Davy
 
Posts: 3377
Joined: Sat Aug 05, 2006 10:53 pm

Post » Thu Jul 04, 2013 1:46 am

I appreciate the responses, but I must say that I really don't understand the multiple posts about the error code. Absolutely nothing in the error log other than what I posted was relevant. It seems like a pet peeve here, but I personally have a pet peeve with extraneous information. Oh, well, moving on.

User avatar
Javaun Thompson
 
Posts: 3397
Joined: Fri Sep 21, 2007 10:28 am


Return to V - Skyrim