moonsharp icon indicating copy to clipboard operation
moonsharp copied to clipboard

Optional parameters / params list

Open Xanatus opened this issue 8 years ago • 4 comments

I'm wondering if there is any way in getting optional paramaters and/or params lists to work with c# class methods. It's kinda a pain in the ass making tons of overloads for everything.

If this is not already possible, this post can be considered as a feature request. Other libraries like NLua support this, so I guess it should be possible to implement.

Xanatus avatar Apr 17 '17 18:04 Xanatus

In theory some support already exists

http://www.moonsharp.org/objects.html#overload

Personally I never got it to work, and ended up just creating multiple methods manually

rgarat avatar Apr 17 '17 18:04 rgarat

I know overloads work, but I would rather see support for optional arguments or even params that would work like lua func(...).

Something like: void Method(int a, int b = 2, string b = "unset") {} void Method(params DynValue[] args) {}

Xanatus avatar Apr 17 '17 19:04 Xanatus

You can do this with a CallbackFunction

static DynValue MyFunction(ScriptExecutionContext ctx, CallbackArguments args)
{
    var arguments = args.GetArray();
    
    // do stuff
    
    return DynValue.Nil;
}

And then add it to a table via:

table["myFunction"] = DynValue.NewCallback(MyFunction);

DaZombieKiller avatar Aug 05 '17 14:08 DaZombieKiller

Thats good to know. Is there a way to make this work with class methods when you register an entire class?

Xanatus avatar Aug 06 '17 19:08 Xanatus