0

我有一个使用 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

MainBuffer是一个类型为 的对象BufferedCommand,其中包含Command结构数组,称为“Hold”、“Release”和“Press”。该Command结构包含两个枚举属性,按钮,类型Button(一个标志枚举)和Directions类型Direction(另一个标志枚举)。

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

4

1 回答 1

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

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

于 2018-01-28T20:38:09.227 回答