This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
MarcoPolo0306's profile picture

How does one make a loading bar?

Started by MarcoPolo0306, 05 May 2018 - 12:52 PM
MarcoPolo0306 #1
Posted 05 May 2018 - 02:52 PM
I am making a loading bar for my installer, and it won't work.
Here is the code:


local function drawBar(startX,endX,fileAmount,currentFile)
paintutils.drawLine(startX,y/2+2,endX,y/2+2,colors.gray)
term.setBackgroundColor(colors.cyan)
string.rep(" ",fileAmount/currentFile)
end

Thanks!
SquidDev #2
Posted 05 May 2018 - 04:06 PM
You never actually do anything with the repeated string: you create it and then throw it away. You probably wanted something like:

term.write(string.rep(" ",fileAmount/currentFile))
MarcoPolo0306 #3
Posted 05 May 2018 - 04:21 PM
Oh, my bad. Thanks!

Instead of going forward, it goes backward, and the closer it gets to the end, the slower the bar moves.
EveryOS #4
Posted 05 May 2018 - 04:43 PM

local function drawBar(startX,endX,fileAmount,currentFile)
  paintutils.drawLine(startX, y/2+2, endX, y/2+2, colors.gray)
  term.setBackgroundColor(colors.cyan)
  term.setCursorPos(startX, y)
  term.write(string.rep(" ",currentFile/fileAmount*(endX-startX)))
end
Edited on 05 May 2018 - 02:46 PM
Luca_S #5
Posted 06 May 2018 - 06:16 AM

local function drawBar(startX,endX,fileAmount,currentFile)
  paintutils.drawLine(startX, y/2+2, endX, y/2+2, colors.gray)
  term.setBackgroundColor(colors.cyan)
  term.setCursorPos(startX, y)
  term.write(string.rep(" ",currentFile/fileAmount*(endX-startX)))
end
You can use paintutils for the second line too: (This also fixes a Problem with your code where the bar would be drawn at the bottom instead of y/2+2)

local function drawBar(startX,endX,fileAmount,currentFile)
  paintutils.drawLine(startX, y/2+2, endX, y/2+2, colors.gray)
  paintutils.drawLine(startX, y/2+2, startX+currentFile/fileAmount*(endX-startX), y/2+2, colors.cyan)
end
Edited on 06 May 2018 - 04:46 AM