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

[SOLVED] [LUA] Trouble with monitors

Started by W00dyR, 18 December 2012 - 06:56 AM
W00dyR #1
Posted 18 December 2012 - 07:56 AM
To start of, Im not very good with all this, so sorry if I am asking a stupid question :(/>

Anyway, my problem is that I have written a little program that allows me to type in anything and it will display on a monitor. The problem I am having is that when I write longer messages, I can't get it to go down to the next line, but it will just spam it of the screen and not show up.

Another question that I have is how do I make it so that I can simply type something like "monitor <message>" instead of having it ask for my input after I type the program name?

My program:


monitor = peripheral.wrap("left")

print("Message?")

term.setCursorPos(1,3)
input = read()

monitor.clear()
monitor.setCursorPos(1,1)
monitor.setTextScale(2,5)
monitor.write(input)


term.clear()
term.setCursorPos(1,1)

Thanks for the advice ahead!
Alekso56 #2
Posted 18 December 2012 - 08:08 AM
You mean something like:


print("Please input a message:")
input = read()

local mon = nil

for _,s in pairs(rs.getSides()) do
	if peripheral.getType(s) == "monitor" then
		mon = peripheral.wrap(s)
		break
	end
end

if mon then
	mon.clear()
	mon.setCursorPos(1,1)
	mon.write(input)
else
	print("No monitor attached!")
	sleep(1)
end

term.clear()
term.setCursorPos(1,1)
term.write(input)
W00dyR #3
Posted 18 December 2012 - 08:12 AM
Alekso56, can you also explain what is happening in the part after "input = read()" till "term.clear()", since I am trying to learn this stuff and not actually copy paste from others :/
Alekso56 #4
Posted 18 December 2012 - 08:27 AM
print("Please input a message:")
input = read()

local mon = nil		  -- here we set the "mon" variable to nil just so we have the variable (it doesnt actually do anything untill the end)

for _,s in pairs(rs.getSides()) do					   -- get all sides of computer/turtle
		if peripheral.getType(s) == "monitor" then	 -- here we check if the peripheral is a monitor and if it is we send it downwards
				mon = peripheral.wrap(s)			   -- wrap dah peripheral (mon is now a "shortcut")
				break					  --quit sub program and continue onto the next line( after the end's)
		end
end

if mon then	 -- we continue if the monitor is present
		mon.clear()	--clear the monitor
		mon.setCursorPos(1,1)   --set cursor to top
		mon.write(input)	-- write input to screen
else
		print("No monitor attached!")	--mon = nil  error
		sleep(1)						-- sleep
end

Alekso56, can you explain stuff?

Something like that?
Lyqyd #5
Posted 18 December 2012 - 08:32 AM
Though, if excessive string length is the problem, it would probably be better to term.redirect to the wrapped monitor, then use print().
W00dyR #6
Posted 18 December 2012 - 08:34 AM
Alekso56, can you explain stuff?

Something like that?


Yes that works for me :)/>

Now is there still any way to make the program work like this:

"monitor <message>"

Instead of having it like you type "monitor" and then it will ask for the message? And I am still looking for a solution so that the monitor doesn't spam of screen and just continues on the next line instead, because I have a 8x6 block monitor screen and right now it allows me to type one line only
W00dyR #7
Posted 18 December 2012 - 08:35 AM
Though, if excessive string length is the problem, it would probably be better to term.redirect to the wrapped monitor, then use print().

If I use something like this, won't it still spam it of the screen? Since I don't see (might just be me) any way that it checks wether the message is excessive for the screen length
Lyqyd #8
Posted 18 December 2012 - 08:50 AM
The print function itself will take care of wrapping the lines as necessary.


local args = {...}
if #args < 1 then
  print("Please provide a message!")
  return
end

local mon
for _,side in ipairs(rs.getSides()) do
  if peripheral.getType(side) == "monitor" then
    mon = peripheral.wrap(side)
    break
  end
end

if not mon then
  print("No monitor found!")
end

term.redirect(mon)
term.clear()
term.setCursoPos(1,1)
print(table.concat(args, " "))
term.restore()

You may also want to call this something other than monitor, since ComputerCraft comes with a program with that name.
W00dyR #9
Posted 18 December 2012 - 08:57 AM
The print function itself will take care of wrapping the lines as necessary.


local args = {...}
if #args < 1 then
  print("Please provide a message!")
  return
end

local mon
for _,side in ipairs(rs.getSides()) do
  if peripheral.getType(side) == "monitor" then
	mon = peripheral.wrap(side)
	break
  end
end

if not mon then
  print("No monitor found!")
end

term.redirect(mon)
term.clear()
term.setCursoPos(1,1)
print(table.concat(args, " "))
term.restore()

You may also want to call this something other than monitor, since ComputerCraft comes with a program with that name.

Alright, I got the program on a disk so it doesn't overwrite the program that comes with ComputerCraft as I experienced.

From what I understand, using arguments will allow me to make it that you can type the program name + message in one line. then there is the part that checks where the monitor is and sets it to print there, the print function will make sure the lines aren't excessive.

The only part I don't really get is the "print(table.concat(args,""))", how exactly does that work? Print function obviously prints on the monitor because it is redirected there, and then the "args" takes the arguments that you typed earlier. but what is the table.concat for? Are the arguments stored in a table? If so, what does the concat stand for?

Thanks again :)/>
Lyqyd #10
Posted 18 December 2012 - 09:06 AM
The shell splits up the command-line arguments by spaces, so to get back to a single string to print, we concatenate all of the arguments back together with a single space separating each. The table.concat function just concatenates the table into a single string.
W00dyR #11
Posted 18 December 2012 - 09:13 AM
The shell splits up the command-line arguments by spaces, so to get back to a single string to print, we concatenate all of the arguments back together with a single space separating each. The table.concat function just concatenates the table into a single string.

Alright that makes sense to me, and a quick probably stupid question, what does the "term.restore()" function do?
W00dyR #12
Posted 18 December 2012 - 09:26 AM
Oh and also, right now I am having trouble placing the monitor.setTextScale(2,5), it keeps giving me errors wherever I try to place it :/
Orwell #13
Posted 18 December 2012 - 09:33 AM
term.redirect(mon) makes all the term functions (like term.clear(), term.write()) work on the monitors instead of the terminal. That way all standard functions for the terminal (like print()) output to the monitor. term.restore() simply restores this and goes back to using the actual terminal.

you need to call setTextScale on the mon variable you have there. monitor is no api. so right before:

term.redirect(mon)
put:

mon.setTextScale(2)
I also don't think that setTextScale(2,5) is useful as setTextScale takes only one argument to my knowledge.
Lyqyd #14
Posted 18 December 2012 - 09:34 AM
term.restore returns the output to the normal console. Without it, after the program had run, the shell would be on the monitor instead!

You will need to use mon.setTextScale, since we wrapped the monitor into the variable "mon", not the variable "monitor". Place it just before the term.redirect and be sure to use 2.5, not 2,5.
W00dyR #15
Posted 18 December 2012 - 09:45 AM
Alright, thanks everyone, Lyqyd in special, I learned some new stuff + figured out my problems :P/>