Programming in Lua: difficulty finding bug

  • #1, by nmvhWednesday, 22. June 2022, 09:41 2 years 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

    38 Posts


  • #2, by SimonSWednesday, 22. June 2022, 10:08 2 years 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

    1581 Posts

  • #3, by nmvhWednesday, 22. June 2022, 12:42 2 years 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

    38 Posts