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

Billboard Code

Started by eccentricayman, 16 September 2015 - 01:52 AM
eccentricayman #1
Posted 16 September 2015 - 03:52 AM
Does anyone know why this code doesn't work?
It's original intention was to get a redstone signal from a redstone conduit in the form of a number, and read it and display something on a monitor based upon the results.

mon = peripheral.wrap("back")

while true do
  mon.clear()
  mon.setCursorPos(1,1)
  local x2,y2 = mon.getCursorPos()
  local x,y = mon.getSize()
  mon.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), math.ceil(y / 2))
  mon.setTextScale(4) 
  if rs.getBundledInput("front") == 128 then
    mon.write("Welcome Mohian!")
  elseif rs.getBundledInput("front") == 256 then
    mon.write("Welcome Farhan!")
  elseif rs.getBundledInput("front") == 2048 then
    mon.write("Welcome Evolutionary!")
  else
    if rs.getBundledInput("front") ~= 0 then
	  mon.write("Welcome Visitors!")
    end
  end
end
Bomb Bloke #2
Posted 16 September 2015 - 04:56 AM
Your main problem is that you're attempting to get the length of a "text" variable that you've never bothered to set, but there's also the issue where you never allow your code to yield.

Here's a somewhat cleaned version:

local mon = peripheral.wrap("back")
local x, y = mon.getSize()  -- Some tasks shouldn't need to be repeated.
mon.setTextScale(4)

while true do
	local text
	if rs.testBundledInput("front", colours.grey) then
		text = "Welcome Mohian!"
	elseif rs.testBundledInput("front", colours.lightGrey) then
		text = "Welcome Farhan!"
	elseif rs.testBundledInput("front", colours.blue) then
		text = "Welcome Evolutionary!"
	elseif rs.getBundledInput("front") ~= 0 then
		text = "Welcome Visitors!"
	end

	mon.clear()
	mon.setCursorPos(math.floor((x - text:len()) / 2) + 1, math.floor(y / 2) + 1)
	mon.write(text)
	
	os.pullEvent("redstone")  -- Yield until a redstone state change occurs.
end
eccentricayman #3
Posted 16 September 2015 - 11:03 PM
Thank you so much!

About the cleaned code you gave, it returns an index a nil value error on line 18 whenever the redstone signal turns off, or when the program is started without an active signal. Any suggestions?
KingofGamesYami #4
Posted 17 September 2015 - 12:12 AM
Inside the while loop, change this:

local text
To this:

local text = ""
Edited on 16 September 2015 - 10:12 PM
eccentricayman #5
Posted 17 September 2015 - 12:16 AM
That did the trick. Thanks!

One last question, and sorry for the trouble :)/>/>
I changed up the code slightly so it all fits on the screen nicer, but when the redstone signal isn't active the monitor displays "Welcome" instead of being blank. Does anybody know why?

http://pastebin.com/H6hDzD7p
KingofGamesYami #6
Posted 17 September 2015 - 01:27 PM

local mon = peripheral.wrap("top")
local x, y = mon.getSize()  -- Some tasks shouldn't need to be repeated.
mon.setTextScale(3)

while true do
        local text
        if rs.testBundledInput("front", colours.grey) then
                text = "Mohian!"
        elseif rs.testBundledInput("front", colours.lightGrey) then
                text = "Farhan!"
        elseif rs.testBundledInput("front", colours.blue) then
                text = "Evolutionary!"
        elseif rs.getBundledInput("front") ~= 0 then
                text = "Visitors!"
        end

        mon.clear()
        if text then
          mon.setCursorPos(math.floor((x - text:len()) / 2) + 1, 1)
          mon.write("Welcome")
          mon.setCursorPos(math.floor((x - text:len()) / 2) + 1, 2)       
          mon.write(text)
        end
        os.pullEvent("redstone")  -- Yield until a redstone state change occurs.
end