嗨,我是一个相当新的Lua程序员,我正在尝试将参数传递给多维数组,但我一直得到一个nil值错误。
我有一个多维数组:
local transform = {
forward = {
distance = 1,
swing = robot.swing,
move = robot.forward
},
left = {
distance = 1,
swing = robot.swing,
move = robot.forward
},
down = {
distance = 1,
swing = robot.swingDown,
move = robot.down
}
}
我想尝试传递数组的部分内容:
function testFunc(transform, direction)
print(transform.direction.distance)
end
myFunc(transform, forward)
答案 0 :(得分:1)
表达式transform.direction
在transform
中查找其键为" direction"的值。如果您想要其键值为direction
变量的值,请改用transform[direction]
:
function testFunc(transform, direction)
print(transform[direction].distance)
end