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

monitors flashing different colors for teleport

Started by Patey, 10 May 2013 - 09:44 PM
Patey #1
Posted 10 May 2013 - 11:44 PM
theres 4 monitors surrounding the player flashing differen't colors, but I can't get it to work right…

I codeded this

local mon = peripheral.wrap("monitor_4")
local mon2 = peripheral.wrap("monitor_5")
local mon3 = peripheral.wrap("monitor_6")
local mon4 = peripheral.wrap("monitor_7")

mon.setBackgroundColor(colors.red)
mon2.setBackgroundColor(colors.yellow)
mon3.setBackgroundColor(colors.green)
mon4.setBackgroundColor(colors.cyan)

mon.setTextColor(colors.blue)
mon2.setTextColor(colors.red)
mon3.setTextColor(colors.black)
mon4.setTextColor(colors.gray)

mon.setCursorPos(1,1)
mon2.setCursorPos(1,1)
mon3.setCursorPos(1,1)
mon4.setCursorPos(1,1)

mon.clear()
mon2.clear()
mon3.clear()
mon4.clear()

mon.write("Teleporting...")
mon2.write("Teleporting...")
mon3.write("Teleporting...")
mon4.write("Teleporting...")


os.sleep(1)


mon.setBackgroundColor(colors.lime)
mon2.setBackgroundColor(colors.lightblue)
mon3.setBackgroundColor(colors.pink)
mon4.setBackgroundColor(colors.orange)

mon.setTextColor(colors.blue)
mon2.setTextColor(colors.black)
mon3.setTextColor(colors.gray)
mon4.setTextColor(colors.green)

mon.setCursorPos(1,1)
mon2.setCursorPos(1,1)
mon3.setCursorPos(1,1)
mon4.setCursorPos(1,1)

mon.clear()
mon2.clear()
mon3.clear()
mon4.clear()

mon.write("Teleporting...")
mon2.write("Teleporting...")
mon3.write("Teleporting...")
mon4.write("Teleporting...")

On the terminal, so I copied and pasted the second colors and changed the colors around to make this:

http://pastebin.com/gvEVChV9

but when i retrieve that from pastebin i get error:36:expected number.

I can't figure out where I wen't wrong, can anyone help me?
Zoinky #2
Posted 11 May 2013 - 12:16 AM
You've wrapped the monitor incorrectly. You need to tell it what side the monitor is on, so


local mon = peripheral.wrap("monitor_4")

Should be:


local mon = peripheral.wrap("top") -- "Top" is the side that the monitor is on (Just an example)

Also, Lua is case sensitive. So line 36 should be


mon2.setBackgroundColor(colors.lightBlue) -- Notice the capital B, every word after the first usually has a capital letter.

EDIT: One more thing, just use sleep() instead of os.sleep()
OmegaVest #3
Posted 11 May 2013 - 02:01 AM
Actually, using monitor_4 is correct for a wired modem connection. Since grabbing "top" would grab the modem(were that the side the wire was on), you have to use its name as the side. Everything else was right in Zoinky's post.
Zoinky #4
Posted 11 May 2013 - 03:22 AM
Actually, using monitor_4 is correct for a wired modem connection. Since grabbing "top" would grab the modem(were that the side the wire was on), you have to use its name as the side. Everything else was right in Zoinky's post.

Oh, sorry. I haven't got to use wired modems yet :S
H4X0RZ #5
Posted 11 May 2013 - 03:42 AM
Improvement:

randcolors = {colors.cyan, colors.blue, colors.lime, colors.green, colors.red} --you can add more colors if you want
sides = {left, right, top, back} --the sides where your computers are

last_computer = 1
--Peripheral wrapping
for k,v in pairs(sides) do
mon..last_computer = peripheral.wrap(v)
last_computer = last_computer + 1
end

