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

Monitor Scrolling text

Started by Polyryph, 28 March 2012 - 10:03 AM
Polyryph #1
Posted 28 March 2012 - 12:03 PM
Hi there,

I've recently set up computercraft 1.31 and i'm tinkering with the new monitors, which has been fantastic.. Took me a little while to find the commands for them but I got it in the end.

I have come up with an idea which I know is possible and I know a way of doing it. But i'm trying (hoping) to find a easier way of doing it. That is scrolling text, i've set up monitors 2 high and 7 wide, I would like text to scroll from left to right, much like a news feed you see on some news programs on tv or stock market figures.

As I said, I have a fair idea of how I can do it. But I would like to get some feedback on the issue and see if anyone knows a more efficient way of doing it.

I'll be sure to post results once the task is complete.

ps. My mind is more being spent on a much larger program i'm working on at the moment so it's blocking me from figuring this one out.

Thanks =D

Pøly
Advert #2
Posted 28 March 2012 - 12:19 PM
You'd have to store the entire string, then use string.sub to scroll it, along with timers to update the text.
Polyryph #3
Posted 28 March 2012 - 12:22 PM
That is what I thought..

I will assume, not wrongly I hope. That, that is the best option I have.

Thanks for the help =)
Evis #4
Posted 31 March 2012 - 06:27 AM
I just got them working, any links to resources? I got three pages of search results, but don't see any guides.
toxicwolf #5
Posted 01 April 2012 - 10:23 PM
I created a text scrolling function for my news board:

function scrollText(tStrings, nRate)
nRate = nRate or 5
if nRate < 0 then
  error("rate must be positive")
end
local nSleep = 1 / nRate

width, height = mon.getSize()
x, y = mon.getCursorPos()
	sText = ""
	for n = 1, #tStrings do
		sText = sText .. tostring(tStrings[n])
  sText = sText .. " | "
	end
	sString = "| "
	if width / string.len(sText) < 1 then
		nStringRepeat = 3
	else
		nStringRepeat = math.ceil(width / string.len(sText) * 3)
	end
for n = 1, nStringRepeat do
  sString = sString .. sText
end
while true do
  for n = 1, string.len(sText) do
   sDisplay = string.sub(sString, n, n + width - 1)
   mon.clearLine()
   mon.setCursorPos(1, y)
   mon.write(sDisplay)
   sleep(nSleep)
  end
end
end
Note that the variable "mon" can be set to a monitor using "mon = peripheral.wrap(sSide)", or can be replaced with "term" to write all text to a terminal.

I created an example news board script as well:

mon = peripheral.wrap("left")
function lineBreak()
    x, y = mon.getCursorPos()
    if y ~= 1 then
	    y = y + 1
    end
    mon.setCursorPos(1, y)
    width, height = mon.getSize()
    mon.write("+" .. string.rep("-", width - 2) .. "+")
end
function printString(sString)
    x, y = mon.getCursorPos()
    y = y + 1
    mon.setCursorPos(1, y)
    mon.write(sString)
end
function printStringCentre(sString)
    x, y = mon.getCursorPos()
    y = y + 1
    mon.setCursorPos(1, y)
    width, height = mon.getSize()
    nStringCentre = math.floor(string.len(sString) / 2)
    nMonitorCentre = math.floor(width / 2)
    x = math.floor(nMonitorCentre - nStringCentre)
    mon.setCursorPos(x, y)
    mon.write(sString)
end
function printStringRight(sString)
width, height = mon.getSize()
x, y = mon.getCursorPos()
y = y + 1
x = math.ceil(width - string.len(sString))
mon.setCursorPos(x, y)
mon.write(sString)
end
function scrollText(tStrings, nRate)
nRate = nRate or 5
if nRate < 0 then
  error("rate must be positive")
end
local nSleep = 1 / nRate

width, height = mon.getSize()
x, y = mon.getCursorPos()
    sText = ""
    for n = 1, #tStrings do
	    sText = sText .. tostring(tStrings[n])
  sText = sText .. " | "
    end
    sString = "| "
    if width / string.len(sText) < 1 then
	    nStringRepeat = 3
    else
	    nStringRepeat = math.ceil(width / string.len(sText) * 3)
    end
for n = 1, nStringRepeat do
  sString = sString .. sText
end
while true do
  for n = 1, string.len(sText) do
   sDisplay = string.sub(sString, n, n + width - 1)
   mon.clearLine()
   mon.setCursorPos(1, y)
   mon.write(sDisplay)
   sleep(nSleep)
  end
