I would like to call & await a C# async method from Lua / MoonSharp code.
For example:
1).
async void Test1() {
    await Something();
}
2).
async Task Test2() {
    await Something();
}
And then calling it from Lua - 1). does not await but continues script execution, and 2). throws ScriptRuntimeException: cannot convert clr type System.Threading.Tasks.Task`1[System.Threading.Tasks.VoidTaskResult]
MoonSharp.Interpreter.Interop.Converters.ClrToScriptConversions.ObjectToDynValue exception.
Is there some way to make this work?
I've finally gone with callbacks. I don't think it's a good solution, though. So if anyone have a better one i'll be more than happy to change the accepted answer.
For anyone interested here's how to make the callbacks work in MoonSharp:
Lua / MoonSharp
SomethingAsync(10, function()
    SomePrintFunction('async work done')
end)
C#
async void SomethingAsync(int whatever, DynValue callback) {
    await SomeAsyncWorkBeingDone();
    if (callback.Type == DataType.Function) {
        callback.Function.Call();
    }
}
More info can be found in the doc's.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With