I have a game engine that uses NLua for scripting. However, I can't seem to access elements in my Command
array:
-- 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
is an object of type BufferedCommand
, which contains arrays of Command
structs, called "Hold," "Release," and "Press." The Command
struct contains two enum properties, Buttons, of type Button
(a flags enum), and Directions
of type Direction
(another flags enum).
When I try running this script, I get the error in the title. Why is it trying to cast to System.Object[]
? Is there any way to get around that?
I solved the problem thanks to a hint from LuaInterface docs, which I believe was the basis for NLua.
Apparently you cannot access arrays in this way in NLua. It's quite annoying, but you have to use the Array.GetValue()
method like so:
-- 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
Again, I don't like this, but if anyone has any way of converting this to proper array indexing, I'd love to know! But for now, this does what I want.