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

Need help making a % counter

Started by Darky_Alan, 02 August 2012 - 08:28 AM
Darky_Alan #1
Posted 02 August 2012 - 10:28 AM
It's purely for show and serves no real purpose other than looks in my program, but I've no idea how to make a counter that will stay on one line, it just prints on EVERY line

also would like it to pause between number updates like sleep(.5)

EDIT: Fixed the delay issue.

local i = 0
while i < 100 do
  i = i + 1
  print( i )
sleep(.1)
end
for i=0,100
  print( i )
end
Luanub #2
Posted 02 August 2012 - 10:42 AM
just clear the screen each time and set the cursor position to where you want it to print. Just modify this clear function to the line that you want it on.


local function clear()
term.clear()
term.setCursorPos(1,3) -- position 1 on line 3
end

Then do this..

local i = 0
while i < 100 do
  i = i + 1
  clear() -- added the clear
  print( i )
sleep(.1)
end
for i=0,100
  print( i )
end
Darky_Alan #3
Posted 02 August 2012 - 10:49 AM
just clear the screen each time and set the cursor position to where you want it to print. Just modify this clear function to the line that you want it on.


local function clear()
term.clear()
term.setCursorPos(1,3) -- position 1 on line 3
end

Then do this..

local i = 0
while i < 100 do
  i = i + 1
  clear() -- added the clear
  print( i )
sleep(.1)
end
for i=0,100
  print( i )
end
Where would I call that? after every print?

EDIT: Didn't see your edit
Darky_Alan #4
Posted 02 August 2012 - 10:53 AM
It gave me:

dltest:4: attempt to index ? (a nil value)
Luanub #5
Posted 02 August 2012 - 10:58 AM
Make sure you have the function above the rest of the code. Lua works from the top of the code down.
Darky_Alan #6
Posted 02 August 2012 - 11:12 AM
Not working right, I'm basing this off of what a friend suggested, lets say I want this to print "Download complete!" when it is done, this is really confusing me for some reason >< not use to loops.

This is the non working code.

function dl()
	local function clearDL()
		term.clear()
		term.setCursorPos(1,3)
	end

	local i = 0
	while i < 100 do
		cleadrDL()
		sleep(.1)
		print( i.."%")
	end
	for i = 0, 100 do
		print( i.."%" )
	end
end

dl()
Luanub #7
Posted 02 August 2012 - 11:23 AM
remove the clear function from the dl function


local function clearDL()
    term.clear()
    term.setCursorPos(1,3)
end
function dl()
	

	local i = 0
	while i < 100 do
		cleadrDL()
		sleep(.1)
		print( i.."%")
	end
	for i = 0, 100 do
		print( i.."%" )
	end
end

dl()
Darky_Alan #8
Posted 02 August 2012 - 12:16 PM
remove the clear function from the dl function


local function clearDL()
	term.clear()
	term.setCursorPos(1,3)
end
function dl()
	

	local i = 0
	while i < 100 do
		cleadrDL()
		sleep(.1)
		print( i.."%")
	end
	for i = 0, 100 do
		print( i.."%" )
	end
end

dl()
Everything is going black and then at the end everything prints out. in individual lines.
Luanub #9
Posted 02 August 2012 - 12:33 PM
Tested and this should do what you're wanting.


local function clearDL()
	    term.clear()
	    term.setCursorPos(1,3)
end
function dl()
    local i = 0
    for i = 0, 100 do
        clearDL()
        print( i.."%" )
        sleep(.5)
    end
end

dl()
Noodle #10
Posted 02 August 2012 - 12:33 PM
To make an actual counter you could do
for i = 0,100 do
clearDL()
sleep(.1)
print( i, "%" )
end
Ahh he beat me..
Darky_Alan #11
Posted 02 August 2012 - 12:50 PM
Tested and this should do what you're wanting.


local function clearDL()
		term.clear()
		term.setCursorPos(1,3)
end
function dl()
	local i = 0
	for i = 0, 100 do
		clearDL()
		print( i.."%" )
		sleep(.5)
	end
end

dl()
To make an actual counter you could do
for i = 0,100 do
clearDL()
sleep(.1)
print( i, "%" )
end
Ahh he beat me..

Thanks for the help, +1 to both of you, you get a +1 for the effort Noodles x]
MysticT #12
Posted 02 August 2012 - 06:32 PM
If you don't want to clear the whole screen, you can remove the term.clear() and put a term.clearLine() after term.setCursorPos():

local function clear()
  term.setCursorPos(1, 3)
  term.clearLine()
end
Just in case there's something on the screen you don't want cleared :ph34r:/>/>
Darky_Alan #13
Posted 02 August 2012 - 11:03 PM
If you don't want to clear the whole screen, you can remove the term.clear() and put a term.clearLine() after term.setCursorPos():

local function clear()
  term.setCursorPos(1, 3)
  term.clearLine()
end
Just in case there's something on the screen you don't want cleared :ph34r:/>/>
Sweet! thanks.
KaoS #14
Posted 03 August 2012 - 09:24 AM
you should add a progress bar, it's quite awesome. I am just writing this now as I am at work and cannot test it but try it out


for i=1,100 do
term.clear()
term.setCursorPos(1,1)
print(i.."%")
write("<")
for a=1,i/5 do
write("=")
end
for b=1,(100-i)/5
write(" ")
end
write(">")
sleep(0.5)
end

let me know if that works, quick question though, why are you just making it count to 100%? for effect?
KaoS #15
Posted 03 August 2012 - 09:26 AM
I forgot to add a rounding down function on the for a loop and a rounding up function on the for b loop, don't forget that or your progress bar will change length