Jump to content

Beginner Scripting Help: Function w/ Optional Parameters


Recommended Posts

Hi everybody,

 

Over the last few months or so, I've experimented a bit with modifying scripts of various mods that did not do exactly what I wanted them to do. For the most part, this has worked fine. I followed some guides on how to setup Skyrim SE/Creation Kit, and after that I've simply used the `Papyrus Script Manager` of the Creation Kit to compile the individual script I've modified. I will add that I am running everything through Mod Organizer 2.

 

However, there is one thing that I have not been able to figure that is bothering me, which none of the tutorials I've seen seem to mention: I cannot compile scripts that uses functions from other scripts with optional arguments if the optional argument is not specified at the callsite (or compile scripts that references such scripts!). It will simply complain that I must specify the optional argument (which everytime I've checked has been specified in the original function signature!).

 

If you're a modder, you've probably already figured out where my efforts has hit a snag: Everytime I want to compile a file that through it's dependencies references any MCM menu script I get issues since these scripts uses functions that has optional arguments heavily.

 

Until now, I've simply solved it by stripping out all code from the MCM menu configuration scripts leaving only the method signatures and variables. Doing so fixes the problem and has not really been an issue since I have no intention of compiling those MCM menu scripts anyway. However, having to do this so often is really a pain and it prevents me from modifying such scripts entirely.

 

Therefore I would like to ask: What am I doing wrong? I must be doing something fundamentally wrong since I cannot find anyone else asking this question when searching.

  • Am I using the wrong tooling?
  • Am I missing some extension/mod to the Creation Kit?
  • Should I use a different compiler than the one provided with Creation Kit?
Edited by leakim
Link to comment

A proper example to explain what you mean would probably be best. I assume by optional parameters you mean default initialized ones

 

Default initialized parameters dont need to be initialized by the caller as the callee already offers a paramter which should  suffice most needs or to signify that the parameter is made redundant, for example

 

Function f(a, b = 0)

 

can be called by the caller either as

 

f(1, 1)

 

or

 

f(1)

 

In the latter case, the "b" isnt optional but will be initialized as 0, as the callee offers the default initialization of this second parameter if missing, ie the compiler will compile this second call for you as

 

f(1, 0)

 

Thats all the magic that happens there

Link to comment
38 minutes ago, Scrab said:

A proper example to explain what you mean would probably be best. I assume by optional parameters you mean default initialized ones

 

Default initialized parameters dont need to be initialized by the caller as the callee already offers a paramter which should  suffice most needs or to signify that the parameter is made redundant, for example

 

Function f(a, b = 0)

 

can be called by the caller either as

 

f(1, 1)

 

or

 

f(1)

 

In the latter case, the "b" isnt optional but will be initialized as 0, as the callee offers the default initialization of this second parameter if missing, ie the compiler will compile this second call for you as

 

f(1, 0)

 

Thats all the magic that happens there

 

The problem is I cannot compile the code that includes `f(1)`, while I can easily compile the code `f(1, 1)`, even though the function signature is `Function f(a, b = 0)`.

 

Since you're asking for an example, I shall oblige:

 

Here's me trying to compile SlaveTats 1.3.7 file SlaveTatsMCMMenu.psc:

 

When doing this, one of the errors I get is the following: 

 

...\Skyrim Special Edition\Data\Scripts\Source\SlaveTatsMCMMenu.psc(173,8): argument a_acceptlabel is not specified and has no default value

 

This error refers to line:

 

ShowMessage("Warning: This version of SlaveTats uses version 3 or greater of the JContainers API. You do not have JContainers installed.", false)

 

The `ShowMessage` function is part of the `SKI_ConfigBase.psc` file from SkyUI, and if I open that file I have the following function:

 

bool function ShowMessage(string a_message, bool a_withCancel = true, string a_acceptLabel = "$Accept", string a_cancelLabel = "$Cancel")
	{Shows a message dialog and waits until the user has closed it}
	Guard()
endFunction

 

As you can see, the original function signature indicates that the accept label is optional (or default initialized), but I cannot compile a script that uses this function without providing the default initialized arguments.

 

What is going on here?

Link to comment

Most likely you got a source file somewhere which is overwriting the one you wish to use

 

The default compiler doesnt make mistakes like that, so you either use a custom one (which you shouldnt be doing as a beginner and Im unaware of any that actually work properly) or you have file conflicts without realizing

If you compile through the CK you should be able to see which file the CK uses by using on the top options, one of the rightern menus has a sub menu on the  giving you 2 options about Papyrus Scripts, one to compile and one to manage. You can use one of them to view the Source of the Script youre compiling

Link to comment
10 minutes ago, Scrab said:

Most likely you got a source file somewhere which is overwriting the one you wish to use

 

The default compiler doesnt make mistakes like that, so you either use a custom one (which you shouldnt be doing as a beginner and Im unaware of any that actually work properly) or you have file conflicts without realizing

If you compile through the CK you should be able to see which file the CK uses by using on the top options, one of the rightern menus has a sub menu on the  giving you 2 options about Papyrus Scripts, one to compile and one to manage. You can use one of them to view the Source of the Script youre compiling

 

Ahhh! You're right. I do have a mod that overwrites `SKI_ConfigBase.psc`. Kinda embarrassed that I did not notice that. Apparently (and perhaps not particularly suprisingly) the MCM Recorder mod overwrites these files in order to hook into their calls, since this is obviously not supported in the standard SkyUI API.

 

What is suprising though, is that the author of MCM Recorder has removed all references to default initialized arguments. Removing the overwritten source scripts (while keeping the compiled pex files) seems to fix the problem.

 

Thank you so much. :) 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. For more information, see our Privacy Policy & Terms of Use