while true do
for k,v in pairs(sides) do
txtrand = math.random(1, #colors)
bgrand = math.random(1, #colors)
mon..k.setBackgroundColor(randcolors[bgrand])
mon..k.setTextColor(randcolors[txtrand])
mon..k.write("Teleporting...")
end
sleep(0.5)
end
Espen #6
Posted 11 May 2013 - 06:34 AM
@Freack100:
Before you claim you have improved some code, you should at least test it once to see if your supposed improvement actually works.
Don't get me wrong, I'm not trying to discourage you from posting improvements, but in this particular instance your code makes no sense as it contains odd errors, doesn't run and thus is not an improvement at all.

Some specific pointers:
You can't just concatenate mon with a number and wrap a peripheral to it. Especially not if mon+NUMBER is a string.
Your sides table is redundant because there is a native rs.getSides() function which does the same.
You used #colors when you really want to count the number of entries of your own color pallette from the beginning of the code.
To make the whole background change color, you need to clear the screen after setting the color.
Also you should only wrap a peripheral, if there actually is a peripheral connected to a particular side AND it is the type of peripheral you want.

Here's a tested example that makes your code run:
local pallette = {colors.cyan, colors.blue, colors.lime, colors.green, colors.red} --you can add more colors if you want
local last_computer = 0
local mon = {}

-- Peripheral wrapping
for k, v in pairs(rs.getSides()) do
  if peripheral.isPresent(v) and peripheral.getType(v) == "monitor" then
    last_computer = last_computer + 1
    mon[last_computer] = peripheral.wrap(v)
  end
end

while true do
  for i, v in ipairs(mon) do
    local txtrand = math.random(1, #pallette)
    local bgrand = math.random(1, #pallette)
    mon[i].setBackgroundColor(pallette[bgrand])
    mon[i].clear()
    mon[i].setTextColor(pallette[txtrand])
    mon[i].write("Teleporting...")
  end
  sleep(0.5)
end

Again, please don't be discouraged by this, but try to take something away from it.
It doesn't help anyone if you try to help without testing your own code, but potentially creates even more confusion than there was to begin with.
H4X0RZ #7
Posted 11 May 2013 - 06:48 AM
@Freack100:
Before you claim you have improved some code, you should at least test it once to see if your supposed improvement actually works.
Don't get me wrong, I'm not trying to discourage you from posting improvements, but in this particular instance your code makes no sense as it contains odd errors, doesn't run and thus is not an improvement at all.

Some specific pointers:
You can't just concatenate mon with a number and wrap a peripheral to it. Especially not if mon+NUMBER is a string.
Your sides table is redundant because there is a native rs.getSides() function which does the same.
You used #colors when you really want to count the number of entries of your own color pallette from the beginning of the code.
To make the whole background change color, you need to clear the screen after setting the color.
Also you should only wrap a peripheral, if there actually is a peripheral connected to a particular side AND it is the type of peripheral you want.

Here's a tested example that makes your code run:
local pallette = {colors.cyan, colors.blue, colors.lime, colors.green, colors.red} --you can add more colors if you want
local last_computer = 0
local mon = {}

-- Peripheral wrapping
for k, v in pairs(rs.getSides()) do
  if peripheral.isPresent(v) and peripheral.getType(v) == "monitor" then
    last_computer = last_computer + 1
    mon[last_computer] = peripheral.wrap(v)
  end
end

while true do
  for i, v in ipairs(mon) do
    local txtrand = math.random(1, #pallette)
    local bgrand = math.random(1, #pallette)
    mon[i].setBackgroundColor(pallette[bgrand])
    mon[i].clear()
    mon[i].setTextColor(pallette[txtrand])
    mon[i].write("Teleporting...")
  end
  sleep(0.5)
end

Again, please don't be discouraged by this, but try to take something away from it.
It doesn't help anyone if you try to help without testing your own code, but potentially creates even more confusion than there was to begin with.
Yea, sorry for that, I was typing from my smartphone ^_^/>