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

While loop not working

Started by makeme, 02 March 2014 - 02:14 PM
makeme #1
Posted 02 March 2014 - 03:14 PM
I've been trying to print a Bar from a function but keep running into issues
it just prints the back ground bar atm How can I fix it?


Heres the code:



os.loadAPI("BarAPI")
local Mon = peripheral.wrap("Right")
BarAPI.PercentBar(1, 1, 2, 1, 25, Mon)


--bar API
--BgColor = bar unused
--BColor = Bar Color
--Percent = Percentage that needs to be filled
--CX, Cursor X pos
--CY, Cursor Y pos
--
-- Made By SteamPunk_Devil -- CCfourm Name Makeme -- Ingame Name SteamPunk_Angel
function Percentage(current,total) --returns One number as a percentage of the second one, use Round() to round
local No1 = current
local No2 = total
local Percent = (No1 / No2) * 100
return Percent
end
function Round(num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end
function PercentBar(CursorX, CursorY, InActiveColor, ActiveColor, n, Mon)
local n10 = n/10
local FormdN = Round(n10, 0)
local i = 1
local Paste = Mon or term
Paste.setCursorPos(CursorX, CursorY)
Paste.setBackgroundColor(InActiveColor)
Paste.write("[		  ]")
Paste.setCursorPos(CursorX, CursorY)
while FormdN <= i do
Paste.setBackgroundColor(ActiveColor)
Paste.write(" ")
i = i + 1
end
end
Edited on 02 March 2014 - 04:51 PM
makeme #2
Posted 02 March 2014 - 05:45 PM
Ive worked out that the while loop isnt working


while FormdN <= i do
Paste.setBackgroundColor(ActiveColor)
Paste.write(" ")
i = i + 1
end

Can somebody tell me it isnt working?
Edited on 02 March 2014 - 04:58 PM
CometWolf #3
Posted 02 March 2014 - 07:27 PM
your loop is utterly pointless, since it will just write a blank space in the same spot with the same color over and over again.

Edit: My bad, just realized it would obviously move the cursor as it writes.
I'd suggest adding some prints to check the variable FormdN and i as it goes along.
Edited on 02 March 2014 - 07:48 PM
Inumel #4
Posted 02 March 2014 - 07:55 PM
Maybe this is what you're looking for

for i=1, FormdN
  term.setCursorPos(1,i)
  Paste.setBackgroundColor(ActiveColor)
  Paste.write(" ")
end
makeme #5
Posted 03 March 2014 - 11:05 AM
Edit: My bad, just realized it would obviously move the cursor as it writes.
I'd suggest adding some prints to check the variable FormdN and i as it goes along.

All ready tried with FormdN it's correct I'll try with i later
CometWolf #6
Posted 03 March 2014 - 02:31 PM
Looking over it again, i realize that i is always 1 at the start, and FormdN needs to be less than or equal to 1 for it to loop, and that would make it loop until it crashes. Is this really correct?


local i = 1
while FormdN <= i do
Edited on 03 March 2014 - 01:31 PM
makeme #7
Posted 03 March 2014 - 03:38 PM
I think I might have got it the wrong way around its meant to be the other way and i gets 1 added to it each time

Edit: I've been working on it at work I can't test it but heres the pastebin link pastebin.com/repWddN9
Edited on 03 March 2014 - 02:44 PM
CometWolf #8
Posted 03 March 2014 - 04:28 PM

while i <= FormdN do 
	    print(i)
	    print(FormdN)
	    Paste.setCursorPos(BarCursX,BarCursY)
	    Paste.setBackgroundColor(ActiveColor)
	    Paste.setTextColor(TextColor)
	    Paste.write("/")
	    BarCursX = BarCursX + 1
end
This will crash because the loop will never end, since you don't change neither i nor FormdN.
makeme #9
Posted 03 March 2014 - 04:37 PM
ffs missed the i=i +1
CometWolf #10
Posted 03 March 2014 - 04:56 PM
Seems to me a for loop would be more ideal for your usage.

Paste.setBackgroundColor(ActiveColor)
Paste.setTextColor(TextColor)
for i = 1,FormdN do
  Paste.write("/")
end
BarCursX = BarCursX + FormdN -- this line im guessing isn't really needed.
Or better yet, forgo the loop entirely with string.rep

Paste.setBackgroundColor(ActiveColor)
Paste.setTextColor(TextColor)
Paste.write(string.rep("/",formdN))
BarCursX = BarCursX + FormdN
Edited on 03 March 2014 - 04:01 PM
makeme #11
Posted 03 March 2014 - 06:22 PM
Or better yet, forgo the loop entirely with string.rep

Paste.setBackgroundColor(ActiveColor)
Paste.setTextColor(TextColor)
Paste.write(string.rep("/",formdN))
BarCursX = BarCursX + FormdN

Thanks Thats exactly what I wanted still getting an issue on the line "Paste.write(string.rep("/",formdN))" I think it has something to do with line 28 in http://pastebin.com/f1SyGYqN

Edit got it on that line it should be FormdN not formdN
Edited on 03 March 2014 - 05:25 PM
CometWolf #12
Posted 03 March 2014 - 06:40 PM
Aaaah, right ofcourse, my bad. The whole first letter caps throws me off, as that's not common practice in Lua :P/>
makeme #13
Posted 03 March 2014 - 07:12 PM
Aaaah, right ofcourse, my bad. The whole first letter caps throws me off, as that's not common practice in Lua :P/>

Its what my javascript teacher taught me and what I default to :P/>