NLua:无法转换类型' Command []'输入' System.Object []'

时间:2018-01-28 20:23:06

标签: c# arrays enums monogame nlua

我有一个使用NLua编写脚本的游戏引擎。但是,我似乎无法访问Command数组中的元素:

-- Basic direction detection. This allows forward and backward to cancel each other out if both are active.
function DetectXHoldDirection()
    directionDetect = 0
    if self.MainBuffer.Hold[0].Directions:HasFlag(Direction.Forward) then
        directionDetect = directionDetect + 1
    end
    if self.MainBuffer.Hold[0].Directions:HasFlag(Direction.Back) then
        directionDetect = directionDetect - 1
    end
end

MainBufferBufferedCommand类型的对象,其中包含Command个结构的数组,称为" Hold," "推出,"和"按。" Command结构包含两个枚举属性,按钮,类型为Button(标记枚举),Directions类型为Direction(另一个标记枚举)。

当我尝试运行此脚本时,我在标题中收到错误。为什么要投射到System.Object[]?有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

由于来自LuaInterface docs的提示,我解决了这个问题,我认为这是NLua的基础。

显然你无法在NLua中以这种方式访问​​数组。这很烦人,但您必须使用Array.GetValue()方法,如下所示:

-- Basic direction detection. This allows forward and backward to cancel each other out if both are active.
function DetectXHoldDirection()
    directionDetect = 0
    if self.MainBuffer.Hold:GetValue(0).Directions:HasFlag(Direction.Forward) then
        directionDetect = directionDetect + 1
    end
    if self.MainBuffer.Hold:GetValue(0).Directions:HasFlag(Direction.Back) then
        directionDetect = directionDetect - 1
    end
end

同样,我不喜欢这个,但如果有人有任何办法将其转换为正确的数组索引,我很想知道!但就目前而言,这就是我想要的。