Page 1 of 1

How to use Default parameter values in Lua

Posted: Thu Sep 07, 2023 4:31 pm
by gamefang
Hi Tony,

I have registered some Cs functions to Lua, with some parameters like:

Code: Select all

// csharp: Func(string a, int b=0)
Lua.RegisterFunction(nameof(Func), this, SymbolExtensions.GetMethodInfo(() => Func(string.Empty, 0)));
Now I use this in Dialogue Database like so:

Code: Select all

// lua
Func("blah")	// TargetParameterCountException: Number of parameters specified does not match the expected number.
Func("blah", 0)	// it's ok, but too long
Can I just omit some parameters to make that easier?(omitted parameters can be placed at the end)

Re: How to use Default parameter values in Lua

Posted: Thu Sep 07, 2023 7:37 pm
by Tony Li
Hi,

No, Lua syntax doesn't allow optional parameters. Register two C# separate methods with Lua. Example:

Code: Select all

Lua.RegisterFunction(nameof(Func), this, SymbolExtensions.GetMethodInfo(() => Func(string.Empty, 0)));
Lua.RegisterFunction(nameof(Func2), this, SymbolExtensions.GetMethodInfo(() => Func(string.Empty)));
...
void Func2(string s)
{
    Func(s, 0);
}

Re: How to use Default parameter values in Lua

Posted: Thu Sep 07, 2023 10:39 pm
by gamefang
It's a good idea, thanks a lot!

Re: How to use Default parameter values in Lua

Posted: Fri Sep 08, 2023 8:18 am
by Tony Li
Glad to help!