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

Vertically scrolling text on monitor?

Started by TylerTharp, 20 July 2013 - 12:38 PM
TylerTharp #1
Posted 20 July 2013 - 02:38 PM
Title: Vertically scrolling text on monitor?

I work on computers in a server and someone asked me to create a welcome sign with text that scrolls from top to bottom, I can create a script that can scroll the text but only horizontally. I also need it to be centered on the monitor screens. I will post the code I am using as a base below.


term.clear()
term.setCursorPos(1,1)
term.setTextColor(8192)
print("Displaying text...")
mon = peripheral.wrap("back")
mon.setTextScale(4)
mon.setTextColor(16)
mon.setBackgroundColor(1024)
strtpos = 12
speed = .2
text = "message here"

pos = strtpos
poslimit = 0 - strtpos

while true do
mon.clear()
mon.setCursorPos(pos, 1)
mon.write(text)
sleep(speed)
if pos == poslimit then
  pos = strtpos
else
  pos = pos - 1
end
end
Lyqyd #2
Posted 20 July 2013 - 11:17 PM
Split into new topic.

Just use term.scroll and fill the next line as it goes?
TylerTharp #3
Posted 21 July 2013 - 03:11 AM
I have got myself all kinds of confused, could you show me an example of implementing this into my code? Sorry for the inconvenience, I am still very new to lua.
H4X0RZ #4
Posted 21 July 2013 - 03:19 AM
You could do it so:

--Yea, It'snot the best way
local mon = peripheral.wrap("yourSide")
local monWidth,monHeight = mon.getSize()
local speed = .2
local pos =1
local yourText = "test"

--Main loop
while true do
  mon.clear()
  mon.setCursorPos((monWidth/2)-(yourText:len()/2),pos)
  mon.write(yourText)
  sleep(speed)
  if pos < monHeight then
    pos = pos + 1
  else
    pos = 1
  end
end
TylerTharp #5
Posted 21 July 2013 - 03:40 AM
What if I were to use it like so?


		local mon = peripheral.wrap("yourSide")
		local monWidth,monHeight = mon.getSize()
		local speed = .1
		local pos =1
		local W = "W"
		local E = "E"
		local L = "L"
		local C = "C"
		local O = "O"
		local M = "M"
		local E2 = "E"
		while true do
		  mon.clear()
		  mon.setCursorPos((monWidth/2)-(#W,#E,#L,#C,#O,#M,#E2/2),pos)
		  mon.write(W)
		  mon.write(E)
		  mon.write(L)
		  mon.write(C)
		  mon.write(O)
		  mon.write(M)
		  mon.write(E2)
		  sleep(speed)
		  if pos < monHeight then
			pos = pos + 1
		  else
			pos = 1
		  end
		end
		
I get the error "attempt to get length of nil"
albrat #6
Posted 21 July 2013 - 09:25 AM
This code does not work. !!!
Spoiler

-- we need to split the string
text = "test message"
w,h = term.getSize()
for a = 1,string.len(text) do
x,y = term.getCursorPos()
term.setCursorPos(w/2, y)
if a < string.len(text) then print(string.sub(text, a, 1)) end
if a > 1 then
  term.setCursorPos(w/2, y-1)
  if a-1 < string.len(text) then print(string.sub(text, a-1, 1))
  else print(" ")
  end
end
if a > 2 then
  term.setCursorPos(w/2, y-2)
  if a-2 < string.len(text) then print(string.sub(text, a-2, 1))
  else print(" ")
  end
end
if a > 3 then
  term.setCursorPos(w/2, y-3)
  if a-3 < string.len(text) then print(string.sub(text, a-3, 1))
  else print(" ")
  end
end
if a > 4 then
  term.setCursorPos(w/2, y-4)
  if a-4 < string.len(text) then print(string.sub(text, a-4, 1))
  else print(" ")
  end
end
if a > 5 then
  term.setCursorPos(w/2, y-5)
  if a-5 < string.len(text) then print(string.sub(text, a-5, 1))
  else print(" ")
  end
end

sleep(0.8)
end

this will only print the text to the screen on 5 lines… I think it will require a set of if statements to make it scroll up , it is possible but not easy. lol


** or maybe even my code there will not work as intended….
*** tries a new approach but think I am on the right lines. lol
albrat #7
Posted 21 July 2013 - 09:54 AM
I tested a little in minecraft and this works…


-- we need to split the string
term.clear()
scroll = 6 -- we are showing 6 characters on the screen on our upwards scroll...
text = "test message"
w,h = term.getSize()
term.setCursorPos(w/2, h/2)
for a = 1,string.len(text)+scroll do
sleep(0.2)
textout = string.sub(text, a, a)
x,y = term.getCursorPos()
if a-1 < string.len(text) then term.setCursorPos(w/2, h/2 - 1) print(textout) else term.setCursorPos(w/2, h/2 - 1) print(" ") end
if a > 1 then
  term.setCursorPos(w/2, h/2-2)
  if a-2 < string.len(text) then print(string.sub(text, a-1, a-1))
  else print(" ")
  end
end
if a > 2 then
  term.setCursorPos(w/2, h/2-3)
  if a-3 < string.len(text) then print(string.sub(text, a-2, a-2))
  else print(" ")
  end
end
if a > 3 then
  term.setCursorPos(w/2, h/2-4)
  if a-4 < string.len(text) then print(string.sub(text, a-3, a-3))
  else print(" ")
  end
end
if a > 4 then
  term.setCursorPos(w/2, h/2-5)
  if a-5 < string.len(text) then print(string.sub(text, a-4, a-4))
  else print(" ")
  end
end
if a > 5 then
  term.setCursorPos(w/2, h/2-6)
  if a-6 < string.len(text) then print(string.sub(text, a-5, a-5))
  else print(" ")
  end
end
sleep(0.8)
end

scrolls "test message" upwards in the centre of the screen..
MR_nesquick #8
Posted 21 July 2013 - 01:16 PM
i made a test program that's print text vertical. going to see if i still got it

edit:

wohoo found it :)/>



Text = {
"Hello",
"e",
"l",
"l",
"o",
}
mon = peripheral.wrap("right")
mon.clear()
wScreen, hScreen = mon.getSize()

function Vertical()
 for i = -#Text,hScreen do
      for t = 1,#Text do
        mon.setCursorPos(wScreen/2,i+t)
        mon.write(Text[t])
      end
   mon.setCursorPos(wScreen/2,i)
   sleep(.2)
   mon.clear()
  end
end

while true do
 Vertical()
end
albrat #9
Posted 21 July 2013 - 02:23 PM
ok, I like the idea of this scrolling text. hehe. I went a little crazy on the idea too so here is a new and improoved version that gives customizable options.


scroll = 12 -- we are showing 6 characters on the screen on our upwards scroll...
speed = 0.2 -- length of sleep
text = "test message" -- our message
w,h = term.getSize() -- get screen size
posTexty = math.floor(h/2 + scroll/1.5) -- some strange math but /2 gave a odd effect so /1.5 works better
posTextx = math.floor(w/2+12) -- our x pos on the screen (i want a center text)

-- our Vertical print function

function vertical(text, lines, a, xpos, ypos)  -- text, number of lines, our pos in the text, x loc, y loc.
  for b = lines,1, -1 do  -- for each line of our scroll...
	  term.setCursorPos(xpos, ypos-(b+1)) -- set pos on screen
--      if a-b > h-1 or a-b < h-h+1 then printError(" Scroll over screen edge Error!! ")error() end
	  if a-b < string.len(text)+1 and a-b > 0 then
	    print(string.sub(text, a-b, a-B)/>)
	  else print(" ")
	  end
  end
end

-- main
term.clear()
for a = 1,string.len(text)+scroll+1 do  -- do our string length + scroll + 1 (so the last char is not missed)
sleep(speed) -- the speed of our scroll.
vertical(text, scroll, a, posTextx, posTexty) -- call the function.
end
-- EOF!!

The Forum code is a bit screwy, I keept getting %ltg; instead of ((…
Edited on 21 July 2013 - 12:49 PM
albrat #10
Posted 21 July 2013 - 02:52 PM
I am not using that bleeping edit button no more. there is an error in the above code that the forum post features is making.

scroll = 12 -- we are showing 6 characters on the screen on our upwards scroll...
speed = 0.2 -- length of sleep
text = "test message" -- our message
w,h = term.getSize() -- get screen size
posTexty = math.floor(h/2 + scroll/1.5) -- some strange math but /2 gave a odd effect so /1.5 works better
posTextx = math.floor(w/2+12) -- our x pos on the screen (i want a center text)
-- our Vertical print function
function vertical(text, lines, a, xpos, ypos)  -- text, number of lines, our pos in the text, x loc, y loc.
  for b = lines,1, -1 do  -- for each line of our scroll...
	  term.setCursorPos(xpos, ypos-(b+1)) -- set pos on screen
--   if a-b > h-1 or a-b < h-h+1 then printError(" Scroll over screen edge Error!! ")error() end
	  if a-b < string.len(text)+1 and a-b > 0 then
	    print(string.sub( text, a-b, a-b ))  -- here is where forum error occurs B)/>) turns to B)/>)
	  else print(" ")
	  end
  end
end
-- main
term.clear()
for a = 1,string.len(text)+scroll+1 do  -- do our string length + scroll + 1 (so the last char is not missed)
sleep(speed) -- the speed of our scroll.
vertical(text, scroll, a, posTextx, posTexty) -- call the function.
end
-- EOF!!

