我在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) 我不明白为什么会发生这种情况。我正在使用 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 一起使用,不是吗?
提前致谢。