Too few parameters passed to function, but parameter is optional? [solved]

I am trying to find out what can cause this error on a user created function. My function worked fine but started giving me an error out of nowhere. It still works if all three parameters are defined.

Error running script: Too few parameters passed to GetText function - only 2 passed, but 3 expected

msg(GetText(Library.General,"test")) <- Gives error
msg(GetText(Library.General,"test",1)) <- Works
<function name="GetText" parameters="library, key, mod" type="string"><![CDATA[
    p = game.pov.parent.name
    if (IsDefined("mod")) {
      p = p + "_" + ToString(mod)
    }
    script = ScriptDictionaryItem(library,key)
    Library.entry = p
    invoke (script)
    string = "<br/>"+Library.result
    return (string)
  ]]></function>
<object name="Library">
      <inherit name="editor_object" />
      <entry type="string"></entry>
      <result type="string"></result>
      <General type="scriptdictionary">
        <item key="test">
          switch (Library.entry) {
            case ("room") {
              Library.result = "This is a test"
            }
            case ("room_1") {
              Library.result = "Modifier test"
            }
          }
        </item>
      </General>
    </object>

I could make a workaround for the third parameter but I would rather figure out would cause this problem and fix it correctly.


I think: if you set it as having 3 parameters, you must pass/use/have 3 arguments/inputs to it

(also, you may want to remove the spaces/white-spaces in the parameters and arguments, after the commas in them, just to be safe, not sure if quest ignores them or not)

I think: you can overload Functions in quest:

msg(GetText(Library.General,"test"))

msg(GetText(Library.General,"test",1))

----------------

<function name="GetText" parameters="library,key" type="string">
</function>

<function name="GetText" parameters="library,key,mod" type="string">
</function>

"parameter and/or return-values are optional", means that you don't need to have parameters and/or return-values for your function/definition:

f1
f2 ("bye")
msg (f3)
msg (f4 ("maybe"))

-------

output:

hi
bye
okay
maybe

--------

<function name="f1">
  msg ("hi")
</function>

<function name="f2" parameters="p">
  msg (p)
</function>

<function name="f3" type="string">
  r = "okay"
  return (r)
</function>

<function name="f4" parameters="p" type="string">
  return (p)
</function>

@hegemonkhan

I think: if you set it as having 3 parameters, you must pass/use/have 3 arguments/inputs to it

Incorrect. If a function is defined with 3 parameters, you can call it by passing up to 3 parameters to it. In this case, you need to use IsDefined to check the existence of any optional parameters, exactly as the OP has done.

(also, you may want to remove the spaces/white-spaces in the parameters and arguments, after the commas in them, just to be safe, not sure if quest ignores them or not)

No, whitespace is ignored. The standard libraries include a single space after the comma, and the GUI editor will output it this way too.


@zer_0

I just tested your code in my own game, and it works fine in both cases.

Are you sure about which line the error is coming from?

Because:

  • msg(GetText(Library.General,"test")) should work
  • msg(GetText(Library.General,"test",1)) should work
  • GetText(Library.General,"test",1) Should work, but doesn't output anything
  • GetText(Library.General,"test") gives an error

For some reason, function calls are handled differently depending whether it's in an expression or not. If a function call is on its own on a line, you must specify the exact number of parameters.


I had a look back at my code and that was the problem, an instance where it was called on it's own. I figure it had to be something simple I was overlooking. No output makes sense, but the parameter error is strange.

And yes, nearly all that white space is generated by Quest, I do my coding in the gui code view and not full game code view. Quest likes to reformat the code when toggling back and forth between code view and the gui blocks.


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums