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

Center Text Program - (Help!)

Started by ColbiesTheName, 08 June 2016 - 08:42 PM
ColbiesTheName #1
Posted 08 June 2016 - 10:42 PM
Quick info first - I quit playing minecraft for a while in favor of Garry's Mod. There I programmed with E2, which is sort of similar to LUA. Anyway, I came back to minecraft (specifically because a server i used to play on changed packs), and I've started doing CC again.

I can't figure out wtf is wrong with my code and I need help.
I'm probably just making a noob mistake, but could someone help me out?

while (side==nil) do
  term.clear()
  term.setCursorPos(1,1)
  term.write("Center Text Program, made by ColbiesTheName.")
  term.setCursorPos(1,3)
  term.write("Enter the side your monitor is touching this computer.")
  term.setCursorPos(1,4)
  term.write("(top, bottom, left, right, back, front)")
  side = read()
  os.sleep(0.5)
end

while (text==nil) do	--The error is happening on this line.
  term.clear()
  term.setCursorPos(1,1)
  term.write("Center Text Program made by ColbiesTheName.")
  term.setCursorPos(1,3)
  term.write("Please enter the text you would like to be written.")
  text = read()
  os.sleep(0.5)
end

local function centerText(text)
  mon = peripheral.wrap(side)

  if (mon == nil) do
	term.clear()
	term.setCursorPos(1,1)
	term.write("Incorrect side! Please restart this program.")
	os.sleep(10)
  else do

	sizex,sizey = mon.getScale()
	textlength = text.len()
	writex = math.floor(((sizex / 2) - (textlength / 2)))
	writey = math.floor(((sizey / 2) - 1))
	mon.setCursorPos(writex,writey)
	mon.write(text)
  end
end
It says "bios:14: [string "centerText"]:28: 'then' expected" and I have no idea why. There shouldn't be a need for a 'then' at line 14 (I marked where it is in the code.)
Dog #2
Posted 09 June 2016 - 01:22 AM
Hey ColbiesTheName,

Welcome to the CC forums :)/>

The error statement you provided says the problem is on line 28, not where you think it's happening. However, in your case, the error is actually elsewhere…
Your if statements are malformed. They should read as if/then not if/do. Also your 'else do' should be an 'else'.
Edited on 08 June 2016 - 11:30 PM
ReBraLaCC #3
Posted 09 June 2016 - 12:45 PM
I don't get it why you are getting textlength of a nil value, i would say use "textlength = #text" # returns the length of the input..

Never used monitors that much to be honest, need to get into them :)/>
Dog #4
Posted 09 June 2016 - 03:49 PM
ReBraLaCC brings up a good point. textlength = text.len() should be textlength = text:len() - note the colon instead of the dot. You could also use string.len(text) (note the dot in this case). Another way to do it would be, as ReBraLaCC mentioned - textlength = #text

Either way will get you the same result.
Edited on 09 June 2016 - 01:54 PM
TheRockettek #5
Posted 09 June 2016 - 05:18 PM
XD alot of ways to just find the length of a string
ReBraLaCC #6
Posted 11 June 2016 - 10:09 PM
And if you need a text centering function look at this http://www.computercraft.info/forums2/index.php?/topic/26856-new-update-paintu-paint-api/ Its an API that could be very usefull!
The_Cat #7
Posted 12 June 2016 - 09:36 AM
XD alot of ways to just find the length of a string

I mean there is another I could think off ;)/>

text = "Hello"
cursorX, __ = term.getCursorPos()
write(text)
cursorXAfter, _ = term.getCursorPos()
stringLength = cursorXAfter - cursorX
Would NOT recommend. :D/>
ReBraLaCC #8
Posted 12 June 2016 - 09:57 AM

text = "Hello"
cursorX, __ = term.getCursorPos()
write(text)
cursorXAfter, _ = term.getCursorPos()
stringLength = cursorXAfter - cursorX
Would NOT recommend. :D/>/>

Don't you need to do -1 also then? I can't test it so ;)/>
The_Cat #9
Posted 12 June 2016 - 10:20 AM

Nope, I tested it. :P/>