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

[How to] On startup display message on monitor

Started by Bruno, 11 May 2012 - 11:57 PM
Bruno #1
Posted 12 May 2012 - 01:57 AM
So, I built an office complex, and I want a monitor to display text messages centered on it saying "Welcome to Weegee Towers!"

How can I program it so that it says "Welcome to Weegee Towers!" on the monitor, which is located to the left? It is a 4x1 monitor display.
MysticT #2
Posted 12 May 2012 - 02:05 AM
Try this code:

local sText = "Welcome to Weegee Towers!" -- You can change the text
local sSide = "left" -- you can change the side of the monitor

local function printCenter(mon, txt)
  local w, h = mon.getSize()
  local x = math.floor((w / 2) - (#txt / 2))
  local y = math.floor((h / 2))
  mon.setCursorPos(x, y)
  mon.write(txt)
end

local monitor = peripheral.wrap(sSide)
printCenter(monitor, sText)
I haven't tested this, so it may have some error.
Bruno #3
Posted 12 May 2012 - 02:30 AM
Error.

bios:355: bad argument: string expected, got nil
Bruno #4
Posted 12 May 2012 - 02:34 AM
Nevermind, it was user error.

Thank you very much.

How did you learn lua so I can learn it and stop pestering you? :)/>/>
MysticT #5
Posted 12 May 2012 - 03:01 AM
Well, the language is really easy to learn, specially if you already know about programming (i learned c/c++, c#, java, ruby, and others before). I would recommend reading some tutorials and the lua manual on it's site and wiki. Then, to learn about CC specifics you can go to the wiki. And of course, the best way is to practice, try things out and learn from your errors.
You can always come back with any questions and we'll try to help you.
Happy coding! :)/>/>
EmTeaKay #6
Posted 12 May 2012 - 03:07 AM
I learned by reading others code. Which, by my opinion, is a good way to learn. Try doing a password system for a door first. That's one of the first ones I did. And after you did that, build apon that, make it cleaner code, more user friendly, make it into DoorOS. Most importantly, learn by your mistakes.
Bruno #7
Posted 12 May 2012 - 04:19 AM
How can I modify the text? I increased screen size to 2x4, and there is room for two different rows of text, but when I add to the text line, both are clipped off the screen.
MysticT #8
Posted 12 May 2012 - 07:13 PM
I made a few changes to the code:

local tText = {
"First line", -- you can change the lines here
"Second line",
"Some more",
"lines here"
-- and you can add more if you want, just don't forget to put the commas (,) at the end of the lines (except for the last one)
}
local sSide = "left"

local function printCenter(mon, t)
  local w, h = mon.getSize()
  local y = math.floor((h / 2) - (#t / 2)) - 1
  for i, line in ipairs(t) do
	local x = math.floor((w / 2) - (#line / 2))
	mon.setCursorPos(x, y + i)
	mon.write(line)
  end
end

printCenter(peripheral.wrap(sSide), tText)
It writes the lines of text you want, allways centered on the monitor.
tigerman4527 #9
Posted 13 July 2012 - 11:48 PM
Using the revised code, how would I change the text size?
MysticT #10
Posted 13 July 2012 - 11:58 PM
Well, this is a really old post, but anyway.
This should work:

local tText = {
"First line", -- you can change the lines here
"Second line",
"Some more",
"lines here"
-- and you can add more if you want, just don't forget to put the commas (,) at the end of the lines (except for the last one)
}
local sSide = "left"
local nTextSize = 1 -- change the text size here (from 1 to 5)

local function printCenter(mon, t)
  local w, h = mon.getSize()
  local y = math.floor((h / 2) - (#t / 2)) - 1
  for i, line in ipairs(t) do
	local x = math.floor((w / 2) - (#line / 2))
	mon.setCursorPos(x, y + i)
	mon.write(line)
  end
end

local mon = peripheral.wrap(sSide)
mon.setTextScale(nTextSize)
printCenter(mon, tText)
Rubixmaster5567 #11
Posted 08 August 2012 - 06:19 PM
Try this code:

local sText = "Welcome to Weegee Towers!" -- You can change the text
local sSide = "left" -- you can change the side of the monitor

local function printCenter(mon, txt)
  local w, h = mon.getSize()
  local x = math.floor((w / 2) - (#txt / 2))
  local y = math.floor((h / 2))
  mon.setCursorPos(x, y)
  mon.write(txt)
end

local monitor = peripheral.wrap(sSide)
printCenter(monitor, sText)
I haven't tested this, so it may have some error.

i typed in this codes EXACTLY but im get this error: "attempt to index ? (a nil value)"

HELP ME
MysticT #12
Posted 08 August 2012 - 08:55 PM
Try this code:

local sText = "Welcome to Weegee Towers!" -- You can change the text
local sSide = "left" -- you can change the side of the monitor

local function printCenter(mon, txt)
  local w, h = mon.getSize()
  local x = math.floor((w / 2) - (#txt / 2))
  local y = math.floor((h / 2))
  mon.setCursorPos(x, y)
  mon.write(txt)
end

local monitor = peripheral.wrap(sSide)
printCenter(monitor, sText)
I haven't tested this, so it may have some error.

i typed in this codes EXACTLY but im get this error: "attempt to index ? (a nil value)"

HELP ME
Check that sSide matches the side of your monitor.