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

[Lua] Issues with monitor.write()

Started by Salin, 27 March 2013 - 10:36 AM
Salin #1
Posted 27 March 2013 - 11:36 AM
At the moment, I'm working on an elevator control turtle that will take touchscreen input. I plan to have it simply write floor numbers to the monitor, and have the player click the appropriate number. However, monitor.write() is behaving strangely. If I pass it an integer to write, in this case 5, it will return "5.0". Is there a way I can make it return simply "5", and as such avoid having to reposition the cursor and overwrite the decimal with whitespace?

Will post code shortly, Chrome has a tendency to crash on me.

Edit: Code:

m = peripheral.wrap("left") --Wrapping the monitor
w = peripheral.wrap("right") --Wrapping the REther module
local floor = 1 --Turtle starts at floor 1. Will play with filesystem so that I dont have to replace it every time the world loads
local floors= 5 --Highest Floor
local draw = 1
function drawing() --Relevant Code
m.setTextScale(2) --Just a bit easier to click
while draw <= floors do --Start the loop
m.setCursorPos((draw*2)-1,1) --Set the position - I want output in odd numbered positions only!
m.write(draw) --Assume draw is 5. This writes "5.0" to the monitor
write(draw) -- This outputs "5" to the console. How can I do this on the monitor?
--wait = read("*") -- Just for debug
draw = draw + 1 -- Advance the loop
end
draw = 1 -- Reset the variable so that this function can run again at the next floor
end
drawing()
faubiguy #2
Posted 27 March 2013 - 12:09 PM
If you pass the number to tostring first, like m.write(tostring(draw)), it will only write the '5'.
Salin #3
Posted 27 March 2013 - 01:18 PM
Brilliant. Exactly what I needed - Thank you so much!
Doyle3694 #4
Posted 27 March 2013 - 09:43 PM
If you have an further questions with monitor.write(), it might be of use to know that it acts exactly as term.write(), which is way more documented
theoriginalbit #5
Posted 27 March 2013 - 09:47 PM
personally I prefer to redirect the terminal to the monitor, that way you can use write and print functions which allow for delimited strings (i.e. \n \t etc)

example
Spoiler

local mon = peripheral.wrap('left')

term.redirect( mon ) -- give terminal control to the monitor

print('This will go \n over several lines unlike \n with term.write and mon.write')

term.restore() -- give control back to the computer

however to do things like text scale you still need to do it with

mon.setTextScale( <size> )
even after redirecting…