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

Monitor New-line help

Started by Pugz, 28 July 2012 - 11:50 AM
Pugz #1
Posted 28 July 2012 - 01:50 PM
Hey pro's! :)/>/> please help

I started learning Lua around 2 days ago so please understand I'm far from a pro xD thus why I'm here. I need to understand the new-line "/n" in monitors I have searched alot and found some interesting answers but none simple as I would like.

Posts about monitors and the coding are few and far between so trying to get the info i need is tough but not impossable. I have created a simple code to check if there is a peripheral on the right and print a message to confirm and also to check what kind of peripheral it is and to report if it is indeed a monitor.

incoming nub-cake coding:


---[[
local w,h = term.getSize()
local wm, hm, monitor
local monP = peripheral.isPresent("right")
local monT = peripheral.getType("right")
--]]

---[[
function startMonitor()
  monitor = peripheral.wrap("right")
  monitor.setTextScale(1)
  wm, hm = monitor.getSize()
  monitor.clear()
end
startMonitor()
--]]

---[[
if monP == true then
  print("The monitor on the right is present")
  else
  print("No monitor on the right detected")
end
if monT == "monitor" then
  print("This is a monitor")
  else
  print("I don't know what the peripheral is")
end
--]]

---[[
if monP == true then
  monitor.write("The monitor on the right is present")
  else
  monitor.write("No monitor on the right detected")
end
if monT == "monitor" then
  monitor.write("This is a monitor")
  else
  monitor.write("I don't know what the peripheral is")
end
--]]

As you can see I called a few local variables and setup the monitor function, and yes I have tried to change the last part of the code to have the new-line but it does nothing but show a ?.

new-line coding:
---[[
if monP == true then
  monitor.write("The monitor on the right is presentn")
  else
  monitor.write("No monitor on the right detectedn")
end
if monT == "monitor" then
  monitor.write("This is a monitorn")
  else
  monitor.write("I don't know what the peripheral isn")
end
--]]

I'm guessing that maybe i need to set a variable in the header for it to understand n? maybe I'm just being the plank that I am and missing something?

I know you can use term.redirect() but that doesn't seem to be without it's issues at the moment (or at least for me it doesn't) so I would like to try and keep it clean and to set it to functions that I can call later within the program.

Any help would be great!

Edit: changed the /n to n.. what a plank lol xD
Pinkishu #2
Posted 28 July 2012 - 02:04 PM
Uhm didn't test it but try n instead?
Pugz #3
Posted 28 July 2012 - 02:11 PM
Uhm didn't test it but try n instead?

Ohhh what a classic plank I am lol, I pasted the code into the OP from the test I did checking if /n would work! I will change it but it was supposed to be n in the OP not /n!! sorry, wow I'm so stupid.

Just to recap I used "n" and it shows a "?" changing it to "/n" just shows the /n in the string printed/written.
Pinkishu #4
Posted 28 July 2012 - 02:53 PM
Well then monitors probably don't support new line escape xD just do something like
term.write("line1")
local cX,cY = term.getCursorPos()
term.setCursorPos(1,cY+1)
term.write("line2")
Pugz #5
Posted 28 July 2012 - 03:16 PM
Well then monitors probably don't support new line escape xD just do something like
term.write("line1")
local cX,cY = term.getCursorPos()
term.setCursorPos(1,cY+1)
term.write("line2")

At the moment this is just me trying to get things working on the monitor, in the long run I have very big plans and to have to set the CursorPos every time i need a new-line would be a massive pain in the rear lol, not to mention having to check the CursorPos and allow the new line function to work as intended.
I'm sure there is a better way of doing this, is n supposed to work? I mean should I post a bug report? maybe I'm wrong?
Pinkishu #6
Posted 28 July 2012 - 03:24 PM
well you could do like

function newLine()

  local _,cY= term.getCursorPos()
  term.setCursorPos(1,cY+1)
end

term.write("line1")
newLine()
term.write("line2")
Pugz #7
Posted 28 July 2012 - 04:08 PM
This is interesting to say the least xD learning Lua is just way to much fun. OK here is the new revised code with your suggestions plus a couple of changes and yes, it does work great! :)/>/> thank you so much


---[[
monitor = peripheral.wrap("right")
local w,h = term.getSize()
local monP = peripheral.isPresent("right")
local monT = peripheral.getType("right")
local cX,cY = monitor.getCursorPos()
--]]

---[[
function startMonitor()
  monitor.setTextScale(1)
  wm, hm = monitor.getSize()
  monitor.clear()
end
startMonitor()
--]]

---[[
function newLine()
  local _,cY= monitor.getCursorPos()
  monitor.setCursorPos(1,cY+1)
end
--]]

---[[
if monP == true then
  print("The monitor on the right is present")
  else
  print("No monitor on the right detected")
end
if monT == "monitor" then
  print("This is a monitor")
  else
  print("I don't know what the peripheral is")
end
--]]

---[[
if monP == true then
  monitor.write("The monitor on the right is present")
  newLine()
  else
  monitor.write("No monitor on the right detected")
  newLine()
end

if monP == true then
  monitor.write("The monitor on the right is present")
  newLine()
  else
  monitor.write("No monitor on the right detected")
  newLine()
end

if monT == "monitor" then
  monitor.write("This is a monitor")
  else
  monitor.write("I don't know what the peripheral is")
end
--]]

as you can see I've added another


if monP == true then
  monitor.write("The monitor on the right is present")
  newLine()
  else
  monitor.write("No monitor on the right detected")
  newLine()
end

to make sure that the function is working as intended and yes.. yes it is! it's great when you get things right! it would have been alot better if n would have worked fine but still a workaround will do just fine :D/>/> Thanks for you help Pinkishu!

PS:
Might I ask though what does the function do? I get an idea but I don't understand the _, part? does it just call for nothing? :S

function newLine()
  local _,cY= monitor.getCursorPos()
  monitor.setCursorPos(1,cY+1)
end
Pinkishu #8
Posted 28 July 2012 - 04:20 PM
Well term.getCursorPos returns to values, the first being the X position fo the cursor (so horizontal) the second the Y position (so vertical)
since you don't use the X position i just called the variable for it _; i heard someone once say thats a convention in lua for variables you won't use or so ^^"
Pugz #9
Posted 28 July 2012 - 04:28 PM
Great, thanks for clearing that up and thank you so much for your help, it's greatly appreciated.