Jump to content




Multiple updating lines on monitor


5 replies to this topic

#1 disruptive

  • Members
  • 5 posts

Posted 09 April 2017 - 05: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 by Bomb Bloke, 10 April 2017 - 12:41 AM.


#2 blunty666

  • Members
  • 79 posts

Posted 09 April 2017 - 03: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 by blunty666, 09 April 2017 - 03:33 PM.


#3 disruptive

  • Members
  • 5 posts

Posted 10 April 2017 - 08:50 AM

View Postblunty666, on 09 April 2017 - 03:31 PM, said:

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.

#4 Larry84

  • Members
  • 51 posts
  • LocationItaly

Posted 10 April 2017 - 12:09 PM

View Postdisruptive, on 10 April 2017 - 08:50 AM, said:

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 by Larry84, 10 April 2017 - 12:09 PM.


#5 blunty666

  • Members
  • 79 posts

Posted 10 April 2017 - 08:05 PM

Larry84 beat me to it! :) that code looks much more readable now

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 10 April 2017 - 10: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users