• Home
  • Health
  • Software
  • Blog
  • computercraft

    Lua Notes

    I always forget how Lua works, and the special ComputerCraft stuff you can do:


    -- Comments
    
    -- Turtle stuff:
    turtle.turnLeft()
    turtle.up()
    turlte.down()
    -- etc
    
    -- start at 1, increment by 1, go up to 2
    for i = 1, 2, 1 do
      -- comment
    end
    
    
    if x>3 then 
      --comment
    end
    
    
    function test(param)
      -- comment
    end
    
    
    if x>4 or x<6 and x<7 then
      -- comment
    end
    
    -- if x not 3
    if x~=3 then
      --comment
    end
    
    
    while true do
      print("sleeping")
      sleep(1)
      return
    end
    
    
    write "Enter Password: "
    password = read() -- blocking
    print(password)
    
    -- modulus example:
    for i = 1, 100 do
        if i % 2 == 0 then
            print( i .. " is divisible.")
        end
    end
    
    -- Example function to download a file:
    function download(url, file)
      local content = http.get(url).readAll()
      if not content then
        error("Could not connect to website")
      end
      f = fs.open(file, "w")
      f.write(content)
      f.close()
    end