1

Hi I am a fairly new Lua programmer and I am trying to pass parameters to a multidimensional array, but I keep getting a nil value error.

I have a multidimensional array:

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
  }
}

and I am trying to pass parts of the array like so:

function testFunc(transform, direction)
  print(transform.direction.distance)
end

myFunc(transform, forward)
1
  • Those are tables, not arrays. OK, in Lua arrays are tables, but they're not array-like tables. They're just key-value tables. Commented Aug 1, 2017 at 22:33

1 Answer 1

1

The expression transform.direction looks up the value in transform whose key is the word "direction". If you want the value whose key is the value of the direction variable, use transform[direction] instead:

function testFunc(transform, direction)
  print(transform[direction].distance)
end
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That answered my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.