Search code examples
c#unity-game-engineluamonomoonsharp

Passing a C# object into a lua function with moonsharp.


I'm having issues using Moonsharp in Unity3d. I'm trying to pass 'AppletMessage' objects:

[MoonSharpUserData]
public class AppletMessage {
    public string Name;
    public string[] Args;

    public AppletMessage(string _name, string[] _args) {
        Name = _name;
        Args = _args;
    }
}

into a function in lua and I'm not sure how to do that. What I'm currently doing is this:

//In a start function
UserData.RegisterType<AppletMessage>();

//Later on, after popping the latest message from a stack as 'LatestMessage'
result = applet.Call( applet.Globals["OnCatchMessage"],new AppletMessage[] {LatestMessage});

this is giving me this error coming from the applet class when it tries to call the function and pass the AppletMessage in:

cannot access field  of userdata<AppletMessage>

Solution

  • Did you add Moonsharp.Interpreter.UserData.RegisterAssembly(); when your game starts? You need this to detect and make the [MoonSharpUserData] tags work properly.