moonsharp-devs / moonsharp

An interpreter for the Lua language, written entirely in C# for the .NET, Mono, Xamarin and Unity3D platforms, including handy remote debugger facilities.
http://www.moonsharp.org
Other
1.42k stars 214 forks source link

ScriptRuntimeException: cannot convert a function to a clr type System.Action #186

Open Rhuantavan opened 7 years ago

Rhuantavan commented 7 years ago

I have this simple LUA script calling the GUIManager's GUI.OpenMessageWindowWithAction method. I am passing a playKnockSound function to it, so the C# implementation of OpenMessageWindowWithAction calls that function.

LUA

function openMessageWindowWithAction (message, doneCallback)
    GUI.OpenMessageWindowWithAction (message, doneCallback)
end

playKnockSound = function ()
    playSound("door-knock-3")
end

openMessageWindowWithAction ("Someone knocked on the tavern door.", playKnockSound)

C# GUIManager.cs

public class Singleton<T> : MonoBehaviour where T : MonoBehaviour {
    protected static T instance;

    /**
      Returns the instance of this singleton.
    */
    public static T Instance {
        get {
            if(instance == null) {
                instance = (T)FindObjectOfType(typeof(T));

                if (instance == null) {
                    Debug.LogError("An instance of " + typeof(T) + " is needed in the scene, but there is none.");
                }
            }

            return instance;
        }
    }
}

public class GUIManager : Singleton<GUIManager> {

    public static void OpenMessageWindowWithAction(string text, Action done) {
        // Skipped actual code, just calling the callback directly
        done();
    }

}

C# ScriptManager.cs

UserData.RegisterType<GUIManager>();
script.Globals["GUI"] = typeof(GUIManager); // Make statics available

Sadly, Unity errors out:

ScriptRuntimeException: cannot convert a function to a clr type System.Action
MoonSharp.Interpreter.Interop.Converters.ScriptToClrConversions.DynValueToObjectOfType (MoonSharp.Interpreter.DynValue value, System.Type desiredType, System.Object defaultValue, Boolean isOptional)
MoonSharp.Interpreter.Interop.StandardUserDataMethodDescriptor.Callback (MoonSharp.Interpreter.Script script, System.Object obj, MoonSharp.Interpreter.ScriptExecutionContext context, MoonSharp.Interpreter.CallbackArguments args)
MoonSharp.Interpreter.Interop.StandardUserDataOverloadedMethodDescriptor.PerformOverloadedCall (MoonSharp.Interpreter.Script script, System.Object obj, MoonSharp.Interpreter.ScriptExecutionContext context, MoonSharp.Interpreter.CallbackArguments args)
MoonSharp.Interpreter.Interop.StandardUserDataOverloadedMethodDescriptor+<>c__DisplayClass5.<GetCallback>b__4 (MoonSharp.Interpreter.ScriptExecutionContext context, MoonSharp.Interpreter.CallbackArguments args)
MoonSharp.Interpreter.CallbackFunction.Invoke (MoonSharp.Interpreter.ScriptExecutionContext executionContext, IList`1 args, Boolean isMethodCall)
MoonSharp.Interpreter.Execution.VM.Processor.Internal_ExecCall (Int32 argsCount, Int32 instructionPtr, MoonSharp.Interpreter.CallbackFunction handler, MoonSharp.Interpreter.CallbackFunction continuation, Boolean thisCall, System.String debugText, MoonSharp.Interpreter.DynValue unwindHandler)
MoonSharp.Interpreter.Execution.VM.Processor.Processing_Loop (Int32 instructionPtr)

Any idea if I am doing something wrong and how to solve it? Thank you!

rgarat commented 7 years ago

Hi, I answered your question in the forum, not sure if you saw it, hope it helps

https://groups.google.com/d/msg/moonsharp/On6dhfrpn1A/vohiIF1zFgAJ

MEMESCOEP commented 2 months ago

@rgarat If you still remember what the solution was, could you post it here? I can't access the google groups post ;(