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

Multiple updating lines on monitor

Started by disruptive, 09 April 2017 - 04:59 AM
disruptive #1
Posted 09 April 2017 - 06:59 AM
Hello. So right now I am just making a silly thing for my factory. The idea is that I have a monitor above my PC which displays the Current % efficiency of the factory and also the Day Average %. I got it to work fine for having just one line, but I dont know how to get both to work and constantly update, because they just override each other. Here is the code:

function newLine()
	local _,cY = m.getCursorPos()
	m.setCursorPos(1,cY + 1)
end

function currentP()
	m.write("Current: " .. math.random(85,98) .. "%")
	sleep(1)
	m.clearLine()
	m.setCursorPos(1,4)
end

function dayP()
	m.write("Day Avg: " .. math.random(94,99) .. "%")
	sleep(1)
	m.clearLine()
	m.setCursorPos(1,5)
end

m = peripheral.wrap("top")
m.clear()
m.setCursorPos(1,1)
m.setTextScale(0.5)
m.write("Efficiency Of")
newLine()
m.write("Factory")
newLine()
newLine()

while true do
	currentP()
	dayP()
end

If you need more explanation, please feel free to ask. Thankyou!
Edited on 09 April 2017 - 11:41 PM
blunty666 #2
Posted 09 April 2017 - 04:31 PM
In your currentP() and dayP() functions you are doing things back to front… first you need to choose which line you are writing to ( m.setCursorPos(1, lineNumber) ), then you need to clear that line ( m.clearLine() ), then you need to write the text ( m.write(…) ).

I'd also recommend removing the sleep calls in these 2 functions and just having one sleep call in the while loop, that way they'll both update at the same time.

PS - Indent your code!!! :P/>
Edited on 09 April 2017 - 02:33 PM
disruptive #3
Posted 10 April 2017 - 09:50 AM
In your currentP() and dayP() functions you are doing things back to front… first you need to choose which line you are writing to ( m.setCursorPos(1, lineNumber) ), then you need to clear that line ( m.clearLine() ), then you need to write the text ( m.write(…) ).

I'd also recommend removing the sleep calls in these 2 functions and just having one sleep call in the while loop, that way they'll both update at the same time.

PS - Indent your code!!! :P/>

Thanks so much! Also, by when you say to indent your code, where should I have indented? Thanks again.
Larry84 #4
Posted 10 April 2017 - 01:09 PM
Thanks so much! Also, by when you say to indent your code, where should I have indented? Thanks again.

I think the "Get help faster" section may help you!
http://www.computerc...king-questions/
Also, if you want more help, take a look at this tutorial: http://www.computercraft.info/forums2/index.php?/topic/4596-tutorial-indentation/
Edited on 10 April 2017 - 11:09 AM
blunty666 #5
Posted 10 April 2017 - 09:05 PM
Larry84 beat me to it! :)/> that code looks much more readable now
Bomb Bloke #6
Posted 10 April 2017 - 11:30 PM
Truth be told, although it was hard to tell due to the rich-text formatting (which I've removed), the code was originally indented.