Repost due to buggy forum edit system.
TylerTharp #11
Posted 21 July 2013 - 03:34 PM
That works pretty good on the terminal, but how would I go about having it displayed on a monitor?
albrat #12
Posted 21 July 2013 - 03:48 PM
in the main section insert term.redirect(monitorside)
then term.restore() after the end of the loop.

-- main
term.redirect("right") -- assuming our monitor is on the right.
term.clear()
for a = 1,string.len(text)+scroll+1 do  -- do our string length + scroll + 1 (so the last char is not missed)
sleep(speed) -- the speed of our scroll.
vertical(text, scroll, a, posTextx, posTexty) -- call the function.
end
term.restore()
TylerTharp #13
Posted 21 July 2013 - 04:08 PM
I had a few errors that I fixed, but this one has me stumped even though I think its a simple fix.

Error: "term:16: Invalid redirect object"

Here is the code

mon.setTextScale(4)
mon.setTextColor(16)
mon.setBackgroundColor(1024)
scroll = 18
speed = 0.2
text = "test message"
w,h = term.getSize()
posTexty = math.floor(h/2 + scroll/1.5)
posTextx = math.floor(w/2+12)
function vertical(text, lines, a, xpos, ypos)
  for b = lines,1, -1 do
		  term.setCursorPos(xpos, ypos-(b+1))
		  if a-b < string.len(text)+1 and a-b > 0 then
		    print(string.sub( text, a-b, a-b ))
		  else print(" ")
		  end
  end
end
term.redirect("right")
term.clear()
for a = 1,string.len(text)+scroll+1 do
sleep(speed)
vertical(text, scroll, a, posTextx, posTexty)
end
term.restore()
albrat #14
Posted 21 July 2013 - 05:02 PM
may have to add " mon = peripheral.wrap( "right" ) "

Then make the line term.redirect(mon)
** confirms that works or
term.redirect( peripheral.wrap( "right" ) )
TylerTharp #15
Posted 21 July 2013 - 10:12 PM
It works for the most part, I am having a problem with the text size. And for somereason random characters pop up in the message. Here is a pic of what is happening..



And here is the code, I changed some things that may have caused it.


scroll = 12
speed = 0.2
text = "test message"
w,h = term.getSize()
posTexty = math.floor(h/2 + scroll/1.5)
posTextx = math.floor(w/2+12)
function vertical(text, lines, a, xpos, ypos)
  for b = lines,1, -1 do
		  term.setCursorPos(xpos, ypos-(b+1))		  if a-b < string.len(text)+1 and a-b > 0 then
		    print(string.sub( text, a-b, a-b ))
		  else print(" ")
		  end
  end
end
scroll = 12
speed = 0.2
text = "test message"
w,h = term.getSize()
posTexty = math.floor(h/2 + scroll/1.5)
posTextx = math.floor(w/2+12)
function vertical(text, lines, a, xpos, ypos)
  for b = lines,1, -1 do
		  term.setCursorPos(xpos, ypos-(b+1))		  if a-b < string.len(text)+1 and a-b > 0 then
		    print(string.sub( text, a-b, a-b ))
		  else print(" ")
		  end
  end
end
while true do
term.redirect( peripheral.wrap( "right" ) )
term.clear()
for a = 1,string.len(text)+scroll+1 do
sleep(speed)
vertical(text, scroll, a, posTextx, posTexty)
end
term.restore()
end
albrat #16
Posted 22 July 2013 - 08:13 AM
hm, try this code adjustment.

mon = peripheral.wrap("right")
scroll = 12 -- we are showing 6 characters on the screen on our upwards scroll...
speed = 0.2 -- length of sleep
text = "test message" -- our message
w,h = mon.getSize() -- get screen size
posTexty = math.floor( h/2 + scroll/1.5 ) -- some strange math but /2 gave a odd effect so /1.5 works better
posTextx = math.floor( w/2 ) -- our x pos on the screen (i want a center text)

-- our Vertical print function

function vertical(text, lines, a, xpos, ypos)  -- text, number of lines, our pos in the text, x loc, y loc.
  for b = lines,1, -1 do  -- for each line of our scroll...
	  mon.setCursorPos(xpos, ypos-(b+1)) -- set pos on screen
	  if a-b < string.len(text)+1 and a-b > 0 then
	    mon.write(string.sub(text, a-b, a-B)/>)
	  else mon.clearLine()
	  end
  end
end

-- main
while true do
mon.clear()
for a = 1,string.len(text)+scroll+1 do  -- do our string length + scroll + 1 (so the last char is not missed)
sleep(speed) -- the speed of our scroll.
vertical(text, scroll, a, posTextx, posTexty) -- call the function.
end
end
-- EOF!!