将任意参数从C#传递给Lua函数 [英] Passing Arbitrary Arguments From C# to Lua Functions

查看:475
本文介绍了将任意参数从C#传递给Lua函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已找到问题的原因.答案已发布在下面.

问题已更改,请参阅问题" 部分.

The problem has changed, please see "The problem" section.

我正在使用LuaInterface.使用此库对lua函数的通用调用具有此签名LuaFunction.Call(params object[] args).我创建了一个包装器函数,该函数可以从库中捕获异常并设置其格式以在游戏机控制台窗口中显示.

I am using LuaInterface. The generic call for lua functions using this library has this signature LuaFunction.Call(params object[] args). I have created a wrapper function that catches exceptions from the library and formats them for display on the in-game console window.

我正在尝试调用lua函数,但未接收到参数.这是C#中的行

I am trying to call a lua function, but it is not receiving the arguments. This is the line in C#

Game.Instance.scriptEngine.Call("GenerateChunk", chunks[chunkID], GetChunkGridPosition(chunkID));

这只是包装对此Lua函数的调用,该函数接受两个参数:

Which is simply wrapping a call to this Lua function that accepts two arguments:

//lua
function GenerateChunk(worldChunk, chunkGridPosition)
    Log(LogLevel.Error, worldChunk.ToString());
    Log(LogLevel.Error, chunkGridPosition.ToString());
end

仅调用C#Log函数(可以正确解析,并且在Lua上下文中可见).

that merely calls back into a C# Log function (which resolves correctly, and is visible in the Lua context).

问题是,当我尝试调用GenerateChunk函数并将其返回时,我从luainterface收到了"invalid arguments to method call"错误:

The problem is that I am getting an "invalid arguments to method call" error from luainterface when attempting to call the GenerateChunk function, throwing this back:

invalid arguments to method call
   at JASG.ScriptEngine.LuaError(Exception ex) Scripting\ScriptEngine.cs:line 144
   at JASG.ScriptEngine.Call(String fnName, Object[] args) Scripting\ScriptEngine.cs:line 86
   at JASG.ChunkManager.WakeChunk(Int32 chunkID) World\ChunkManager.cs:line 123
   at JASG.ChunkManager.GetChunk(Int32 chunkID, Boolean wakeIfAsleep) World\ChunkManager.cs:line 53


我尝试了各种调用ScriptEngine.Call方法的方法,尝试将参数包装在object []数组中,等等,但是没有骰子.有什么想法为什么lua没有收到我要通过的论点?我已经验证了传入的两个参数在C#中都不为空.


I have tried various ways of calling the ScriptEngine.Call method, tried wrapping the arguments in an object[] array, etc., but no dice. Any ideas why lua is not receiving my arguments that I am passing? I have verified both arguments are non-null in C# when being passed in.

推荐答案

我错误地将问题识别为与Lua的调用有关.实际上,我收到的错误消息源自Lua脚本,该脚本回呼了我的C#Log函数.

I incorrectly identified the problem as being with the call into Lua. The error message I was receiving was in fact originating from the Lua script calling back into my C# Log function.

我发现了一种艰难的方式,尽管将枚举LogManager.LogLevel暴露于lua脚本环境中,但 Lua不支持枚举类型.因此,

I have discovered the hard way that in spite of exposing the enum LogManager.LogLevel to the lua script envronment, Lua does not support enum types. Thus,

Log(LogLevel.Debug, "hello"); 

正在成为

Log("Debug", "hello");

当LuaInterface为C#函数编组时.直到我创建了一个辅助ScriptLog(string level, string msg),我才能够从lua内部正确使用该功能.我想保留能够在Lua中使用枚举名称的功能.

when marshalled by LuaInterface for the C# function. It was not until I created an ancillary ScriptLog(string level, string msg) that I was able to properly use the function from within lua. I wanted to keep the functionality of being able to use the enum names within Lua.

注意:由于Lua不支持枚举类型,因此tonumber(LogLevel.Debug)也会失败.

NOTE: As Lua does not support enum types, tonumber(LogLevel.Debug) fails as well.

这篇关于将任意参数从C#传递给Lua函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