How to call & await async C# method from Lua / MoonSharp script?

badevilerror

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?

badevilerror

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related