end
end
mon.clear()
mon.setCursorPos(1, 1)
lineBreak()
printStringCentre("|News Board|")
lineBreak()
printString("")
lineBreak()
tScrollText = {}
tScrollText[1] = "BREAKING NEWS"
tScrollText[2] = "Giant cookies falling from sky"
x, y = mon.getCursorPos()
y = y - 1
mon.setCursorPos(1, y)
scrollText(tScrollText)
The scrollText function can support any amount of table entries or any length of string from what I've seen so far in testing.

Hope this helps! :)/>/>
xXLeNinjaXx #6
Posted 06 January 2014 - 04:19 PM
I created a text scrolling function for my news board:

function scrollText(tStrings, nRate)
nRate = nRate or 5
if nRate < 0 then
  error("rate must be positive")
end
local nSleep = 1 / nRate

width, height = mon.getSize()
x, y = mon.getCursorPos()
	sText = ""
	for n = 1, #tStrings do
		sText = sText .. tostring(tStrings[n])
  sText = sText .. " | "
	end
	sString = "| "
	if width / string.len(sText) < 1 then
		nStringRepeat = 3
	else
		nStringRepeat = math.ceil(width / string.len(sText) * 3)
	end
for n = 1, nStringRepeat do
  sString = sString .. sText
end
while true do
  for n = 1, string.len(sText) do
   sDisplay = string.sub(sString, n, n + width - 1)
   mon.clearLine()
   mon.setCursorPos(1, y)
   mon.write(sDisplay)
   sleep(nSleep)
  end
end
end
Note that the variable "mon" can be set to a monitor using "mon = peripheral.wrap(sSide)", or can be replaced with "term" to write all text to a terminal.

I created an example news board script as well:

mon = peripheral.wrap("left")
function lineBreak()
	x, y = mon.getCursorPos()
	if y ~= 1 then
		y = y + 1
	end
	mon.setCursorPos(1, y)
	width, height = mon.getSize()
	mon.write("+" .. string.rep("-", width - 2) .. "+")
end
function printString(sString)
	x, y = mon.getCursorPos()
	y = y + 1
	mon.setCursorPos(1, y)
	mon.write(sString)
end
function printStringCentre(sString)
	x, y = mon.getCursorPos()
	y = y + 1
	mon.setCursorPos(1, y)
	width, height = mon.getSize()
	nStringCentre = math.floor(string.len(sString) / 2)
	nMonitorCentre = math.floor(width / 2)
	x = math.floor(nMonitorCentre - nStringCentre)
	mon.setCursorPos(x, y)
	mon.write(sString)
end
function printStringRight(sString)
width, height = mon.getSize()
x, y = mon.getCursorPos()
y = y + 1
x = math.ceil(width - string.len(sString))
mon.setCursorPos(x, y)
mon.write(sString)
end
function scrollText(tStrings, nRate)
nRate = nRate or 5
if nRate < 0 then
  error("rate must be positive")
end
local nSleep = 1 / nRate

width, height = mon.getSize()
x, y = mon.getCursorPos()
	sText = ""
	for n = 1, #tStrings do
		sText = sText .. tostring(tStrings[n])
  sText = sText .. " | "
	end
	sString = "| "
	if width / string.len(sText) < 1 then
		nStringRepeat = 3
	else
		nStringRepeat = math.ceil(width / string.len(sText) * 3)
	end
for n = 1, nStringRepeat do
  sString = sString .. sText
end
while true do
  for n = 1, string.len(sText) do
   sDisplay = string.sub(sString, n, n + width - 1)
   mon.clearLine()
   mon.setCursorPos(1, y)
   mon.write(sDisplay)
   sleep(nSleep)
  end
end
end
mon.clear()
mon.setCursorPos(1, 1)
lineBreak()
printStringCentre("|News Board|")
lineBreak()
printString("")
lineBreak()
tScrollText = {}
tScrollText[1] = "BREAKING NEWS"
tScrollText[2] = "Giant cookies falling from sky"
x, y = mon.getCursorPos()
y = y - 1
mon.setCursorPos(1, y)
scrollText(tScrollText)
The scrollText function can support any amount of table entries or any length of string from what I've seen so far in testing.

Hope this helps! :)/>/>

could you make it so if u have layers of computers it shows multipul like
[Line One] the cookies are falling ]
[Line Two]
[Line Three]
etc..

make the top scrolling but bewlow it it shows solid text