标签: moonsharp

使用moonsharp将C#对象传递给lua函数.

我在Unity3d中使用Moonsharp时遇到问题.我正在尝试传递'AppletMessage'对象:

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

    public AppletMessage(string _name, string[] _args) {
        Name = _name;
        Args = _args;
    }
}
Run Code Online (Sandbox Code Playgroud)

进入lua的一个函数,我不知道该怎么做.我目前正在做的是:

//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});
Run Code Online (Sandbox Code Playgroud)

当它试图调用函数并传递AppletMessage时,这给了我来自applet类的错误:

cannot access field  of userdata<AppletMessage>
Run Code Online (Sandbox Code Playgroud)

c# mono lua unity-game-engine moonsharp

5
推荐指数
1
解决办法
582
查看次数

Moonsharp pair(...) 引发异常“错误的参数 #1 到 'next'(需要表,得到字符串)”

我不明白为什么会发生这种情况。我正在使用 Moonsharp 在我的应用程序中运行 LUA 脚本,我创建了一个 LUA 函数 IN(v, ...) 并且我想对 ... 参数进行配对。

IN('param1', 'param2', 'param1') -- expected it to return true
function IN(v, ...)
    local args = ...
    local res = true
    for i, v in pairs(args) do
        if valueIn == v then
            res = true
            break
        end
    end
    return res
end
Run Code Online (Sandbox Code Playgroud)

如果它被调用,我会收到以下异常:

“MoonSharp.Interpreter.ScriptRuntimeException” 'next' 的错误参数 #1(需要表,得到字符串)

所以我决定检查我的 ... 变量中是否有一个字符串而不是一个表。

function args(v, ...)
    return ...
end
Run Code Online (Sandbox Code Playgroud)

C# 中的返回值是一个包含 'param2' 和 'param1' 的 2 个值的元组,所以它应该与对或 ipairs 一起使用,不是吗?

提前致谢。

c# lua moonsharp

1
推荐指数
1
解决办法
768
查看次数

标签 统计

c# ×2

lua ×2

moonsharp ×2

mono ×1

unity-game-engine ×1