Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional parameters / params list #190

Open
Xanatus opened this issue Apr 17, 2017 · 4 comments
Open

Optional parameters / params list #190

Xanatus opened this issue Apr 17, 2017 · 4 comments

Comments

@Xanatus
Copy link

Xanatus commented Apr 17, 2017

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.

@rgarat
Copy link

rgarat commented Apr 17, 2017

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

@Xanatus
Copy link
Author

Xanatus commented Apr 17, 2017

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) {}

@DaZombieKiller
Copy link

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);

@Xanatus
Copy link
Author

Xanatus commented Aug 6, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants