7 posts
Posted 30 March 2014 - 04:41 AM
I'm needing help on centering text on a 2x5 monitor. I've used the code that has been posted on other related topics but none seem to work. I want it to say on line 1: "Welcome To" and on line 2: "Town". Any help will be greatly appreciated.
7083 posts
Location
Tasmania (AU)
Posted 30 March 2014 - 07:59 AM
local mon = peripheral.wrap("top") -- Or whatever direction the monitor is in.
local xSize, ySize = mon.getSize() -- We need to know how many columns exist on the display.
local myMessages = {"Welcome To","Town"} -- Cram our strings in a table.
mon.clear()
for i=1,#myMessages do -- For every index in the "myMessages" table, ...
mon.setCursorPos(math.floor((xSize-#myMessages[i])/2), i) -- Position the cursor.
mon.write(myMessages[i]) -- Write the current table entry.
end
Sticking a # symbol in front of a given variable generally gives you the "length" of that variable. In the case of a table (eg "myMessages")you get back the number of entries in it - in the case of a string, you get back the number of characters (eg, "myMessages[1]" represents the string "Welcome To").
math.floor() rounds down to the nearest integer. To go up instead, use math.ceil().
Edited on 30 March 2014 - 06:00 AM
7 posts
Posted 30 March 2014 - 04:20 PM
It returns '=' expected on line 7. I typed everything on that line.
1852 posts
Location
Sweden
Posted 30 March 2014 - 05:03 PM
It returns '=' expected on line 7. I typed everything on that line.
Post your current code, Bomb Blokes code seems correct so you must have messed up something
7 posts
Posted 30 March 2014 - 05:34 PM
local mon = peripheral.wrap("back")
local xSize, ySize = mon.getSize()
local myMessages = {"Welcome To","Town"}
mon.clear
for i=1,#myMessages do
mon.setCursorPos(math.floor((xSize-myMessages[i])/2, i)
mon.write(myMessages[i])
end
Edited on 30 March 2014 - 03:53 PM
1281 posts
Posted 30 March 2014 - 05:58 PM
mon.clear
mon.clear()
See the difference?
7 posts
Posted 30 March 2014 - 06:53 PM
mon.clear
mon.clear()
See the difference?
Ok i fixed that, but now I have an error saying "startup:8: attempt to preform arithmetic __sub on nil and string"
1281 posts
Posted 30 March 2014 - 07:06 PM
mon.setCursorPos(math.floor((xSize-#myMessages[i])/2), i)
mon.setCursorPos(math.floor((xSize-myMessages[i])/2, i)
We're gonna be doing this all day aren't we… Take a look at the line yourself next time, please.
Also, this
xSize-myMessages
is the only subtraction in your entire code, however xSize should not be nil because
local xSize, ySize = mon.getSize()
So i'd suggest you take a look at that line aswell.
7 posts
Posted 30 March 2014 - 07:55 PM
mon.setCursorPos(math.floor((xSize-#myMessages[i])/2), i)
mon.setCursorPos(math.floor((xSize-myMessages[i])/2, i)
We're gonna be doing this all day aren't we… Take a look at the line yourself next time, please.
Also, this
xSize-myMessages
is the only subtraction in your entire code, however xSize should not be nil because
local xSize, ySize = mon.getSize()
So i'd suggest you take a look at that line aswell.
Oh dang….. I didn't see that there… It's working now. Thanks for all the help.