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

Could somebody help me with this monitor code?

Started by artman41, 03 April 2015 - 08:09 PM
artman41 #1
Posted 03 April 2015 - 10:09 PM
Here's the monitor code: http://pastebin.com/TW4QCW1T

My current problem is that it isn't writing to the advanced monitor that's to the right of the computer.

The monitor is 4x4 if that affects anything.

Could someone tell me what I'm doing wrong with this code please?
Lupus590 #2
Posted 03 April 2015 - 10:43 PM
where you have monitor.write (and simular commands) you need to use your mon variable
for example lines 11 and 12 should be

mon.setCursorPos(x, y)
mon.write(UserString)
Quintuple Agent #3
Posted 03 April 2015 - 10:46 PM
Ok, I believe I see the problem.
First off is your centerText function
1. At the start do

userString=tostring(userString)
That way it will still work in case you give the function a number
2. You have

monitor.write(UserString)
change that to

monitor.write(userString)
3. While not really needed, make your x and y variables local, this is a good thing to practice.
Also remove the i= i + 1 in your monitorTimer function, it is unneeded as the for loop automatically increments it.
Edited on 03 April 2015 - 08:47 PM
artman41 #4
Posted 03 April 2015 - 10:53 PM
Ok, I believe I see the problem.
First off is your centerText function
1. At the start do

userString=tostring(userString)
That way it will still work in case you give the function a number
2. You have

monitor.write(UserString)
change that to

monitor.write(userString)
3. While not really needed, make your x and y variables local, this is a good thing to practice.
Also remove the i= i + 1 in your monitorTimer function, it is unneeded as the for loop automatically increments it.

Ah, thanks for pointing that out, It works now :)/>

Nice to know that you don't need to manually increment i in For loops and thanks for pointing out the problem, I hadn't noticed that at all. Also, thanks for the tip about converting the input to a string, that'll most likely prevent future problems :D/>
KRHN #5
Posted 03 April 2015 - 10:57 PM
I dont looked to your code but you can write center with this function;

local function centerText(string)
  local mX, mY = mon.getSize();
  x, y = term.getCursorPos();
  x = mX / 2
  x = x - string.len(string) / 2
  term.setCursorPos(x, y)
	 io.write(string)
end