How to use Default parameter values in Lua

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
gamefang
Posts: 4
Joined: Thu Sep 07, 2023 4:07 pm

How to use Default parameter values in Lua

Post 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)
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to use Default parameter values in Lua

Post 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);
}
gamefang
Posts: 4
Joined: Thu Sep 07, 2023 4:07 pm

Re: How to use Default parameter values in Lua

Post by gamefang »

It's a good idea, thanks a lot!
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to use Default parameter values in Lua

Post by Tony Li »

Glad to help!
Post Reply