Programming in Lua: difficulty finding bug

  • #1, by nmvhWednesday, 22. June, 09:41 6 days ago
    So I'm dabbling with some Lua programming in Visionaire. This function (which I put in the "definition script" area) checks if all six bricks are aligned in a simple brick puzzle.

    The function is called everytime the user rotates one of the bricks. But either the function itself or the way I call it has one or several errors in it, because it wont work.

    function bricksAligned()
        bricks = {Objects["BrickA1"], Objects["BrickA2"], Objects["BrickB1"], Objects["BrickB2"],                         Objects["BrickC1"], Objects["BrickC2"]}
         for i, brick in bricks
         do
            if (brick.Rotation ~= 0) then
               return false
            end
          end
       return true
    end

    The calling of the above function takes place in this code:

    rotateBrick(Objects["BrickA1"])
    if (bricksAligned() == true) then
      [the user solved the puzzle; do stuff]
    end

    Please someone help out!

    Newbie

    37 Posts


  • #2, by SimonSWednesday, 22. June, 10:08 6 days ago
    Always take a look in the tab console for any errors. "Doesn't work" is not very helpful most of the time. print the results and check for what you expect. But just some things I'm seeing:

    function bricksAligned()
        local bricks = {Objects["BrickA1"], Objects["BrickA2"], Objects["BrickB1"], Objects["BrickB2"], Objects["BrickC1"], Objects["BrickC2"]}
         for i,brick in pairs(bricks) do
            if brick.Rotation ~= 0 then
               return false
            end
          end
       return true
    end


    Also you're probably not instantly turning the bricks? So there might be a delay.

    Thread Captain

    1543 Posts

  • #3, by nmvhWednesday, 22. June, 12:42 6 days ago
    SimonS, a big thanks! It was the "pairs" detail I'd missed. Now it works as intended. Also, the "local" keyword seems like a good practice.

    Newbie

    37 Posts

Write post